Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have declared some variables:

dim variable as integer
dim variable2 as integer
dim variable3 as integer

and i have created an array with positions:

sum1 'on index 0
sum2 'on index 1
sum3 'on index 2

The question is how to replace their names with values from an array?

or

How to create variables with names from an array?

Please help.

Improve:

I have a simple code generating dynamically some controls on the form:


VB
Imports System
Imports System.Windows

Public Class Form1

    Public WithEvents new_button As Button = New Button()
    Public new_label As Label = New Label()
    Public new_text_box As TextBox = New TextBox()

    Private Sub frmDoPakowania_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Dim on_top As Integer
        Dim on_top_tb As Integer
        Dim tb_value As Integer = 0
        on_top = 25
        on_top_tb = 21

        Dim counter As Integer = 2

        Try
            For d = 1 To counter
                new_label = New Label()
                new_text_box = New TextBox()
                new_button = New Button()
                new_label.Text = "Value:"
                new_label.Left = 13
                new_label.Top = on_top
                new_label.Width = 67
                new_label.Height = 13
                Me.Controls.Add(new_label)
                new_text_box.Font = New Font(Me.Font.FontFamily, 8.5, FontStyle.Regular)
                new_text_box.Text = tb_value
                new_text_box.Left = 80
                new_text_box.Top = on_top_tb
                new_text_box.Width = 35
                new_text_box.Height = 20
                new_text_box.ForeColor = Color.Black
                Me.Controls.Add(new_text_box)
                new_button.Text = "Raise up"
                new_button.Font = New Font(Me.Font.FontFamily, 7.75, FontStyle.Regular)
                new_button.Height = 20
                new_button.Left = 128
                new_button.Top = on_top_tb
                AddHandler new_button.Click, AddressOf Me.new_button_Click
                Me.Controls.Add(new_button)
                counter -= 1
                on_top += 23
                on_top_tb += 23
            Next
        Catch ex As Exception
        End Try

        RemoveHandler new_button.Click, AddressOf Me.new_button_Click

    End Sub

    Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

        Dim aa As Integer
        aa = new_text_box.Text
        aa = aa + 1
        new_text_box.Text = aa
    End Sub

End Class


I've published the same code in other question, so i'm sorry for duplicates.

The problem is that when I click on any button only value in bottom textbox is raising. As you know the target is to raise up value in text box on the left of button...

Don't suggest that there are only two lines, because variable counter can have different values, i.e. 20.

I don't know how to handle with this problem so I have many ideas, including this with variables, but I'm sure it will not work correctly even if I will do it... :(

can You help?
Posted
Updated 28-Jul-11 10:50am
v3

You can't - VB does not support pointers.
Instead, consider using a Dictionary:
Dim dict As New Dictionary(Of String, Integer)()
dict.Add("Variable1", 0)
dict.Add("Variable2", 1)
dict.Add("Variable3", 2)
Dim i As Integer = dict("Variable2") + dict("Variable3")
Console.WriteLine(i)



""thanks but You misunderstood me - surely becasue of names in array. :) i haven't in my mind sum as math function. :) there also can be positions like:
boat1
boat2
boat3


Ah! If I understand you you want to read strings from an array, and create a variable with the name of the value in the string? You can do that, using Reflection, but it is a cumbersome sledgehammer to crack what it probably a simple nut, because your variable names will not be available as compile time, so they will be as difficult to use as they are to create.

Why do you want to do that?



Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

     Dim aa As Integer
     aa = new_text_box.Text
     aa = aa + 1
     new_text_box.Text = aa
 End Sub



"I've published the same code in other question, so i'm sorry for duplicates.

The problem is that when I click on any button only value in bottom textbox is raising. As you know the target is to raise up value in text box on the left of button..."


Ah! That makes it easy!

You don't need to worry about names, you just need to get the correct instance. Fortunately, every Event passes you the object that raised the event as the sender parameter:
Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click

     Dim t As TextBox = TryCast(sender, TextBox)
     If t IsNot Nothing Then

        Dim aa As Integer
        aa = t.Text
        aa = aa + 1
        t.Text = aa

     End If
 End Sub


Edit - I just realized...

The event is a Button event, so it won't work quite like that.
Add a line of code to your frmDoPakowania_Shown function:
AddHandler new_button.Click, AddressOf Me.new_button_Click
new_button.Tag = new_text_box   '****** ADD THIS
Me.Controls.Add(new_button)
Then change the function to use a button, and it's Tag property:

Private Sub new_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles new_button.Click
     Dim b as Button = TryCast(sender, Button)
     If b IsNot Nothing Then

        Dim t As TextBox = TryCast(b.Tag, TextBox)
        If t IsNot Nothing Then
           Dim aa As Integer
           aa = t.Text
           aa = aa + 1
           t.Text = aa
        End If
     End If
 End Sub
The Tag property is something each control has, and is available for you to put an object into. In this case, each button is concerned with a specific TextBox, so we tell the button which box to refer to.


[edit]DOH! Replaced new_text_box.Text with t.Text - OriginalGriff[/edit]
 
Share this answer
 
v5
Comments
szataniel 28-Jul-11 9:45am    
thanks but You misunderstood me - surely becasue of names in array. :) i haven't in my mind sum as math function. :) there also can be positions like:

boat1
boat2
boat3
OriginalGriff 28-Jul-11 9:50am    
Answer updated
szataniel 28-Jul-11 10:43am    
may I send You my problem on PM or e-mail?
OriginalGriff 28-Jul-11 10:52am    
We don't have PM's here - thankfully!
And I don't give out my email too readily :laugh:
Use the "Improve question" widget to edit your question and post the relevant bits there - if you then reply to my message I will get an email to say so.
This means that everyone can see the problem, so if they have a better answer they can suggest it. Private messages mean that you may not get the best answer possible, because the number of people who have the info is limited.
szataniel 28-Jul-11 10:55am    
you are right. i'll do it on time.
You could do it by storing the name/object in some type of collection, and write code to retrieve the data into an object that you have written code for. But that still begs the quest: why?
 
Share this answer
 
Comments
szataniel 28-Jul-11 9:39am    
it's very difficult to answer your question. i should pastemy code so it will we more easier. but the main quation is how to create variables with names taken from array?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900