Hello all,
I need to know if I can call a C# constructor in the same way VB.NET and
JAVA can. For example:
class myClass {
public myClass() {
Console.WriteLine("Hello from constructor 1");
}
public myClass(string name) {
Console.WriteLine("{0} says \"Hello from constructor 2\"", name);
myClass(); //want to call the 1st constructor!!
}
}
many thanks and Happy New Year (in advance)
Alan Seunarayan 5 1254
oh well, I think I have sorted it!
class myClass {
public myClass() {
Console.WriteLine("Hello from constructor 1");
}
public myClass(string name) :this() { //<---------fixed?
Console.WriteLine("{0} says \"Hello from constructor 2\"", name);
// myClass(); //want to call the 1st constructor!!
}
}
but if anyone knows better then please tell me.
thanks again
"Alan Seunarayan" <w@xyz.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Hello all, I need to know if I can call a C# constructor in the same way VB.NET
and JAVA can. For example:
class myClass { public myClass() { Console.WriteLine("Hello from constructor 1"); }
public myClass(string name) { Console.WriteLine("{0} says \"Hello from constructor 2\"", name); myClass(); //want to call the 1st constructor!! } }
many thanks and Happy New Year (in advance)
Alan Seunarayan
that idea sort of came to me just after posting, but there are always more
than one way to "skin a cat" and I just wanted to see if there was also
another way.
thank though
Alan
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:e9****************@TK2MSFTNGP11.phx.gbl... Alan,
but if anyone knows better then please tell me.
What do you mean by "better"? What's wrong with using this()? Mattias
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Alan Seunarayan <w@xyz.com> wrote: that idea sort of came to me just after posting, but there are always more than one way to "skin a cat" and I just wanted to see if there was also another way.
Nope, that's it. Note that your first example wouldn't have worked in
Java either though, as (just like in C#) the other constructor (or the
base class's constructor) must be the first thing called.
See http://www.pobox.com/~skeet/csharp/constructors.html for more
information.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Thanks and happy new year!
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Alan Seunarayan <w@xyz.com> wrote: that idea sort of came to me just after posting, but there are always
more than one way to "skin a cat" and I just wanted to see if there was also another way.
Nope, that's it. Note that your first example wouldn't have worked in Java either though, as (just like in C#) the other constructor (or the base class's constructor) must be the first thing called.
See http://www.pobox.com/~skeet/csharp/constructors.html for more information.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Giulio |
last post by:
why definition of two constructors like these is not possible in c++???
-----------------------
date::date(const int d, const int m, const int y, const int ora, const int
mi, const int se){...
|
by: Philipp |
last post by:
Hello, a very simple question:
Ok I have a class MyClass with a constructor MyClass(int) (no constructor
without argument defined)
how can I make an array of pointers to objects of that class,...
|
by: lallous |
last post by:
Hello,
Probably I am not seeing it now, but is it possible for constructor(int) to
call constructor() ?
class A
{
private:
int x, y;
public:
|
by: Sylvain |
last post by:
Let's say I have the following code where a class 'pipo' has 8 instances
of foo:
class foo
{
foo ( const char * _name):
name = _name
{
}
|
by: Gonçalo Rodrigues |
last post by:
Hi all,
Is it possible to call a constructor of a class, call it Object,
explicitely? e.g. suppose you have a void pointer ptr pointing to a
block of memory big-enough to hold an Object. Is...
|
by: Daisy |
last post by:
What I want to do is this:
Object1 list = (Object1)this.MyMethod(typeOf(Object1), "Object1",
"string1", "string2");
and
Object2 list = (Object2)this.MyMethod(typeOf(Object2), "Object2",...
|
by: Josef Meile |
last post by:
Hi,
I have this constructor:
public CExcelDatabase(string host, string user, string password, string
database,
bool promptCredentials, int
findExcelInstance, bool readOnly)
{
//Some code...
|
by: a |
last post by:
Hi
Extremely easy question for someone who knows the answer I'm sure
:-) (aren't they all)
What exactly does LoadControl do? Whats the difference between calling
this and just constructing a...
|
by: Michael |
last post by:
Hello,
I want to use an object (LowCut) within another object (SampleRateConverter)
like it is written as follows:
class SampleRateConverter
{
public:
SampleRateConverter( int...
|
by: Kavya |
last post by:
Suppose I have class MyClass. Then which will be termed as explicit
call to constructor.
MyClass( );
or
MyClass object;
object.MyClass( );
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |