Object reference on the fly

Gopher2011

Well-known member
Joined
Mar 3, 2011
Messages
111
Location
South England
Programming Experience
10+
Does anyone know if I can create objects on the fly?
Currently I have

DI_01 is an object on my form. Microsoft.VisualBasicPowerpack.OvalShape

Digital_In.Add(DI_01)
...and so on...
Digital_In.Add(DI_16)

What I want to do is reference the object at runtime

Dim i As Integer
For i = 1 To 16
Digital_In.Add("DI_0" & i)
Next

Is this possible as my example does not work.

Goph
 
VB.NET:
For i As Integer = 1 To 16
    Digital_In.Add (Me.Controls (String.Format ("DI_{0:00}", i)))
Next
 
Dim i As Integer
For i = 1 To 16
Digital_In.Add("DI_0" & i)
Next

What would happen when you got to 10 - surely you would get "DI_010" - is this what the control is called, or is it "DI_10"?

Oh, and this also suggests to me that you are writing code with Option Strict Off. You really should turn Option Strict On :)
 
Thanks. I will turn Option Strict on and see what happens! (probably something bad)
:)

You are indede correct - my control is called DI_10! My code was set out to be :-

VB.NET:
Dim i As Integer
        For i = 1 To 9 : DigitalsOnScreen.Add("DI_0" & i) : Next
        For i = 10 To 16 : DigitalsOnScreen.Add("DI_" & i) : Next

Thanks again for the tip!! That fixed it!
 
Back
Top