473,473 Members | 1,469 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

a question about Dim statement

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
Nov 21 '05 #1
3 1116
* "Amin Sobati" <am***@morva.net> scripsit:
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.


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/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:#x**************@TK2MSFTNGP11.phx.gbl...
* "Amin Sobati" <am***@morva.net> scripsit:
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.


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/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
* "Amin Sobati" <am***@morva.net> scripsit:
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


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/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
1
by: Dirk Försterling | last post by:
Hi, sorry for reposting, but it seems my message just hit nothing but air. If I posted to the wrong list or did something else wrong with the message, please let me know. I really want to...
15
by: Thelma Lubkin | last post by:
formA determines some_where and some_value and issues docmd.close ,Me docmd.openform "formB", , ,some_where, , ,some_value formB receives the correct some_where and some_value After...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
9
by: Michael | last post by:
Hi all, I would like to get people's opinion about executing SQL statements in C# (or any other .NET language really). I used to create my SQL statement by building a string and replacing single...
11
by: Sensei | last post by:
Hi again! I have still curiosity about the reason of some C constructs/keywords... The first is about static functions. What was the reason of restricting a function to be visible just in a...
4
by: kernelxu | last post by:
Hi,folks. I got some suggestion about bitwise shift from <The C Book, second edition>(written by Mike Banahan, Declan Brady and Mark Doran, originally published by Addison Wesley in 1991. This...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: Caper | last post by:
Hello everyone, I am a bit confused about Innodb deadlock and the "SELECT…FOR UPDATE” statement. 1. Here is a deadlock example 1) Create actor table CREATE TABLE actor(actor_id INT...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.