473,386 Members | 1,708 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,386 software developers and data experts.

Newbie: Dynamic Variable Names

Im trying to create a 7 segment display for a project in visual basic 2005 express edition. I have a 2 dimentional boolean which stores the values of each of the segments for a certain display. I have written a case statement for the first display and would like to use it for the other displays instead of having to copy the code. I have tried everything I can think of but can't get it to pass the name of the boolean array im trying to set the values to from my main code to the procedure. Here is the code im passing:

Expand|Select|Wrap|Line Numbers
  1.  For i As Integer = 1 To Len(CStr(Number)) Step 1  
  2. Dim MidString As String              
  3. MidString = CStr(Number)              
  4. GetValues("VisPPL", Mid(MidString, (Len(MidString) - i + 1), 1), i)         
  5. Next i
The "VisPPL" is the name of the boolean array im trying to set the values to.

Here is the code from the start of my procedure my procedure:

Expand|Select|Wrap|Line Numbers
  1. Public Sub GetValues(ByVal VarName As ????, ByVal Number As Integer, ByVal Row As Integer)   
  2.  
  3. Select Case Number 
  4.  
  5. Case 0                  
  6. VarName(Row, 1) = True                  
  7. VarName(Row, 2) = True                 
  8. VarName(Row, 3) = True                  
  9. VarName(Row, 4) = False                  
  10. VarName(Row, 5) = True                  
  11. VarName(Row, 6) = True                  
  12. VarName(Row, 7) = True

Ive tried using a string where the ???? is as the datatype for the name of the boolean array and im simply trying to get the VarName to be replaced with my boolean name (VisPPL) which im passing to it so I can pass different boolean arrays to the same procedure and it will set the values to that array.

Im not sure if I have explained my problem very well but any help is greatly appreciated.

Thanks, James
Feb 15 '08 #1
13 2322
kadghar
1,295 Expert 1GB
...
Expand|Select|Wrap|Line Numbers
  1. Public Sub GetValues(ByVal VarName As ????, ByVal Number As Integer, ByVal Row As Integer)   
  2.  
  3. Select Case Number 
  4.  
  5. Case 0                  
  6. VarName(Row, 1) = True                  
  7. VarName(Row, 2) = True                 
  8. VarName(Row, 3) = True                  
  9. VarName(Row, 4) = False                  
  10. VarName(Row, 5) = True                  
  11. VarName(Row, 6) = True                  
  12. VarName(Row, 7) = True
Use ByRef, instead of ByVal, i'd say something like this will do

Expand|Select|Wrap|Line Numbers
  1. Public Sub GetValues(ByRef VarName, ByVal Number As Integer, ByVal Row As Integer)   
  2. dim i as integer       
  3. select case number 
  4. case 0
  5.     for i = 1 to 7
  6.         varname(row,i)=true
  7.     next
  8.     varname(row,4)=false
  9. case 1
  10. ....
This way, the variable will be called VarName inside the sub, but as you change VarName, you'll be changing the reference too.

HTH
Feb 15 '08 #2
Thanks that worked great. I have another question, at the moment I have to declare the values of each of my segments in reference to the array in the follow way:
Expand|Select|Wrap|Line Numbers
  1.         ltrTop1.Visible = Segs(2, 3, 1)
  2.         ltrTopL1.Visible = Segs(2, 3, 2)
  3.         ltrTopR1.Visible = Segs(2, 3, 3)
  4.         ltrMid1.Visible = Segs(2, 3, 4)
  5.         ltrBotL1.Visible = Segs(2, 3, 5)
  6.         ltrBotR1.Visible = Segs(2, 3, 6)
  7.         ltrBot1.Visible = Segs(2, 3, 7)
  8.  
  9.         ltrTop2.Visible = Segs(2, 2, 1)
  10.         ltrTopL2.Visible = Segs(2, 2, 2)
  11.         ltrTopR2.Visible = Segs(2, 2, 3)
  12.         ltrMid2.Visible = Segs(2, 2, 4)
  13.         ltrBotL2.Visible = Segs(2, 2, 5)
  14.         ltrBotR2.Visible = Segs(2, 2, 6)
  15.         ltrBot2.Visible = Segs(2, 2, 7)
  16.  
  17.         ltrTop3.Visible = Segs(2, 1, 1)
  18.         ltrTopL3.Visible = Segs(2, 1, 2)
  19.         ltrTopR3.Visible = Segs(2, 1, 3)
  20.         ltrMid3.Visible = Segs(2, 1, 4)
  21.         ltrBotL3.Visible = Segs(2, 1, 5)
  22.         ltrBotR3.Visible = Segs(2, 1, 6)
  23.         ltrBot3.Visible = Segs(2, 1, 7)
  24.  

