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...