Newbie: Dynamic Variable Names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JBuckland
    New Member
    • Feb 2008
    • 7

    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:

    Code:
     For i As Integer = 1 To Len(CStr(Number)) Step 1  
    Dim MidString As String              
    MidString = CStr(Number)              
    [B][U]GetValues("VisPPL", Mid(MidString, (Len(MidString) - i + 1), 1), i)[/U][/B]         
    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:

    Code:
    Public Sub GetValues(ByVal VarName As [B][U]????[/U][/B], ByVal Number As Integer, ByVal Row As Integer)   
               
    Select Case Number 
                 
    Case 0                  
    VarName(Row, 1) = True                  
    VarName(Row, 2) = True                 
    VarName(Row, 3) = True                  
    VarName(Row, 4) = False                  
    VarName(Row, 5) = True                  
    VarName(Row, 6) = True                  
    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
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by JBuckland
    ...
    Code:
    Public Sub GetValues(ByVal VarName As [B][U]????[/U][/B], ByVal Number As Integer, ByVal Row As Integer)   
               
    Select Case Number 
                 
    Case 0                  
    VarName(Row, 1) = True                  
    VarName(Row, 2) = True                 
    VarName(Row, 3) = True                  
    VarName(Row, 4) = False                  
    VarName(Row, 5) = True                  
    VarName(Row, 6) = True                  
    VarName(Row, 7) = True
    Use ByRef, instead of ByVal, i'd say something like this will do

    [CODE=vb]Public Sub GetValues(ByRef VarName, ByVal Number As Integer, ByVal Row As Integer)
    dim i as integer
    select case number
    case 0
    for i = 1 to 7
    varname(row,i)= true
    next
    varname(row,4)= false
    case 1
    ....[/CODE]

    This way, the variable will be called VarName inside the sub, but as you change VarName, you'll be changing the reference too.

    HTH

    Comment

    • JBuckland
      New Member
      • Feb 2008
      • 7

      #3
      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:
      Code:
              ltrTop1.Visible = Segs(2, 3, 1)
              ltrTopL1.Visible = Segs(2, 3, 2)
              ltrTopR1.Visible = Segs(2, 3, 3)
              ltrMid1.Visible = Segs(2, 3, 4)
              ltrBotL1.Visible = Segs(2, 3, 5)
              ltrBotR1.Visible = Segs(2, 3, 6)
              ltrBot1.Visible = Segs(2, 3, 7)
      
              ltrTop2.Visible = Segs(2, 2, 1)
              ltrTopL2.Visible = Segs(2, 2, 2)
              ltrTopR2.Visible = Segs(2, 2, 3)
              ltrMid2.Visible = Segs(2, 2, 4)
              ltrBotL2.Visible = Segs(2, 2, 5)
              ltrBotR2.Visible = Segs(2, 2, 6)
              ltrBot2.Visible = Segs(2, 2, 7)
      
              ltrTop3.Visible = Segs(2, 1, 1)
              ltrTopL3.Visible = Segs(2, 1, 2)
              ltrTopR3.Visible = Segs(2, 1, 3)
              ltrMid3.Visible = Segs(2, 1, 4)
              ltrBotL3.Visible = Segs(2, 1, 5)
              ltrBotR3.Visible = Segs(2, 1, 6)
              ltrBot3.Visible = Segs(2, 1, 7)

      I was hoping to change this to something more like

      Code:
      For i As integer = 1 to 3 Step 1
      
              ltrTop"i".Visible = Segs(2, (4 - "i"), 1)
              ltrTopL"i".Visible = Segs(2, (4 - "i"), 2)
              ltrTopR"i".Visible = Segs(2, (4 - "i"), 3)
              ltrMid"i".Visible = Segs(2, (4 - "i"), 4)
              ltrBotL"i".Visible = Segs(2, (4 - "i"), 5)
              ltrBotR"i".Visible = Segs(2, (4 - "i"), 6)
              ltrBot"i".Visible = Segs(2, (4 - "i"), 7)
      
      Next

      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

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by JBuckland
        ...
        I was hoping to change this to something more like

        Code:
        For i As integer = 1 to 3 Step 1
        
                ltrTop"i".Visible = Segs(2, (4 - "i"), 1)
                ltrTopL"i".Visible = Segs(2, (4 - "i"), 2)
                ltrTopR"i".Visible = Segs(2, (4 - "i"), 3)
                ltrMid"i".Visible = Segs(2, (4 - "i"), 4)
                ltrBotL"i".Visible = Segs(2, (4 - "i"), 5)
                ltrBotR"i".Visible = Segs(2, (4 - "i"), 6)
                ltrBot"i".Visible = Segs(2, (4 - "i"), 7)
        
        Next
        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:

        [CODE=vb]dim i as integer
        For i = 1 to 3
        form1.controls( "ltrTop" & i).visible = segs(2, 4-i , 1)
        form1.controls( "ltrTopL" & i).visible = segs(2, 4-i,2)
        '... and so on
        next[/CODE]

        HTH

        Comment

        • JBuckland
          New Member
          • Feb 2008
          • 7

          #5
          Thanks, I have it set up like this but im still getting an error:

          Code:
          For i3 As Integer = 1 To 5 Step 1
                      Me.Controls("salTop" & i3).Visible = Segs(3, (6 - i3), 1)
                      Me.Controls("salTopL" & i3).Visible = Segs(3, (6 - i3), 2)
                      Me.Controls("salTopR" & i3).Visible = Segs(3, (6 - i3), 3)
                      Me.Controls("salMid" & i3).Visible = Segs(3, (6 - i3), 4)
                      Me.Controls("salBotL" & i3).Visible = Segs(3, (6 - i3), 5)
                      Me.Controls("salBotR" & i3).Visible = Segs(3, (6 - i3), 6)
                      Me.Controls("salBot" & i3).Visible = Segs(3, (6 - i3), 7)
                  Next i3
          
                  For i2 As Integer = 1 To 5 Step 1
                      Me.Controls("ltrTop" & i2).Visible = Segs(2, (4 - i2), 1)
                      Me.Controls("ltrTopL" & i2).Visible = Segs(2, (4 - i2), 2)
                      Me.Controls("ltrTopR" & i2).Visible = Segs(2, (4 - i2), 3)
                      Me.Controls("ltrMid" & i2).Visible = Segs(2, (4 - i2), 4)
                      Me.Controls("ltrBotL" & i2).Visible = Segs(2, (4 - i2), 5)
                      Me.Controls("ltrBotR" & i2).Visible = Segs(2, (4 - i2), 6)
                      Me.Controls("ltrBot" & i2).Visible = Segs(2, (4 - i2), 7)
                  Next i2
          
          
                  For i As Integer = 1 To 5 Step 1
                      Me.Controls("pplTop" & i).Visible = Segs(1, (5 - i), 1)
                      Me.Controls("pplTopL" & i).Visible = Segs(1, (5 - i), 2)
                      Me.Controls("pplTopR" & i).Visible = Segs(1, (5 - i), 3)
                      Me.Controls("pplMid" & i).Visible = Segs(1, (5 - i), 4)
                      Me.Controls("pplBotL" & i).Visible = Segs(1, (5 - i), 5)
                      Me.Controls("pplBotR" & i).Visible = Segs(1, (5 - i), 6)
                      Me.Controls("pplBot" & i).Visible = Segs(1, (5 - i), 7)
                  Next i
          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

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by JBuckland
            Thanks, I have it set up like this but im still getting an error:
            ...
            The error can be seen here: https://<br /> http://www.jamesbuck...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.

            Comment

            • JBuckland
              New Member
              • Feb 2008
              • 7

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

              Comment

              • kadghar
                Recognized Expert Top Contributor
                • Apr 2007
                • 1302

                #8
                Originally posted by JBuckland
                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)

                Comment

                • JBuckland
                  New Member
                  • Feb 2008
                  • 7

                  #9
                  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

                  Comment

                  • kadghar
                    Recognized Expert Top Contributor
                    • Apr 2007
                    • 1302

                    #10
                    Originally posted by JBuckland
                    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?

                    Comment

                    • kadghar
                      Recognized Expert Top Contributor
                      • Apr 2007
                      • 1302

                      #11
                      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':
                      [CODE=vb]me.frame1.contr ols("something" & i).visible = true.[/CODE]

                      Comment

                      • JBuckland
                        New Member
                        • Feb 2008
                        • 7

                        #12
                        They all seem to have values:


                        Thanks, James

                        Comment

                        • JBuckland
                          New Member
                          • Feb 2008
                          • 7

                          #13
                          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

                          Comment

                          • kadghar
                            Recognized Expert Top Contributor
                            • Apr 2007
                            • 1302

                            #14
                            Originally posted by JBuckland
                            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.

                            Comment

                            Working...