I was hoping to change this to something more like

Expand|Select|Wrap|Line Numbers
  1.  
  2. For i As integer = 1 to 3 Step 1
  3.  
  4.         ltrTop"i".Visible = Segs(2, (4 - "i"), 1)
  5.         ltrTopL"i".Visible = Segs(2, (4 - "i"), 2)
  6.         ltrTopR"i".Visible = Segs(2, (4 - "i"), 3)
  7.         ltrMid"i".Visible = Segs(2, (4 - "i"), 4)
  8.         ltrBotL"i".Visible = Segs(2, (4 - "i"), 5)
  9.         ltrBotR"i".Visible = Segs(2, (4 - "i"), 6)
  10.         ltrBot"i".Visible = Segs(2, (4 - "i"), 7)
  11.  
  12. Next
  13.  
  14.  

But, I cannot get it to work. Once again thanks for all the help its greatly appreciated. I have been racking my brains on how to tackle this 7 segment display and I just feel there should be a simpler way to go about things but I cannot think of one, it doesn't help that i'm new to visual basic with a background in PHP which i thought would help but its proving not so.

Thanks, James
Feb 15 '08 #3
kadghar
1,295 Expert 1GB
...
I was hoping to change this to something more like

Expand|Select|Wrap|Line Numbers
  1.  
  2. For i As integer = 1 to 3 Step 1
  3.  
  4.         ltrTop"i".Visible = Segs(2, (4 - "i"), 1)
  5.         ltrTopL"i".Visible = Segs(2, (4 - "i"), 2)
  6.         ltrTopR"i".Visible = Segs(2, (4 - "i"), 3)
  7.         ltrMid"i".Visible = Segs(2, (4 - "i"), 4)
  8.         ltrBotL"i".Visible = Segs(2, (4 - "i"), 5)
  9.         ltrBotR"i".Visible = Segs(2, (4 - "i"), 6)
  10.         ltrBot"i".Visible = Segs(2, (4 - "i"), 7)
  11.  
  12. Next
  13.  
  14.  
Thanks, James
No, in VB you cannot do that with the names of the objects. But if they're controls in a Form, you can call them from the controls collection, using their name as a string.

Lets say you have your form called Form1, then something like this will do:

Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. For i = 1 to 3
  3.     form1.controls("ltrTop" & i).visible = segs(2, 4-i , 1)
  4.     form1.controls("ltrTopL" & i).visible = segs(2, 4-i,2)
  5.     '... and so on
  6. next
HTH
Feb 15 '08 #4
Thanks, I have it set up like this but im still getting an error:

Expand|Select|Wrap|Line Numbers
  1. For i3 As Integer = 1 To 5 Step 1
  2.             Me.Controls("salTop" & i3).Visible = Segs(3, (6 - i3), 1)
  3.             Me.Controls("salTopL" & i3).Visible = Segs(3, (6 - i3), 2)
  4.             Me.Controls("salTopR" & i3).Visible = Segs(3, (6 - i3), 3)
  5.             Me.Controls("salMid" & i3).Visible = Segs(3, (6 - i3), 4)
  6.             Me.Controls("salBotL" & i3).Visible = Segs(3, (6 - i3), 5)
  7.             Me.Controls("salBotR" & i3).Visible = Segs(3, (6 - i3), 6)
  8.             Me.Controls("salBot" & i3).Visible = Segs(3, (6 - i3), 7)
  9.         Next i3
  10.  
  11.         For i2 As Integer = 1 To 5 Step 1
  12.             Me.Controls("ltrTop" & i2).Visible = Segs(2, (4 - i2), 1)
  13.             Me.Controls("ltrTopL" & i2).Visible = Segs(2, (4 - i2), 2)
  14.             Me.Controls("ltrTopR" & i2).Visible = Segs(2, (4 - i2), 3)
  15.             Me.Controls("ltrMid" & i2).Visible = Segs(2, (4 - i2), 4)
  16.             Me.Controls("ltrBotL" & i2).Visible = Segs(2, (4 - i2), 5)
  17.             Me.Controls("ltrBotR" & i2).Visible = Segs(2, (4 - i2), 6)
  18.             Me.Controls("ltrBot" & i2).Visible = Segs(2, (4 - i2), 7)
  19.         Next i2
  20.  
  21.  
  22.         For i As Integer = 1 To 5 Step 1
  23.             Me.Controls("pplTop" & i).Visible = Segs(1, (5 - i), 1)
  24.             Me.Controls("pplTopL" & i).Visible = Segs(1, (5 - i), 2)
  25.             Me.Controls("pplTopR" & i).Visible = Segs(1, (5 - i), 3)
  26.             Me.Controls("pplMid" & i).Visible = Segs(1, (5 - i), 4)
  27.             Me.Controls("pplBotL" & i).Visible = Segs(1, (5 - i), 5)
  28.             Me.Controls("pplBotR" & i).Visible = Segs(1, (5 - i), 6)
  29.             Me.Controls("pplBot" & i).Visible = Segs(1, (5 - i), 7)
  30.         Next i
  31.  
