Solved

Using a variable to represent a component name

Posted on 2004-08-28
9
187 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now