collection object not showing properties of stored object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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? Or is there another object I should use that can do
this? Actaully, what I am storing in the collection object is a DataTable.
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 Rows after tblColl(0). What should I do to perform this kind
of operation with my datatables?

Thanks,
Rich
 
Nevermind. I forgot:

dim tblColl() As DataTable
tblColl = New DataTbl(){ds.tbl1, ds.tbl2, ds.tbl3}

Now I have my dropdpwns.
 
Rich said:
Nevermind. I forgot:

dim tblColl() As DataTable
tblColl = New DataTbl(){ds.tbl1, ds.tbl2, ds.tbl3}

Now I have my dropdpwns.

Not sure if you know this, but the DataSet class has a Tables collection
already built in. So, try:

....
For i As Integer = 0 To dataset1.Tables(j).Rows.Count - 1
...
Next i
....

HTH,
Mythran
 
Rich,

There is one thing that every regular in this newsgroup tels about the
microsoft.visualbasic.namespace they should in that not have included the
collection, that one is weird working.

Try the arraylist or any other collection or list from system.

For the rest, I don't understand your problem (probably it is a cast
problem), if nobody else answers, than please rephrase.

And than not with the collection but with by instance the arraylist (the
most basic one).

http://msdn2.microsoft.com/en-us/library/system.collections.aspx

I hope this helps,

Cor
 
Rich said:
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.
 
Phill,

I will not say that my answers are always excellent readable (I wished they
were). I had to read one sentence of you in this message 5 times, because I
thought that you were writing it wrong but could not believe it because of
the rest of the answer.

Therefore for others, the "this" in the message about the DirectCast and the
CType is the DirectCast, than the explanation is completely correct.

Don't see this as comment, it was an excellent answer,

Cor

Phill W. said:
Rich said:
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.
 
Back
Top