Collections

  • Thread starter Thread starter WStoreyII
  • Start date Start date
W

WStoreyII

when i try to reference my collection (cards)
alls i get is the gettype property

for example

cards(1).gettype

where are all the other properties?????

wstoreyII
 
A collection is just an object so you have to cast it to the correct type to
get to it's properties.

for example...

\\\
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Console.WriteLine(DirectCast(sender, System.Windows.Forms.Button).Text)
Console.WriteLine(CType(sender, System.Windows.Forms.Button).Text)
End Sub
///

The online help will explain the differences between 'DirectCast' and
'CType'.

Gary
 
but i need to use it like for instance
how do i show the control on the form
i can add it to the forms controls like this

Me.Controls.Add(MyDeck.Collection.Item(1))

but it will not let me show the control and since i am making a card game i
need to show the cards

thanks

WStoreyII
 
Dim MyCard as Card = CType(MyDeck.Collection.Item(1), Card)
Me.Controls.Add(MyCard)

or simpler:

Me.Controls.Add(CType(MyDeck.Collection.Item(1), Card))

Cheers,
Lubos
 
DirectCast will not generate a compile time error even if the casting is
done incorrectly while CType will. Using DirectCast you will not know if
you have casted correctly or not until the Exception Manager tells you
whereas with CType you are sure to know beforehand. Early and late
binding.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
* "WStoreyII said:
when i try to reference my collection (cards)
alls i get is the gettype property

for example

cards(1).gettype

where are all the other properties?????

Post the declaration of 'cards'.
 
WStoreyII,
Is this related to the source you posted earlier?

You are not using strongly typed collections. Instead of ArrayList &
SortedList create a CardCollection class that inherits from CollectionBase,
then add type safe Add, Remove subs & an Item Property...

Something like (untested):
Public Class Card
End Class

Public Class CardCollection
Inherits CollectionBase

Public Sub Add(ByVal card As Card)
InnerList.Add(card)
End Sub

Default Public Readonly Property Item(ByVal index As Integer) As
Card
Get
Return DirectCast(InnerList.Item(index), Card)
End Get
End Property

End Class

Also, using a type safe collection as above & Option Strict On, you will
catch errors at compile time instead of runtime.

Hope this helps
Jay
 
Ravichandran J.V.
DirectCast will not generate a compile time error even if the casting is
done incorrectly while CType will.
DirectCast & CType will both generate an exception in the same cases & for
the same reasons.

Specifically DirectCast will generate a compile time error especially if the
casting is done incorrectly.

CType may not generate an exception if there is a conversion available!

For example:

Dim i As Integer
Dim s As String
Dim o As Object

s = "123"
o = s

i = CType(o, Integer) ' succeeds
i = DirectCast(o, Integer) ' fails

The first succeeds as there is a conversion available from String to
Integer, the second causes an Exception as a String is not an Integer.

Hope this helps
Jay
 
Dim i As Integer
Dim s As String
Dim o As Object

s = "123"
o = s

i = CType(o, Integer) ' succeeds
i = DirectCast(o, Integer) ' fails

There is not so much to be talked abt if you talk of converting an
inbuilt type to another as in your example above. Try casting into a
user-defined class which is modified as MustInherit(if you are VB
oriented) and obtain it through inheritance into another class. Then,
try DiectCast to cast a normal class to that of the MustInherit class
and it will work till the exception is thrown at runtime. Whereas, in
the same scenario, CType will show you an error before compile time. I
am of course talking in terms of VS.Net Ide.

By the way, since you are a MVP-Outlook, I am asking this question. When
I try to switch my identity in Outlook Express 5.0 after having added a
new identity, why does the switch not take place ?

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
J.V.
Try casting into a
user-defined class which is modified as MustInherit(if you are VB
oriented) and obtain it through inheritance into another class. Then,
try DiectCast to cast a normal class to that of the MustInherit class
and it will work till the exception is thrown at runtime. Whereas, in
the same scenario, CType will show you an error before compile time. I
am of course talking in terms of VS.Net Ide.
That made absolutely no sense! :-(

Please use either proper OO terms or proper VB.NET terms. Or take 2 minutes
to type in an example of what you are attempting to state, it would help.

Do you mean:
1. cast an abstract class (MustInherit) into a derived class (inherited)
2. cast a derived class into a base class
3. cast Class1 to Class2 where Class1 & Class2 are not related
4. convert Class1 to Class2 where Class1 & Class2 are not related

First remember that DirectCast is the Cast operator, while CType is the
Conversion operator that will do a Cast if the cast is allowed. Also
remember that Casting & Converting are separate (although closely related)
operations.

Looking at each of the above:
1. Casting an base (abstract or not) class to a derived class there is no
difference between CType & DirectCast as I stated. This is normal usage for
DirectCast. In this case derived class also includes implemented interfaces.
2. Casting a derived class into a base again there no difference. This cast
is actually implicit so CType or DirectCast is not needed.
3. Casting Class1 to Class2 is simply not allowed, you will get a runtime
error.

4. However! Conversion is allowed, if there is a conversion defined between
Class1 & Class2! Again If a conversion is defined is the key word here!
Today the conversions are defined for the "built-in" types. For user types
you will need to wait for Whidbey to get conversion operators, hence today
Convert Type will give an error when you attempt to Convert Class1 to
Class2, however tomorrow if the conversion is defined it will convert the
type! Conceptionally a conversion to String is defined for all types as
System.Object has the ToString method however I don't think, nor would I
expect, CType will recognize this.

(Hint at) Overloading Operator CType in Whidbey:
http://blogs.gotdotnet.com/cambecc/permalink.aspx/5de5a161-9150-4237-a751-127195cceeab

Whidbey also promises to give us TryCast which is a variation of DirectCast:
http://www.panopticoncentral.net/PermaLink.aspx/0d6ba439-8126-427e-952e-3f5fbba33904

By the way, since you are a MVP-Outlook, I am asking this question. When
I try to switch my identity in Outlook Express 5.0 after having added a
new identity, why does the switch not take place ?
Yes I am an Outlook MVP, I am not however an Outlook Express MVP.

Hope this helps
Jay
 
Back
Top