473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Using a variable to build the name of a variable for assignment

Ok, the subject line of this is pretty confusing, I'll admit.

I'm new to dotnet and VB. I've used this kind of construction in Perl a
zillion times over the years.

What I've got is something like this (not my entire code):

<MYCODE>
Sub setVisible(Stage As Integer)

Select Case Stage
Case 2

Panel1.visible = false
Panel2.visible = true
Panel3.visible = false

Case 3

Panel2.visible = false
Panel3.visible = true
Panel4.visible = false

Case 4

Panel3.visible = false
Panel4.visible = true
Panel5.visible = false

Case 5

Panel4.visible = false
Panel5.visible = true
Panel6.visible = false

End Select

End Sub
</MYCODE>

What I'd like is something like this:

<MYBETTERCODE>
Sub setVisible(Stage As Integer)
'If there were an eval, I might do this
eval("Panel" & Stage - 1 & ".visible") = false
eval("Panel" & Stage & ".visible") = true
eval("Panel" & Stage + 1 & ".visible") = false
End Sub
</MYBETTERCODE>

Is there a way to do this in dotnet VB?

Thanks!


Nov 20 '05 #1
2 1124
* Bruce Dumes <bd****@college.ucla.edu> scripsit:
I'm new to dotnet and VB. I've used this kind of construction in Perl a
zillion times over the years.

What I've got is something like this (not my entire code):

<MYCODE>
Sub setVisible(Stage As Integer)

Select Case Stage
Case 2

Panel1.visible = false
Panel2.visible = true
Panel3.visible = false

Case 3

Panel2.visible = false
Panel3.visible = true
Panel4.visible = false

Case 4

Panel3.visible = false
Panel4.visible = true
Panel5.visible = false

Case 5

Panel4.visible = false
Panel5.visible = true
Panel6.visible = false

End Select

End Sub
</MYCODE>

What I'd like is something like this:

<MYBETTERCODE>
Sub setVisible(Stage As Integer)
'If there were an eval, I might do this
eval("Panel" & Stage - 1 & ".visible") = false
eval("Panel" & Stage & ".visible") = true
eval("Panel" & Stage + 1 & ".visible") = false
End Sub
</MYBETTERCODE>


\\\
Private m_MyPanels() As Panel
..
..
..
m_MyPanels = New Panel() {Panel1, Panel2, ..., Paneln}
..
..
..
Private Sub SetVisible(ByVal Stage As Integer)
m_MyPanels(Stage - 1).Visible = False
...
End Sub
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2
Bruce Dumes wrote:
<MYBETTERCODE>
Sub setVisible(Stage As Integer)
'If there were an eval, I might do this
eval("Panel" & Stage - 1 & ".visible") = false
eval("Panel" & Stage & ".visible") = true
eval("Panel" & Stage + 1 & ".visible") = false
End Sub
</MYBETTERCODE>

Is there a way to do this in dotnet VB?


Unfortunately no. You could theoretically do something like it using
reflection, but the code would become incredibly complex and convoluted, and
slower as well.

--
Sven Groot

http://unforgiven.bloghorn.com

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jens Thiede | last post by:
I found the querk in my code: a = ; b = a; b.append(4); b == ; # As it should. a == ; # - Why? One would think that b is a referance to a - however I know it's not.
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.