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

Home Posts Topics Members FAQ

Forgot Syntax: calling a parameterized constructor of the same class from a non-parameterized one

/* I am writing this C# program because I forgot the syntax for
calling a parameterized constructor from a non-parameterized one
within the same class.

I was coding in Java and just typed it like this:

public MyClass(String someParam)
{
}

public MyClass()
{
this.MyClass("S omeparam");
}

The compiler complained and I remembered it was like this in Java:

public MyClass()
{
this("Someparam ");
}

This led me to think that I am forgetting the way it is done in C#
and that my mistake was influenced by the way it is done in C#. Just
so that I do not forget how to do it in C#, I am writing this small
spike solution as a reminder.

I have now tried

this.MyClass("S omeParam");
MyClass("SomePa ram"); and
this("SomeParam ");

None of them seem to work. What's the correct way. I realize I have
forgotten the correct way.

*/

Apr 5 '07 #1
5 2203
OK. RESOLVED.

It is

new SomeClass("Some Param");
Got it.

Apr 5 '07 #2
Sathyaish wrote:
OK. RESOLVED.

It is

new SomeClass("Some Param");

Got it.
Test that and you'll see it isn't doing what you want. See my reply to your
original.
--
Tom Porterfield

Apr 5 '07 #3
Sathyaish wrote:
/* I am writing this C# program because I forgot the syntax for
calling a parameterized constructor from a non-parameterized one
within the same class.

I was coding in Java and just typed it like this:

public MyClass(String someParam)
{
}

public MyClass()
{
this.MyClass("S omeparam");
}

The compiler complained and I remembered it was like this in Java:

public MyClass()
{
this("Someparam ");
}
What you want is:

public MyClass() : this ("Someparam" )
{
}
--
Tom Porterfield
Apr 5 '07 #4
On Apr 5, 12:35 pm, "Sathyaish" <sathya...@gmai l.comwrote:
/* I am writing this C# program because I forgot the syntax for
calling a parameterized constructor from a non-parameterized one
within the same class.

I was coding in Java and just typed it like this:
<snip>

See http://pobox.com/~skeet/csharp/constructors.html for this - along
with other differences between Java and C# when it comes to
constructors.

Jon

Apr 5 '07 #5
Thanks, Tom and Jon.

Ah! That was it. Thank you for the reminder, Tom. What I did earlier
was create a new object of the same class from an object of itself.

Apr 5 '07 #6

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

Similar topics

2
3876
by: lkrubner | last post by:
My code was dying on the line below where I use method_exists: if (class_exists($nameOfClassToBeUsed)) { $object = new $nameOfClassToBeUsed(); $this->arrayOfAllTheObjectsSoFarLoaded = & $object; if (method_exists($object, "setCallingCode")) $object->setCallingCode($nameOfFunctionOrClassCalling); return $object; } else {
7
1587
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: http://mail.python.org/pipermail/python-dev/2004-August/047112.html http://mail.python.org/pipermail/python-dev/2004-August/047279.html
31
5196
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I haven't been able to figure out is how to call one constructor from another within a class. It's easy enough to call the base class's constructor from the derived class, but that's not what I'm trying to do. For example, in Java (or J#) it's easy...
4
1149
by: Colin McGuire | last post by:
Hi, I have a class with lots of constructors - one is shown below Public Sub New(ByVal rows As Integer, ByVal columns As Integer) InitializeComponent() 'And code in here that sets instance variables, 'and does calculations based on rows and columns passed 'in in the constructor, and lots more. End Sub Another of my constructors has no parameters, and there are various
20
3234
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something like the below and have no idea what to do with it :) inheritedClass::inheritedClass () : originalConstructor () , m_cells(cells), m_srfcs(srfcs) {
11
3454
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
1
8557
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
9
1372
by: parag_paul | last post by:
Hi all I am seeing the following code in one place standalone line in some function , { (void*) new (h) Class_Name(xip, virobj, type, true); } Does it make h an object of the Class_Name class
3
1529
by: willo | last post by:
Greetings all, I have run into a small problem with my understanding of some C++ language syntax, and seek some clarification. Below is a condensed version of some code I'm having difficulty with: =============================================================== #include <cstddef// size_t
4
1979
by: Joe, G.I. | last post by:
I didn't write ClassA, but I'm trying to inherit from ClassA through ClassB. I want to pass a string to the ClassA constructor but not sure I'm calling ClassA's constructor correctly. I get the following g++ error ... In constructor 'ClassB::ClassB(String&)': ../../../include/ClassB.h:19: error: expected `{' at end of input ClassB *myClass = new ClassB("myClassName");
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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...
0
8989
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
7512
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
5397
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
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
3669
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.