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

x
?
Solved

Create Dynamic variables in VB.Net

Posted on 2007-12-06
4
Medium Priority
?
8,327 Views
Last Modified: 2013-11-26
Hello,
   I would like to be able to change this:
        Dim lbl1 as label
        Dim lbl2 as label
        Dim lbl3 as label

        lbl1.text = "Yes"
        lbl2.text = "Yes"
        lbl3.text = "Yes"

to a dynamic sub where I pass in 1, 2 or 3 and it create it in a loop. Any ideas?
0
Comment
Question by:mcdermon
[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
4 Comments
 
LVL 28

Assisted Solution

by:iboutchkine
iboutchkine earned 200 total points
ID: 20418894
Here is an example how to create control array and assign event to it

Private ButtonArray(9) As Button

Public Sub AddButtons()
    For x As Integer = 0 To 9
        ButtonArray(x) = New Button
        With ButtonArray(x)
            .Tag = x
            .Text = "Button " & x
            .Size = New Size(60,20)
            .Location = New Point(20, (x * 20) + 4)
            .Parent = Me
            .Visible = True
            AddHandler .Click, AddressOf ButtonHandler
        End With
    Next        
End Sub

'Then, create a ButtonHandler to handle the button clicks

Public Sub ButtonHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
    MsgBox("Button number " & CType(sender,Button).Tag & " was clicked")
End Sub

For the label you do not need handler, but I showed it for the general knowledge
0
 
LVL 4

Accepted Solution

by:
J1H1W1 earned 400 total points
ID: 20419082
Did you want something like this:
Dim WithEvents C As Control
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Static I As Integer
	I += 1
	C = GetNewLabel(I)
End Sub
Function GetNewLabel(ByVal LabelNumber As Integer) As Label
	GetNewLabel = New Label
	With GetNewLabel
		.Name = "Lbl" & LabelNumber.ToString
		.Parent = Me
		.Left = 0
		.Top = (LabelNumber) * (GetNewLabel.Height)
		.Text = "Yes"
	End With
End Function

Open in new window

0
 
LVL 18

Assisted Solution

by:Joel Coehoorn
Joel Coehoorn earned 200 total points
ID: 20419666
Private Labels As New List(Of Label)
Sub CreateLabels(ByVal LabelCount As Integer)
    Labels.Clear()
    Dim l As Label
    For i As Integer to LabelCount
        l = New Label
        l.Name = "Label" & i
        Labels.Add(l)
    Next i
End Sub
0
 
LVL 27

Assisted Solution

by:VBRocks
VBRocks earned 200 total points
ID: 20420522
Here's an alternative method, that you can call as follows:

        For Each lbl As Label In GetLabels(3)
            'Process...
            Debug.WriteLine(lbl.Name)

        Next


'And the function:
    Private Function GetLabels(ByVal HowMany As Int16) As Label()
        Dim newlbl As Label = Nothing
        Dim lbl() As Label = Nothing

        For i As Int16 = 0 To (HowMany - 1)
            ReDim Preserve lbl(i)

            'Configure label
            newlbl = New Label()
            newlbl.Name = "lbl" & i
            newlbl.Text = "Yes"
            newlbl.Visible = True

            lbl(i) = newlbl

        Next

        Return lbl

    End Function

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
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…

597 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