Groups | Blog | Home
all groups > vb.net > september 2004 >

vb.net : a question about Dim statement


Amin Sobati
9/4/2004 5:49:50 PM
Hi,
I have two classes, class1 and class2.
class2 inherits from class1. I'm wondered that what's the difference
between:
Dim myObj as Class1 = New Class2
and
Dim myObj as Class2 = New Class2
In both cases MsgBox(TypeOf myObj Is Class1) returns true.
Thanks,
Amin

hirf-spam-me-here NO[at]SPAM gmx.at
9/5/2004 1:32:14 PM
* "Amin Sobati" <amins@morva.net> scripsit:
[quoted text, click to view]

Both objects will be objects of type 'Class2', but 'Class1' is a
supertype of 'Class2', so assigning an instance to a variable of type
'Class1' will only allow you to see/access members defined in 'Class1',
even if you are dealing with an object of type 'Class2'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
hirf-spam-me-here NO[at]SPAM gmx.at
9/5/2004 2:38:44 PM
* "Amin Sobati" <amins@morva.net> scripsit:
[quoted text, click to view]

The line above does not make much sense. Assume you write a class
library that contains a class with a 'Foo' method:

\\\
Public Sub Foo(ByVall Goo As Class1)
...
End Sub
///

When writing this class/method, you don't know which subclasses 'Class1'
will have. The user of the class library can now pass instances of any
subclasses of 'Class1' to the method.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
Amin Sobati
9/5/2004 4:58:26 PM
I agree!
But if I need access to Class1 members, I can write:
Dim MyObj as Class1 = New Class1
What's the usage for being able to write:
Dim MyObj as Class1 = New Class2
?
Thanks,
Amin



[quoted text, click to view]

AddThis Social Bookmark Button