473,770 Members | 4,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor call within a constructor


Is it possible to call a constructor from within a
constructor

I mean

Class A
{
public A(string getModifiedVal)
{
.........
}
}

Class B
{
public B()
{
//Do some processing to get modifiedValue

A(modifiedValue ); // I know this doesn't work
//but is there a way to do it
}
}
Nov 16 '05 #1
4 2067
A constructor always constructs an object. In the constructor for B, you
could do this

public B()
{
A obj=new A(modifiedValue );
}

If you don't want an A-object you should put the actual functionality into
a static method:

Class A
{
public A(string getModifiedVal)
{
DoSomething(get ModifiedVal);
}

public static void DoSomething(str ing val)
{
//action
}
}

Class B
{
public B()
{
//Do some processing to get modifiedValue

A.DoSomething(m odifiedValue);
}
}

/Hugo

On Thu, 9 Sep 2004 03:46:11 -0700, Greg wrote:
Is it possible to call a constructor from within a
constructor

I mean

Class A
{
public A(string getModifiedVal)
{
.........
}
}

Class B
{
public B()
{
//Do some processing to get modifiedValue

A(modifiedValue ); // I know this doesn't work
//but is there a way to do it
}
}

Nov 16 '05 #2
A derived class call always the base class constructor, if you don't specify
a constructor the base() constructor is called
The base constructor is called always as first, than is executed the code in
the constructor class.

Class A
{
public A(string s){ ...} //Call Object() constructor
}

Class B: A
{
public B() {} //Call A() that in is this case is Obejct()

public B(string s):base(s){ ... } //Call A(string s)
}

"Greg" <no****@discuss ions.microsoft. com> schrieb im Newsbeitrag
news:8a******** *************** *****@phx.gbl.. .

Is it possible to call a constructor from within a
constructor

I mean

Class A
{
public A(string getModifiedVal)
{
.........
}
}

Class B
{
public B()
{
//Do some processing to get modifiedValue

A(modifiedValue ); // I know this doesn't work
//but is there a way to do it
}
}

Nov 16 '05 #3
Greg <no****@discuss ions.microsoft. com> wrote:
Is it possible to call a constructor from within a
constructor


See http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hi,
"Hugo Wetterberg" <hu************ *@smi.mas.lu.se > wrote in message
news:1w******** *************** *****@40tude.ne t...
A constructor always constructs an object. In the constructor for B, you
could do this

public B()
{
A obj=new A(modifiedValue );
}


This will create a new instance of A local to the constructor of B, this has
nothing to do with the original post, it does not contribute to the
construction of B.

Regarding the original post.
It will not work that way, You have two options though.

If the modifications of the parameter does not imply calling a method of B
you can do so in the constructor like this:

public B( string c): base( c.SubString( 1,2 ) )
{}

if the string used in A is generated by some method in B you CANNOT do
something like:

public B( string c): base( MethodOfB( ) ) {}

if MethodOfB is an instance member cause B is not yet build.

But you can do that if MethodOfB is static :

public B( string c): base( MethodOfB( ) ) {}

static string MethodOfB() { return "test";}
Jon:

Maybe this is a good addition to your contructor page.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #5

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

Similar topics

11
580
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
8
2981
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
10
2251
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this instance constructor. This is described further in Section 10.10.1. So this means:
26
2152
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null); this.property1 = arg; this.property2 = aConstantDefinedGlobally; this.method1 = function (anArg) {
10
2032
by: imutate | last post by:
Some questions about ctors and class members Is v private in the following ? If it is why put the declaration at the top ? Is there any difference to putting it in the private section ? The reference to v() defines the constructor, it will call v's constructor, right ? class vec {
23
7240
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
16030
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
13
2470
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
6
7279
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The static constructor calls a reset function which creates a new instance of BackgroundWorker and attaches the appropriate event handlers. There is also a static method ReportProgress for the called methods to do just that. What is happening is...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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
10057
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
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
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
6676
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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

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.