473,786 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloaded constructors

Can someone quick shed some light on this issue I am having?

I wanted to create 2 constructors for my object, one that takes an ID
value and one that takes a datarow.

If ID value is provided, it is going to get the info from the database
and populate itself. If a datarow is provided, then it will use the
row to populate itself.

So, I wanted to have the "by ID" function get a datarow from the
database, and then call the overloaded New function and pass in the
datarow, to keep all the logic in one place.
For example:

public sub New(ID as integer)
'Get Datarrow from DB
dim oRow as DataRow
call Me.New(oRow)
end sub

public sub New(Row as DataRow)
'Fill properties of class
end sub
Like that...
Now, I know that I can move the logic into another private method,
which is what I have done for a workaround, but is there a reason I am
not allowed to do this? Is this not a good way to work with overloaded
constructors?

Nov 21 '05 #1
7 1500
What do you mean you are not allowed to do that? Are you getting an
error message? In what way is it not working?

Instead of Me.New, I would recommend calling MyClass.New, MyClass being
a reserved keyword in VB.

Nov 21 '05 #2
I get the same compile error w/ Me., MyBase, and MyClass:

Constructor call is valid only as the first statement in an instance
constructor.

Nov 21 '05 #3
You need to have the call to the other ctor be the first instruction -
you can't even have a declaration before it.
The best that I can think of is have a common routine that both call

Public sub new( _
id as integer _
)
Init(GetRow(id) )
end sub

Public sub new ( _
row as DataRow _
)
Init(row)
end sub

Private function GetRow( _
id as integer _
)
...
end function

Private sub Init( _
row as DataRow _
)
// all the code that was previously in the DataRow version of the
ctor
end sub
hth,
Alan.

Nov 21 '05 #4

FYI you can put the code that retrieves the DataRow given the ID in a
Shared method, and call it like this

Public Sub New(ID as integer)
MyClass.New(Get RowFromId(ID))
End Sub
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #5
That is basically what I am doing now, just wanted to make sure I
wasn't missing something or doing it the wrong way.

Nov 21 '05 #6
On 2005-08-29, cmay <cm**@walshgrou p.com> wrote:
Can someone quick shed some light on this issue I am having?

Now, I know that I can move the logic into another private method,
which is what I have done for a workaround, but is there a reason I am
not allowed to do this? Is this not a good way to work with overloaded
constructors?


To answer your second question first, I think using the overloaded
constructors with a private initialization function is indeed the perfect
way to work with constructors (Interestingly, using a shared function
to get the datarow as someone else suggested feels wrong to me, although
the two are basically equivalent).

For the first question, the reason you can't do the above is that when
you write a Sub New, the compiler puts in code to construct the base
class as the first line of the Sub New. IOW, what you're really trying
to do above is...

Public Sub New (id as Integer)
MyBase.New()
' do something
Dim row As New DataRow
Me.New(row)
End Sub

Public Sub New(row As DataRow)
MyBase.New()
' do something
End Sub

You see the problem. The base ctor is being called twice. Now, when
you chain constructors...

Public Sub New(id As Integer)
Me.New()
End Sub

Public Sub New()
MyBase.New() ' <-- The compiler puts in this call
End Sub

In this case, the compiler knows that it doesn't need to call the base
class ctor from New(id), because New() will call the base class, so
New(id) doesn't get the base class call. But the compiler only figures
this out if the very first line of Sub New is a call to another Sub New.
Anything else is a compiler error.

Technically, I suppose the compiler *could* be smarter than this, since
there's no inherent reason that your original code couldn't compile
and run fine. But this behavior has sort of become a standard across
multiple languages, and handling this differently would be a very tricky
problem for a compiler.
Nov 21 '05 #7
Thanks for the indepth answer.
Chris

Nov 21 '05 #8

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

Similar topics

1
2382
by: andrea_gavana | last post by:
Hello NG, I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython implementation. In the C++ source code, a unique class is initialized with 2 different methods (???). This is what it seems to me. I have this declarations: class wxFoldWindowItem { private: wxWindow *_wnd;
1
2226
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen pertaining to casting class A to class B, the implementation of the
3
1599
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client app with; D m_D = new D(tkn); public class A : MarshalByRefObject public A () <--------------------- public class B : A public B () <----------------------
3
3320
by: mblatch | last post by:
This is probably a C#-101 type question, but I've struck out on finding an answer. I am setting up a class with a few overloaded constructors. Inside those constructors, I would like to call the "default" zero-argument constructor. The compiler gives me an error "PC_StreamBuffer is a type but is used like a variable". I've tried adding "this." in front of references to PC_StreamBuffer(), but that gives another compiler error. A...
3
1711
by: Vera | last post by:
I built a class in VB.NET that has an overloaded constructor. It can either accept nothing, a string or an object Public Sub New( MyBase.New( End Su Public Sub New(ByVal strName As String MyBase.New( '... Do init with strNam End Su
5
2284
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the overloaded operator function performs the necessary copy of obj's member in to obj1. Can't the same be done using copy
3
1933
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
13
1520
by: =?Utf-8?B?QW5kcmVhcw==?= | last post by:
Hi, I would like to get some thoughts on Overloaded constructors vs. Object initializations. Assuming that the class supports a default constructor, is there any reason to include overloaded constructors when you are able to use object initialization? I see a point, to provide the overloads, if you are developing framework code that might support the need to run ontop of 2.x but if you are writing a sealed application / know you are...
6
4171
by: Joe HM | last post by:
Hello - I have a question regarding overloading the New() in a MustInherit Class. The following show the code in question ... MustInherit Class cAbstract Public MustOverride Sub print() Protected mName As String
0
9497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7515
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.