"Case" Question

S

Shapper

Hello,

I have a "Select Case MyVar" in which I define the values of an Array
according to the value of MyVar.

I need to use the Array Values in a Loop after End Select.
It seems the Array is deleted on End Select.

How can I make it available for my loop?

Thanks,
Miguel
 
C

Craig Deelsnyder

Shapper said:
Hello,

I have a "Select Case MyVar" in which I define the values of an Array
according to the value of MyVar.

I need to use the Array Values in a Loop after End Select.
It seems the Array is deleted on End Select.

How can I make it available for my loop?

Thanks,
Miguel

Can't really tell without seeing the code, will only be speculating on
an answer. Please post sample of Select Case and where the array is
declared if this doesn't solve it...but if the array's declared inside
the Select Case, it will only be available there, according to scoping
rules. If that's the problem, move the declaration out of the Select
Case (and For loop).
 
K

Kevin Spencer

Deine the array variable outside (previous to) the Select Case statment. If
you need the array after the function exits, define it outside the function.

BTW, declaring variables "on the fly" is a generally bad idea. Putting their
declarations at the top of a class definition, or function definition makes
them easier to find when you come back to the code later, not to mention
making their scope clear.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
S

Shapper

Hello,

Basically I have this:

Sub Build_Menu()

Select Case Session("culture")
Case "pt-PT"
Dim topMenuItems() As String = {"A", "B", "C", "", "D", "E"}
Case "en-GB"
Dim topMenuItems() As String = {"A", "B", "C", "", "D", "E"}
End Select

' Here I have some loops and other code which builds the menu.
Moving all this code inside each case makes no sense.
With other languages I am used to this makes sense:
Use case or if to set different versions of a string.
After it use it inside my Loop.

End Sub

How can I make this work?

Thanks,
Miguel
 
K

Kevin Spencer

You can make it work by declaring your array variable before you start the
loop.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
K

Kevin Spencer

Correction: You can make it available by declaring it before the Select Case
Statement.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
C

Craig Deelsnyder

Shapper said:
Hello,

Basically I have this:

Sub Build_Menu()

Select Case Session("culture")
Case "pt-PT"
Dim topMenuItems() As String = {"A", "B", "C", "", "D", "E"}
Case "en-GB"
Dim topMenuItems() As String = {"A", "B", "C", "", "D", "E"}
End Select

' Here I have some loops and other code which builds the menu.
Moving all this code inside each case makes no sense.
With other languages I am used to this makes sense:
Use case or if to set different versions of a string.
After it use it inside my Loop.

End Sub

How can I make this work?

Thanks,
Miguel

What Kevin was getting at:

Sub Build_Menu()
Dim topMenuItems() As String

Select Case Session("culture")
Case "pt-PT"
topMenuItems = {"A", "B", "C", "", "D", "E"}
Case "en-GB"
topMenuItems = {"A", "B", "C", "", "D", "E"}
End Select

' Here I have some loops and other code which builds the menu.
Moving all this code inside each case makes no sense.
With other languages I am used to this makes sense:
Use case or if to set different versions of a string.
After it use it inside my Loop.

End Sub

This is scoping; the variable is visible at the level it is declared and
below (inside loops, Select Case, etc.). Before you had declared it
inside a Case statement, meaning it was only created for that Case
statement and 'destroyed' when you exited that particular Case statement

Kevin's comment applies, most of the time you put declarations at the
top of your method/property, etc. I however deviate a little from that
in that if I have a 'large' nested piece of code with a sizable amount
of variables exclusive to it (e.g. if the Case statement was large), I
would declare the variables for that Case statement inside of that Case
statement if possible. Why? Because then when the code exits that
level, the variables are released and GAC can reclaim them quicker. But
you have to be careful to not abuse this and get back to 'scattering'
variables all over the place. Just keep that in mind for when you've
been doing this awhile :) For now follow Kevin's advice...
 
K

Kevin Spencer

Hi Craig,

I do, from time to time, do the same thing. My remark, if I remember
correctly, said "generally." For example, if I need a throwaway looping
variable, I will often decare it in the loop initializer. On the other
hand, if I have a rather long method, I will often declare throwaway
variables at the top of the method so I can reuse them and save a bit of
memory. Not that it makes that much difference any more when you're talking
about 32 bits here and there, but I just hate to be wasteful!

--

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,259
Latest member
JorjaBurne

Latest Threads

Top