Rich wrote:
In VB6 I used to be able to add an object like a textbox and see the dropdown
properties of that textbox from the collection object:
Dim col as collection
set col = new collection
col.Add(txt1)
col(0).Text = "test"
This is not happening in VB.Net. Is there a way to use a collection object
in VB2005 this way?
Sort of, but VB.Net does things a little differently.
In VB6, Collections contained Variants so, once you typed as far as a
Variant in the your code, the IDE /gave up/ trying to work out what it
could give you next but let you type it anyway. Only at /run-time/ did
it worry about whether it was valid or not to access, say, a .Text
property on whatever object it happened to have at the time.
In VB.Net, Variants are dead and buried (and good riddance), being
replaced by far tighter variable Type'ing rules at compile time and the
[almost complete] removal of this run-time "guesswork" about whether
method calls are valid or not.
Collections contain Objects (most other types are derived from Object,
so they will all "fit" into an Object variable). To be able to do, say,
TextBox-y things to an item in a Collection, you have to convince the
/compiler/ that it's a valid thing to do. You do this by first ensuring
that the thing you've got from the Collection really /is/ the Type you
want it to be, then tell the compiler to treat it as such, as in
Dim txt1 as New TextBox
Dim col as New Collection
' There are better classes for this, ArrayList being the easiest,
' and "Dim .. As New .." is perfectly correct now.
col.Add(txt1)
' First check the Type of the item ...
If TypeOf col.Item(0) Is TextBox Then
' ... then use it as a TextBox
DirectCast( col.Item(0), TextBox ).Text = "test"
End If
(Some people use CType instead of DirectCast. I prefer this because it
only does Type casting and won't try to do anything "clever" (like
converting between different data types) for you - CType will.
Or is there another object I should use that can do this?
Look in the System.Collections Namespace.
Actaully, what I am storing in the collection object is a DataTable.
Odd thing to do - why not leave them in their more normal setting - a
DataSet?
I want to do something like this:
tblColl.Add(dataset1.tbl1)
tblColl.Add(dataset1.tbl2)
...
For i As Integer = 0 to tblcoll(j).Rows.Count - 1
...
But I don't [see?] Rows after tblColl(0).
Same problem. The collection item is of Type Object, and the Object
Type doesn't have a "Rows" property (it doesn't have much of anything).
To use it /as/ a DataTable, you have to convince the compiler that it
is OK to do so, as in
For i As Integer = 0 _
To DirectCast( tblcoll(j), DataTable ).Rows.Count - 1
HTH,
Phill W.