?
Solved

Using a variable to represent a component name

Posted on 2004-08-28
9
Medium Priority
?
210 Views
Last Modified: 2010-04-23
Hi All

My next complaint about vb.net and vb in general (im really not a fan)

anyway is it possible to make a variable name out of another variable

e.g.

i have a variable called section which equals MyText

Dim section As String = "mainText"

now i have some variables that i want to change depending on which section is clicked

mainTextStyle=style.Text
mainTextWeight=weight.Text

buuut, if i have a section called subText i want to use the same component boxes (style.Text and weight.Text) to put the data into the right variable

its easy to do with a case statement or if statement

if section = "mainText" then
mainTextStyle=style.Text
mainTextWeight=weight.Text

if section ="subText" then
subTextStyle=style.Text
subTextWeight=weight.Text

but i want to do it like this

section+Style=style.Text
section+Weight=weight.Text

or something along those lines where the variable i want to change is built up out of the value of section and then the prefix for the variable. Then i wont need a stupidly long case statement.

Any ideas? Im sure you can do this in mighty vb because u can even do it in flash 4

gregoryyoung: editted to remove profanity
0
Comment
Question by:harveyball
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 252 total points
ID: 11921426
The following shows how to find a control based upon its name:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c As Control
        Dim tb As TextBox

        c = getControl("mainText", "Style")
        If Not (c Is Nothing) Then
            ' do someting with c
            c.Text = "found it"

            ' if you need c converted to a textbox reference then...
            tb = CType(c, TextBox)
            tb.BackColor = Color.Blue
        End If
    End Sub

    Private Function getControl(ByVal section As String, ByVal style As String) As Control
        Dim c As Control
        Dim controlName As String = section & style

        For Each c In Me.Controls
            If c.Name.Equals(controlName) Then
                Return c
            End If
        Next
        MsgBox(controlName, MsgBoxStyle.Critical, "No Matching Control Found")
    End Function

Regards,

Idle_Mind
0
 

Author Comment

by:harveyball
ID: 11921617
thats nothing to do with what i asked

i want a dynamically named variable

ie

section="mainText"

so instead of this

mainTextCol=textCol.Text

i want to be able to do something like this

section+"Col"=textCol.Text

you do it in flash 4 that way, php uses a . or a $$ in many other languages u can do it in many different ways java and delphi can both do it, except vb doesnt seem to have a way of doing it (to my knowledge) the joys of microsoft stuff.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11921638
>> thats nothing to do with what i asked
The title of your question is "Using a variable to represent a component name" isn't it?

There is no way to get a variable based on a string.  The closest thing you can get is to use a HashTable to store key/value pairs.  Let me know if you want to see an example.

Idle_Mind
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:harveyball
ID: 11921647
i meant variable name as i say in the main body

vb sucks
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11921685
>> vb sucks

You are used to doing things a certain way.  

VB.Net is a robust language...perhaps if you explained how your app should work, the Experts here at EE can suggest a solution for you.

Idle_Mind
0
 
LVL 37

Assisted Solution

by:gregoryyoung
gregoryyoung earned 248 total points
ID: 11921967
there are reasons why this is not implemented in vb.net/C#/any .net languages and why it is in some others. Flash 4 and javascript I know support this, this is because they are interpretted languages and therefor have the variable name metadata available to them.

1) it would require variable names to be included in metadata of your executable in the case of local variables
2) it would require late binding

I would be intrested in seeing how delphi and java do this as you claim I bet it actually works very similar to the way vb.net does (especially java as I don't know too much about delphi).

there are other ways that this could be implemented through reflections though it can only work on members variables not on local variables if you are intrested I can discuss these with you ...

public class foo
    private bar as integer 'you can set this through reflections

    private sub bar2()
         dim blah as integer 'you can't set this through reflections
    end sub
end class

there are ways of doing this easily for read access to the variable DataBinder.Eval is one http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuidatabinderclassevaltopic.asp 

for write access however I would suggest a method similar to the one given, you would be using the metadata found in the name of the control I might make a slightly more generic function though ...

foir web controls this is implemented here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIControlClassFindControlTopic1.asp

   Private static Function FindControl(ByVal name As String, byval Controls as ControlCollection) As Control
        Dim c As Control

        For Each c In Controls
            If c.Name.Equals(controlName) Then
                Return c
            End If
            if c.HasControls then
                dim foo as Control = FindControl(name, c.Controls)
                if not foo is nothing
                      Return foo
                end if
            end if
        Next
        return nothing
    End Function

this will return you a handle to a control with the name of whatever and will work with any form, the form can contain containers etc and it will recurse through them until it finds your control. you would use it as Control c = FindControl("nameofcontrol", Form.Controls) you could then set anything you needed on the control.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question