Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a variable to represent a component name

Posted on 2004-08-28
9
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
9 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

618 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