Solved

Create Dynamic variables in VB.Net

Posted on 2007-12-06
4
8,315 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 50 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 100 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 50 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 50 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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