asp.net:
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
[quoted text, click to view] Shapper wrote: > 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). -- Craig Deelsnyder
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. [quoted text, click to view] "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message news:euDESvLYFHA.1404@TK2MSFTNGP09.phx.gbl... > 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 >
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. [quoted text, click to view] "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message news:%23WAPX8RYFHA.3032@TK2MSFTNGP10.phx.gbl... > 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 > > "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message > news:kevin@DIESPAMMERSDIEtakempis.com: > >> 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. >> >> >> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message >> news:euDESvLYFHA.1404@TK2MSFTNGP09.phx.gbl... >> >> > 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 >> > >
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. [quoted text, click to view] "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message news:%23WAPX8RYFHA.3032@TK2MSFTNGP10.phx.gbl... > 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 > > "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message > news:kevin@DIESPAMMERSDIEtakempis.com: > >> 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. >> >> >> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message >> news:euDESvLYFHA.1404@TK2MSFTNGP09.phx.gbl... >> >> > 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 >> > >
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 [quoted text, click to view] "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message news:kevin@DIESPAMMERSDIEtakempis.com: > 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. > > > "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message > news:euDESvLYFHA.1404@TK2MSFTNGP09.phx.gbl... > > > 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 > >
[quoted text, click to view] Shapper wrote: > 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... -- Craig Deelsnyder
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. [quoted text, click to view] "Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message news:%23E$%23jKVYFHA.3488@tk2msftngp13.phx.gbl... > Shapper wrote: >> 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... > > -- > Craig Deelsnyder > Microsoft MVP - ASP/ASP.NET
Don't see what you're looking for? Try a search.
|