Overload constructor

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hello,

I have a class with two constructors:

public MyClass {
public MyClass(int i) {...}
public MyClass(char c) {...}
}

If i use second constructor while creating an object, compiler gives an
error:
'The best overloaded method match for 'MyClass(int i)' has some invalid
arguments'
And second error:
'cannot convert from char to int'

Where is an error?

Thanky you,
Andrey
 
MuZZy said:
Hello,

I have a class with two constructors:

public MyClass {
public MyClass(int i) {...}
public MyClass(char c) {...}
}

If i use second constructor while creating an object, compiler gives an
error:
'The best overloaded method match for 'MyClass(int i)' has some invalid
arguments'
And second error:
'cannot convert from char to int'

Where is an error?

Thanky you,
Andrey
 
I believe the problem is with the code which is responsible for
consuming your class. The following class:

public class Test
{
public Test(int i)
{
Console.WriteLine("int");
}

public Test(char c)
{
Console.WriteLine("char");
}
}

Can be consumed without any problems in the following ways

Test t1 = new Test(10);
Test t2 = new Test('a');

This code was tested in a console application and was able to
run without any compile- and runtime errors.

Hope this helps,

//Andreas
 
Sorry, i posted the question before thoroughly tracking for errors:
I had an error with typecasting.
Now it's working fine

Thank you,
Andrey
 
Back
Top