Rhinoscript:variables
From ksteinfeWiki
Contents |
Overview: Variables
Variables are just that – placeholders for information which varies. They are containers that we can access and modify by referring to them by using a text-name piece of code, something like a label. It's important to remember the difference between the the label we give the variable, and what the variable contains - the difference between the container and the contained.
Variables are useful little buggers. Any time we wish to store information for use later in a script, we use a variable. For example, if we want to remember the results of a calculation or the choice that a user makes, and use it later in our script.
the 'Dim' identifier
To create a variable in rhinoscript, we use the keyword Dim, as shown below.
Dim myVariable
This line of code simply creates a new container for information, and labels this container "myVariable". It's like going down to the store and buying a little jar, and labeling the jar with your handy label maker (the kind that spits out the black sticky-backed labels). You can put whatever you want to in this jar... but notice, if we just used the code above, we haven't got anything in there yet. The variable is empty until we stick something in there.
myVariable = 100
There we go, now our variable has got something in it. Leaving a variable empty is dangerous business - if you try and access an empty variable, you might get an error or some unexpected behavior. Trying to store something in a variable which doesn't exist yet is also dangerous business. Remember the code below from our pseudocode example, a few sections back? One of the lines of code read as below:
somenumber = Rhino.GetReal("Line coordinate")
Well, this wasn’t entirely correct, and we're lucky we didn't get an error. Never ever do this again. From now on, always create (Dim) your variable before you put something inside.
Dim dblSomeNumber
dblSomeNumber = Rhino.GetReal("Line coordinate")
rules for naming your variables
So, there you are, now you can create and modify variables all day long. But with this new found freedom comes new found responsibility: First, you should always Dim your variables before you use them. Second, there are certain guidelines you should follow when naming your variables.
explicit rules
These are naming conventions enforced by the rhinoscript language itself. If you break one of these rules, you'll get an error.
- Variables must not contain space characters " "
[ERROR]
dim my variable
dim this is another one
[TIP]
''use the underscore character "_" instead..
[CORRECT]
dim my_variable dim this_is_another_one
- Variables must not start with a numerical digit (0,1,2,3,4,5,6,7,8,9)
[ERRORS]
dim 12monkeys
dim 2001_a_space_odyssey
[TIP]
''but you can use digits after the first character of the variable's name...
[CORRECT]
dim apollo13
dim friday_the_13th
- Variables must not contain any symbol used by the language
[ERROR]
dim tango+cash
dim sub-traction
[TIP]
''in general avoid using symbols in variables' names except for underscore
- Variables must not start with a symbol (!@#%^&*<>;_.)
[ERROR]
dim _not_valid_
dim @stake
- Variables must not contain a keyword (i.e. a word used by the language). These include: dim, sub, function, for, next, while, until, do, call, to, redim, end, if, then, is, as
[ERROR]
dim dim
dim function
implicit rules
These are naming conventions not actually enforced by the rhinoscript language, but are simply good practice. If you break one of these rules, you'll won't get an error, but you run the risk of getting lynched by a mob of nerds.
- In general try giving meaningful names
dim polygon_index
dim old_vertex
dim new_vertex
- But not too descriptive
dim the_third_day_of_the_month
dim once_upon_a_time_blah_blah_blah
- Avoid capital characters
dim PaInFuL
dim PEOPLE_KICK_ME_OUT_OF_CHAT_ROOMS
- Camel case, however, is encouraged
dim FirstVariable
dim secondVariable
- Use prefixes to show what type of thing you plan on storing in the variable. See Data Types for a complete list of examples.
dim blnSuccess
dim dblPercentage
dim strUsername
Note: Some content for this page lifted shamelessly from Stylianos Dritsas. Thanks, Stelios!
