?
Solved

Create Dynamic variables in VB.Net

Posted on 2007-12-06
4
Medium Priority
?
8,323 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:jcoehoorn
jcoehoorn 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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