473,408 Members | 1,676 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,408 software developers and data experts.

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 1477
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(GetRowFromId(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**@walshgroup.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
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 (???)....
1
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...
3
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...
3
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...
3
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...
5
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...
3
by: jerry.teshirogi | last post by:
I have the following class and main: ////////////////////////////////////////////////////////// #include <iostream.h> class myVector { public: double x, y, z:
13
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...
6
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() ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...

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.