razored

When you develop constructors in a class, such as this :

Code Snippet

class Quad

{

private double valA;

private double valB;

private double valC;

private double valH;

private double valK;

public Quad(double evalA,double evalB,double evalC,double evalH,double evalK)

{

valA = evalA;

valB = evalB;

valC = evalC;

valH = evalH;

valK = evalK;

}

Can I make a new object of the Quad class with not arguments I thought you could, however when I try it :

Code Snippet

Quad fsx = new Quad();

Error 1 No overload for method 'Quad' takes '0' arguments

Why does this occur

Sorry, I did not see that I have posted this in the General Section and in the language section.



(Moderator: Moved thread at user's request)




Re: Visual C# Language Involving constructors...

OmegaMan

You must supply the no argument constructor such as

public Quad() {}

C# does not provide a default type constructor.





Re: Visual C# Language Involving constructors...

razored

OmegaMan wrote:
You must supply the no argument constructor such as

public Quad() {}

C# does not provide a default type constructor.

I don't understand what you mean. That is exactly what I did, public Quad(stuff) {stuff}. Please elucidate.





Re: Visual C# Language Involving constructors...

theTroll527

razored wrote:

OmegaMan wrote:
You must supply the no argument constructor such as

public Quad() {}

C# does not provide a default type constructor.

I don't understand what you mean. That is exactly what I did, public Quad(stuff) {stuff}. Please elucidate.



You created a constructor that took arguments.

public Quad ()
{

}

That is a constructor that uses no arguments. That should be what you need.

theTroll





Re: Visual C# Language Involving constructors...

Jonathan Nappee

Btw you can have both constructors in the same file and use the one you need.
But more interesting is that if you do anything in your empty constructor, you can call it from the one that takes all the arguments:

Code Snippet


public Quad() {}


public Quad(Type a, type b,...)

: this()

{

this.a = a;

this.b = b;

...

}






Re: Visual C# Language Involving constructors...

Mark Dawson

Hi,

not quite true, C# will provide a default constructor as long as you have not defied any constructors yourself, as soon as you define a constructor then the default constructor will not automatically be created.

Mark.






Re: Visual C# Language Involving constructors...

OmegaMan

Mark Dawson wrote:

Hi,

not quite true, C# will provide a default constructor as long as you have not defied any constructors yourself, as soon as you define a constructor then the default constructor will not automatically be created.

Mark.



Mark is right....to sum up
  1. If there are no constructors defined, C# creates one with no parameters, as a default and will initialize all items to zero. A call to a default constructor in this case will not fail. (Instance Constructors) C#
  2. Once the user defines a constructor that has parameters, or not, the default one is not generated and if a call to the no parameter constructor will fail if the user has not defined one.
See Using Constructors (C# Programming Guide) for more info.





Re: Visual C# Language Involving constructors...

razored

theTroll527 wrote:
razored wrote:

OmegaMan wrote:
You must supply the no argument constructor such as

public Quad() {}

C# does not provide a default type constructor.

I don't understand what you mean. That is exactly what I did, public Quad(stuff) {stuff}. Please elucidate.



You created a constructor that took arguments.

public Quad ()
{

}

That is a constructor that uses no arguments. That should be what you need.

theTroll


But then again, what if in a case, I wanted to fill in arguments; however, that constructor would not allow any arguments to be implemented I would get more errors then




Re: Visual C# Language Involving constructors...

OmegaMan

razored wrote:
But then again, what if in a case, I wanted to fill in arguments; however, that constructor would not allow any arguments to be implemented I would get more errors then


Once you have a class that has a constructor, there is no default constructor created....and hence the point is moot.

But if you then choose to have a constructor with no parameters and another constructor that takes parameters, it is up to you the developer to ensure that the class behaves as expected in such a situation. For example I would chain the constructor such as this for such a situation

public class TestMe
{
int _arg1;
int _arg2;
public TestMe(int arg1, int arg2) { _arg1 = arg1; _arg2 = arg2; }

public TestMe() : this(12, 15) {}

}

That way if someone creates a TestMe object, I have initialized the variables properly.