The error can be seen here: http://www.jamesbuckland.com/garage/error.jpg

The controls im changing are image boxes is this why it will not work? Sorry for all the questions and thanks for all the help.

Thanks, James
Feb 15 '08 #5
kadghar
1,295 Expert 1GB
Thanks, I have it set up like this but im still getting an error:
...
The error can be seen here: http://<br /> http://www.jamesbuckl...rage/error.jpg

Thanks, James
when this error occurs, pass the mouse over i3 (anyone) to see its current value at that moment.

its because saltop1 or saltop 2 or... does not exist, or because segs(3,5,1) or segs(3,4,1) or segs(3,3,1).... is not defined, or has not been set to a value or is not a boolean.

But that code you wrote, seems all right to me.
Feb 15 '08 #6
Only the first loop gets a value and the i3 is 1, ive looked over all the naming and it all looks fine. How can I give a value to every space in my array? Will this help?

Thanks, James
Feb 15 '08 #7
kadghar
1,295 Expert 1GB
Only the first loop gets a value and the i3 is 1, ive looked over all the naming and it all looks fine. How can I give a value to every space in my array? Will this help?

Thanks, James
They already have one, it's 'False' by default.

the only thing that comes to my mind is:

what kind of control is salTop1?? i mean, is it a command button, or a combo box, or what is it?.

Or pass the mouse over Segs (any of them) when the error occurs, and check its values. Check if Segs(3, 5, 1) has a boolean's value (true or false)
Feb 15 '08 #8
salTop1 etc. are Pictures Box's with an image of a segment in them. I can change them to something else if it will help.

Thanks, James
Feb 15 '08 #9
kadghar
1,295 Expert 1GB
salTop1 etc. are image box's with an image of a segment in them. I can change them to something else if it will help.

Thanks, James
No no, image boxes does have the visible property, ... and what about the boolean's value? is it set to True or False?
Feb 15 '08 #10
kadghar
1,295 Expert 1GB
other thing that comes to my mind is that maybe the image boxes have another parent, like a frame.

so you might need to put the complete 'path':
Expand|Select|Wrap|Line Numbers
  1. me.frame1.controls("something" & i).visible = true.
Feb 15 '08 #11
They all seem to have values:



Thanks, James
Feb 16 '08 #12
Thank you so much. My stupid mistake i had them in a group box for visual effect and once i added the name for the groups it worked perfectly. Thanks for all the help I really appreciate it and im sure I can only learn from my mistakes.

Thanks, James
Feb 16 '08 #13
kadghar
1,295 Expert 1GB
Thank you so much. My stupid mistake i had them in a group box for visual effect and once i added the name for the groups it worked perfectly. Thanks for all the help I really appreciate it and im sure I can only learn from my mistakes.

Thanks, James
L O L, dont be so rude with that poor mistake.

im glad it helped you.
Feb 16 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ken Halley | last post by:
How does one refer to a variable name using a variable within a variable name??? For example, I want to perform a SQL query and use the results of the query to determine which variable to assign a...
4
by: Jas | last post by:
Hi All I guess I should just have tried this to see if it works but I don't have access to a machine running IIS and can't test it and I am itching for an answer. I want to use dynamic...
9
by: firegun9 | last post by:
Hello all, After looking for the solution for a while in the group, I know this is not possible in C++. So I just say my problme here. The input file is: dev1 1 1 dev2 0 0 0 6 There is a...
4
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
3
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
5
by: Rolf Mander | last post by:
Hi, I need to use dynamic variable names but for objects. As you know something like that works fine: $variable="content"; $part="able"; echo ${"vari".$part}; // gives out content but i...
7
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
0
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
26
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.