Solved

Using a variable to represent a component name

Posted on 2004-08-28
9
190 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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