Problem using variables outside of IF statement

jimmyh01

New member
Joined
Sep 3, 2007
Messages
4
Programming Experience
Beginner
Hi Guys,

Firstly, as it's my first post, a big hello!

I'm having problems using variables outside of an IF statement in a sub. Although I have declared them, VS tells me that I havn't when referencing them, code as follows:

2004442433804417450_rs.jpg


I have tried declaring them outside of the IF statement but then the arrays don't work...I recieve a error about number of indicies being less than number of dimensions.

I am a beginner in the world of VB.net so this is probably an easy one. Thanks in advance for your response.

James
 
Last edited:
Remove the brackets from "Entity()=split...", to "Entity=split...". You either assign a value to one of the array items entity(3) = "hello array" or assign a new array to the variable entity=[new array].
 
it's not going to work because if it doesn't pass through the IF statement at runtime, it's going to hit the variable undeclared. If you declare a varaible within a conditional loop, it's only within scope whilst it is in that loop.

you would need to assign the declare and instantiate the variable before the loop, and then assign the variable in the loop.
 
That is three different operations; declaring the variable, creating an instance (value), assigning the instance (value) to the variable. To use a variable in any block it has to be declared in the same block or higher level, it has to be "within scope", the scope can be a local code block (for example between If...End If), at method level or at class level.
 
I am a beginner in the world of VB.net so this is probably an easy one. Thanks in advance for your response.

James

K, heres the easy intro about SCOPE:

Look at your Dim statement
Look at the indent level (how many times its tabbed over from the left)
Look down the code for the first line that is LESS INDENTED than your Dim
That's the point where that variable vanishes, gone, dead, vaporized. You CANNOT use it after that

In your code, the vanishing point would be the End If because the End If is less indented than Dim Period() as....

You CANNOT then, later (in the next if block), try to use Period again. It's dead already
 
Also, avoid using Microsoft.VisualBasic functions where possible. The modern equivalent of Left() is SubString:

Dim s as String = "@hello"
If "@".Equals(s.SubString(0, 1)) Then


Two concepts here:
First we call .Equals on a constant string - this ensures it will never throw a NullReferenceException. In this case, if s was Nothing then we's still get it because of the call to s.SubString() but in other cases, it's better to call the .Equals on a constant.
Using .Equals is apparently faster then using the = sign
SubString starting at index 0 for a length of 1 character is the new way. Note that strings start at 0 now, not 1

There are other options available to you that make more readable code:

Dim s as String = "@hello"
If s IsNot Nothing AndAlso s.StartsWith("@") Then ...
 
Back
Top