473,396 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inherited class behavior vs. C#

I ran into this while converting some DX C# code to VB.NET (VS 2003),
and I'm just curious as to whether this is intended behavior (and if
so, where might I read up on it) or more of a bug.

This involves creating a New instance of a derived class that inherits
a base class. In C#, private member vars in the derived class ( e.g.
private MySimpleObject MyObject = new MySimpleObject();) are
instantiated immediately when the the calling code attempts to
instantiate the derived class. Order of execution then goes
DerivedClass::New() method, then (before executing code within
DerivedClass::New) to BaseClass:New().

The problem/difference is that, using the same classes in VB.NET, the
order of execution changes. The private member var object in the
Derived class does not get instantiated until AFTER the execution
returns from BaseClass.New() and begins the actual code within
DerivedClass.New().

I can implement a workaround (this actually causes errors with the DX
code due to an overridden method which expects the object to have been
instantiated by that point). But is this an *intended* difference
between the two languages?

If my explanation of the problem isn't clear, please advise and I'll
post some sample code. It's easy to reproduce.

Thanks in advance.

Nov 21 '05 #1
2 1760
The VB behavior is by design - I believe the idea is that before any code in the derived class runs (including the initialization of member variables, which in
vb can use non static base members), the base class is fully constructed. Of course, you can still work around that by calling overriden members in the
base constructor, although that sounds like something I'd personally avoid - the base class is invoking code in the derived class that will be run before the
derived class's constructor has finalized initialization, how sure can you be that whoever is writing the derived class has assumed that that code can be run
before the constructor?
(I mean, I wouldn't trust myself to remember in six months that a method that I override from the base can't assume initialization is finished :) )

The approach In C#'s case is somewhat safer, but it does imply that you can't use base class's data members to initalize the derived class's data
members on the declaration.

Alex
MS VB QA
--------------------
From: "twawsico" <tw******@hotmail.com>
Newsgroups: microsoft.public.dotnet.languages.vb
Subject: Inherited class behavior vs. C#
Date: 20 Mar 2005 07:26:25 -0800
Organization: http://groups.google.com
Lines: 28
Message-ID: <11**********************@z14g2000cwz.googlegroups .com>
NNTP-Posting-Host: 24.0.20.109
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1111332390 16099 127.0.0.1 (20 Mar 2005 15:26:30 GMT)
X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: Sun, 20 Mar 2005 15:26:30 +0000 (UTC)
User-Agent: G2/0.2
Complaints-To: gr**********@google.com
Injection-Info: z14g2000cwz.googlegroups.com; posting-host=24.0.20.109;
posting-account=wCp07Q0AAABPQrPQRaSXkjnLdzxjpO6y
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!news- spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s.google.com!z14g2000cwz.googlegroups.com!not-for-mailXref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.vb:266377
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

I ran into this while converting some DX C# code to VB.NET (VS 2003),
and I'm just curious as to whether this is intended behavior (and if
so, where might I read up on it) or more of a bug.

This involves creating a New instance of a derived class that inherits
a base class. In C#, private member vars in the derived class ( e.g.
private MySimpleObject MyObject = new MySimpleObject();) are
instantiated immediately when the the calling code attempts to
instantiate the derived class. Order of execution then goes
DerivedClass::New() method, then (before executing code within
DerivedClass::New) to BaseClass:New().

The problem/difference is that, using the same classes in VB.NET, the
order of execution changes. The private member var object in the
Derived class does not get instantiated until AFTER the execution
returns from BaseClass.New() and begins the actual code within
DerivedClass.New().

I can implement a workaround (this actually causes errors with the DX
code due to an overridden method which expects the object to have been
instantiated by that point). But is this an *intended* difference
between the two languages?

If my explanation of the problem isn't clear, please advise and I'll
post some sample code. It's easy to reproduce.

Thanks in advance.

Nov 21 '05 #2
Thanks for the info. :)

Nov 21 '05 #3

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

Similar topics

2
by: Joăo Santa Bárbara | last post by:
hi all perhaps can someone help me. i have a form ( base ) where a i change the opacity from 100 to 60 % now i have a form inherited from the previous. and the opacity is correctly at 60%. ...
0
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
5
by: Earl Teigrob | last post by:
Is there a way to rename the public properties of a inherited class? I am inheriting an asp.net control (class) and am adding addtional functionality. (in this case, up to 3 borders on an...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
4
by: dbuchanan | last post by:
Is the following behavior normal? Both the 'Protected sub' in the inherited form and the 'Private Shadows sub' in the derived form fires. My interpretation of MSDN help on the topic "Shadows"...
3
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
0
by: tomislav.bartolin | last post by:
Hi, I have inherited TaskSettings class from the ApplicationSettingsBase class and are able to get the configuration settings for this class quite nicely in my windows app. Now, if I inherit...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
7
by: Daniel Kraft | last post by:
Hi all! I'm getting some for me "unexpected behaviour" from g++ 4.1.2 when compiling this simple test program: #include <cstdlib> #include <cstdio> using namespace std; template<typename...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.