Variables - String Variables

Top  Previous  Next

 

For everything else except numbers used for math operations - you will use string variables.

Letters, words, sentences, paragraphs - are natural contents of string variables !

 

VariableVaseString

 

If we take this crystal glass and put words in it - we get string variable.

Just like that glass on image above, important part of string variable in MMB is label.

String variable label consits of name and $ sign.

Here are some examples of valid labels:

i$

n$

name$

1st_user$

Main_Title$

TextContent$

RegKey$

glass$

 

Never forget to attach $ after label name. That suffix tells MMB "This is string variable".

 

It is recommended to use meaningful & descriptive labels, not too short, not too long - just enough to describe it's content. If you'll use string variable to store user's answer to displayed question, label of that variable should be UserAnswer or AnswerNo1 .

Of course, important part of this story is - assigning of variable content.

 

VarNumericalGlassWords

 

In MMB script code lines, it is being done by:

a) writing label name:

sentence

b) adding suffix:

$

c) followed by equal sign:

=

d) and writing content (value) of variable at the end, behind label, $ suffix and equal sign.

Contents is enclosed in single quotes:

'My first sentence in MMB string variable !'

 

All together, MMB code line for assigning of sentece to string variable looks like this:

sentence$= 'My first sentence in MMB string variable !'

 

There ! With this line you instructed MMB: "Take a string variable sentence and put sentence My first sentence in MMB string variable ! in it."

Where will you give that instruction ? In MMB's Script Editor, of course.

Now, you're probably wondering: "What should I do with strings that contain ' character ?"

If you have a string:

What's this ?

...in MMB's string variable it will look like:

sentence$= 'What\'s this ?'

You use backslash \ to tell MMB "here ya go, I'm using single quote in my string variable..."

While backslash is obviously being used for this case in string variables, we must take care of the case when backslash is used for path to some directory (folder). String variable containing path to folder looks like this:

path$= 'c:\windows\\'

Example above contains double backslash, needed at the end of the string. Explanation is very simple - MMB uses one backslash before ' character so it can display that ' character.

If you have path in string, last backslash must be understood as "hey, we've got the end of a string, not ' character" , and the only way to do this is by using - double backslash.

 

Very nice. And just like in numerical variable case, the question is: can you only assign fixed words and never change 'em again ?

Nope !

It is important to have ability of assigning value of one variable to the value of another variable. Here's an example. If you have one bowl with words in it:

WordBowl

...what will you do to fill another bowl with words ?

You will copy words

from the first bowl

WordBowl

bowl1

=

bowl2

 

In MMB script code lines, it is being done by:

a) writing label of destination string variable (one that accepts value) with $ suffix:

bowl2$

b) followed by equal sign:

=

c) and writing label of source string variable (one that gives value) with $ suffix:

bowl1$

 

All together, MMB code line for assigning content of one string variable to another string variable looks like this::

bowl2$=bowl1$

If variable bowl1$ contains word Hello, code line above will assign the same value to bowl2$ .

 

Usage of variable-to-variable approach is recommended when you're planning some changes on string variable and don't want to loose original value.

When scripting in MMB, you don't have to worry about corrupting the original - just simply take another variable and perform changes on it.

To merge strings and put them into one string variable, use + operator.

 

For example, if definition of first string variable looks like this:

FirstPart$='To be - or not to be...'

And definition of second string variable is:

SecondPart$='the question is now.'

Merging of these strings into one looks like this:

CompleteSentence$=FirstPart$+SecondPart$

Merged strings are contents of CompleteSentence$:

 

To be - or not to be...the question is now

The same goal would be achieved by directly assigning text (but it's fixed then, can't be changed) to CompleteSentence$ :

CompleteSentence$= 'To be - or not to be...' + 'the question is now.'

 

Here are most common combinations of string variable assignments:

 

Definition

Example

Text to variable: fixed (non-changable) text is being assigned to string variable.

name$='Johnny Be Good'

Variable to variable: value of one string variable is being assigned to another string variable (for example, to retrieve value of MMB's EditBox object).

UserPassword$=EditBoxInput$

Variable+Text (or vice versa) to variable: using + for appending, this combination 1) merges contents of source string variable and source (fixed) text, 2) assigns result to destination string variable

a$=UserName$+' is hungry'

 

Contents (or results) of right-side strings are assigned to string variable on the left and you can use given values wherever you want, for example to show 'em as message:

a$=UserName$+' is hungry'

Message("Guess what: ","a$")

These two code lines will

 

a) assign strings to UserName$, and

b) display it using MMB's message box

Here's an example of multiple code lines with append operations:

Player1$='Doug'

Player2$= 'Steve'

MatchPlayers$= Player1$+' vs '+ Player2$

Message("And now, match: ","MatchPlayers$")

String displayed in MMB's message box would be:

And now, match: Doug vs Steve

 

String variables as object labels

 

 

Flexibility of MMB enables using content of string variable as a source for object labels, used for various script commands:

StringVarLabel

As an image above shows, object label "MyScript" is assigned to string variable (label$).

Once label is there, every script command that refers to some object label can use string variable instead of (fixed) label. So first you assign object label to a string variable:

ObjectLabel$= 'MyScript'

 

...and then execute script command that requires object label. For example, RunScript command uses one parameter - script label. Here we set string variable instead of fixed script label:

 

RunScript("ObjectLabel$")

 

This feature is very useful when objects have uniformed labels (Text1, Text2, Text3...) and performed commands should refer to variable labels - usually in for..next loops.

 

Examples mentioned above show you some uses of string variables. There are numerous purposes & combinations, so we leave you here to experiment and enjoy the power of strings!