how to init array of objects? (controls)

  • Thread starter Thread starter Rich
  • Start date Start date
R

Rich

Greetings all,

(apologize in advance if I am too VB6'd out). So I have a
list of controls on a vb.net windows form. 2 datetime
pickers, 2 checkboxes, 4 textboxes. In VB6 I could load
these guys up into a variant array and initialize them. I
understand in vb.net the variant went away for an object.
So I created an array of objects, but I can't initialize
the controls. How to do it? Here is what I have (kind of
pseudocode):

Dim arrObj As Object() = New arrobj() {dtp1, dtp2, chk1,
chk2, txt1, txt2, txt3, txt4}
Dim i As Integer
For i = 0 to arrObj.Length - 1
If i < 2 Then arrObj(i).Value = #3/1/2004#
If i > 1 and i < 4 Then arrObj(i).Checked = True
If i > 3 Then arrObj(i).Text = "test" & i
Next

Obviously, this won't even compile but it is what I would
like to do. Any suggestions appreciated how I could do
this.

Thanks,
Rich
 
Hi

I think you can use GetType().ToString() metho
this will return a string with the name of the class
ex
If (Obj(i).GetType().ToString() == "Label"
'init labe
Else ...

Or you use Select Case
ex
Select Case (Obj(i).GetType().ToString()
Case "Label
'init labe
Case "Button
'init butto
Case ...

Refer to these links in the MSDN documentation
ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfSystemObjectClassGetTypeTopic.ht
ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfSystemTypeClassToStringTopic.htm
 
Thanks. Here is something else I tried (of the dozens of
things I tried)
------------------------------------------------
Dim arrObj As Object() = New arrobj() {dtp1, dtp2, chk1,
chk2, txt1, txt2, txt3, txt4}
Dim ctl As Control

For Each ctl In arrObj
If ctl.GetType.Name.Equals("DateTimePicker") Then
'I get this far but then what?
ctl what? I only get errors from here
 
OK. I turned off Option Strict. Now I can do my thing.
But still, this is like cutting corners. Don't think it
will go too far in .net. Maybe it will. But is there a
way to do this with early binding?
--------------------------------------------
Option Strict Off
....
Dim arrObj As Object() = New arrobj() {dtp1, dtp2, chk1,
chk2, txt1, txt2, txt3, txt4}
Dim ctl As Object
'late binding
For Each ctl In arrobj
If ctl.GetType.Name.Equals("DateTimePicker") Then
ctl.value = #3/1/2004# 'this works OK
End If
Next
....
-------------------------------------------------
 
Solution
Dim Array() As Object = New Object() {DateTimePicker1, Label1
Dim ctr As Contro
For Each ctr In Arra
MsgBox(ctr.GetType().ToString()
If (ctr.GetType().ToString() = "System.Windows.Forms.DateTimePicker") The
CType(ctr, DateTimePicker).Value = New DateTime(2004, 3, 1
End I
If (ctr.GetType().ToString().Equals("System.Windows.Forms.Label")) The
CType(ctr, Label).Text = "Hello World!
End I

Next
 
If you use
dim DTP as DateTimePicker = DirectCase(Ctl, DateTimePicker
then you can have all of the properties and methods of the control available

Also in reference to your original post, if you want to manage a group of controls or other objects, if you stash them in a Hashtable, as opposed to an Object array, you will have more control

If HT is an existing hashtable of controls you have created, you can loop through them as follow
Dim Keys as ICollection = HT.key
dim Key as strin
dim Ctl as Contro
For each Key in Key
Ctl = Ht(key
select case Ctl.gettype.nam
case "TextBox
dim TXT as textbox = directcast(ctl, textbox
'...get full access to control as textbo
case "Label
dim LBL as label = directcast(ctl, label
'...get full access to control as labe
end selec
Nex

There are other ways Hashtables offer a lot of flexibility in managing controls and other objects, such as the methods ContainsKey, and ContainsValu

ArrayLists are handy when you only need to access members by index
 
Hi Rich,

Now some more time, I have till now not seen that I needed a declared
"object" in VB.net so just try to avoid that.

Option Strict On

Dim arrContr As Control() = New arrobj() {dtp1, dtp2, chk1, chk2, txt1,
txt2, txt3, txt4}
Dim ctl As Control
'Early binding
For Each ctl In arrContr
If ctl.GetType.Name.Equals("DateTimePicker") Then
ctl.value = #3/1/2004# 'this works OK
End If
Next

I hope this helps?

Cor
 
Back
Top