Solved

Using a variable to represent a component name

Posted on 2004-08-28
9
193 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
9 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 63 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 85

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

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

vb sucks
0
 
LVL 85

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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