Results 1 to 5 of 5

Thread: Variable from two strings

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    94

    Variable from two strings

    i have:
    Code:
    Public Class Form1
        Public tc1 = Color.Blue
        Public tc2 = Color.Green
        Public tc3 = Color.Orange
        Public tc4 = Color.Yellow
        Public tc5 = Color.Pink
        Public tc6 = Color.Red
        Public cSelNum As Integer
        Private Sub tcTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tcTimer.Tick
            Dim cSel
            If cSelNum = 6 Then
                cSelNum = 1
            Else
                cSelNum += 1
            End If
            cSel = "tc" & cSelNum.ToString
            Label1.ForeColor = cSel
        End Sub
    End Class
    this is supposed to make the text flash different colors. I'm trying to get a certain variable ex: tc6 by taking the current number and putting tc before it. This obviously doesn't work. How can i fix this?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Variable from two strings

    There would be ways to make what you are describing work, but they would be kind of painful. Would you be willing to just have an array of colors? If so, then you wouldn't need to have anything other than the integer portion, as that would be the index into the array.

    You might also consider that cycling through six colors may not be ideal. I have some code that cycles through every possible color in a kind of random pattern. The way I do it is to have three variables, one for Red, one for Green, and one for Blue. Every time the timer ticks, I add two small, but different, prime numbers to two of the three (I think I used 7 and 11), and subtract a different prime number from the third. When the value of any of the three colors gets over 255 or under 0, I flip the sign of the number that is being added to that color. If I was adding 7, I would begin adding -7. The larger the numbers used, the faster the colors change. It makes a pretty interesting effect, so I use it whenever I want a control to 'flash'. Rather than an annoying flash, I end up with a hypnotic effect.
    My usual boring signature: Nothing

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Variable from two strings

    Your code is not working because you are assigning a string "tc1" or so forth to the forecolor property instead of its value. What I suggest is that you use an array instead of 6 variables.

    Here's your same code adapted to use the array of colors

    vb.net Code:
    1. Dim tc(6) As Color
    2.     Dim cSelNum As Integer
    3.  
    4.     Private Sub tcTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tcTimer.Tick
    5.  
    6.         tc(1) = Color.Blue
    7.         tc(2) = Color.Green
    8.         tc(3) = Color.Orange
    9.         tc(4) = Color.Yellow
    10.         tc(5) = Color.Pink
    11.         tc(6) = Color.Red
    12.  
    13.         If cSelNum = 6 Then
    14.             cSelNum = 1
    15.         Else
    16.             cSelNum += 1
    17.         End If
    18.         Label1.ForeColor = tc(cSelNum)
    19.  
    20.     End Sub

    Never try to construct the names of the variables using code to later use them as such and expect to obtain their value.
    Last edited by kaliman79912; Oct 18th, 2010 at 01:12 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    94

    Re: Variable from two strings

    for all 6 colors, i get:
    EDIT: nevermind. fixed.
    thanks!

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Variable from two strings

    Make sure you only declare the array once. the rest of the lines (tc(1)=Color.Blue) etc. are not declarations, they are assigning values to the items in the array so do not put Dim keyword before each line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width