error CS0553: user-defined conversion to/from base class


B

bsa

Hi,

This maybe a stupid question for which I apologies, but I couldn't
find a solutions so I'll ask.
I'm a newbie and I'm trying to do the following:

A have a base class called space that should accommodate all the
common operation over a dimensional object - vector, vertex or matrix.


public class Space
{
protected double [] data;
protected int columns;
protected int rows;

public Space( int _rows, int _columns )
{
rows = _rows;
columns = _columns;
data = new double[ rows * columns ];
}
public int Columns
{
get { return columns; }
}
public int Rows
{
get { return rows; }
}
public static Space operator +( Space rl, Space rr)
{
return new Space( rl.Rows, rl.Columns );
}
}

I also have a Vector class inherited from Space


public class Vector : Space
{
public Vector( ) : base( 1, 3 )
{
}
public static implicit operator Vector ( Space s )
{
return new Vector( s );
}
}

And I'm trying to use it as:

Vector v1 = new Vector( );
Vector v2 = new Vector( );

Vector v3 = v1 + v2;

And here I get the mentioned error CS0553.
How I should deal when I'm in need to implicitly convert one base
object to the inherited one?

Do you any of you have an idea how this could be solved?

thanks,
bsa
 
Ad

Advertisements

N

NaraendiraKumar R. R.

I think it is saying you don't need to implement the below function in the
derived class. C# inherently provides this for you.

public static implicit operator Vector ( Space s )
{
return new Vector( s );
}
 
B

bsa

But not implementing this function, the code is still not compiled
because of error CS0029: Cannot implicitly convert type.
Which means although the compiler provide this function it doesn't use
it !

bsa

NaraendiraKumar R. R. said:
I think it is saying you don't need to implement the below function in the
derived class. C# inherently provides this for you.

public static implicit operator Vector ( Space s )
{
return new Vector( s );
}


---
bsa said:
Hi,

This maybe a stupid question for which I apologies, but I couldn't
find a solutions so I'll ask.
I'm a newbie and I'm trying to do the following:

A have a base class called space that should accommodate all the
common operation over a dimensional object - vector, vertex or matrix.


public class Space
{
protected double [] data;
protected int columns;
protected int rows;

public Space( int _rows, int _columns )
{
rows = _rows;
columns = _columns;
data = new double[ rows * columns ];
}
public int Columns
{
get { return columns; }
}
public int Rows
{
get { return rows; }
}
public static Space operator +( Space rl, Space rr)
{
return new Space( rl.Rows, rl.Columns );
}
}

I also have a Vector class inherited from Space


public class Vector : Space
{
public Vector( ) : base( 1, 3 )
{
}
public static implicit operator Vector ( Space s )
{
return new Vector( s );
}
}

And I'm trying to use it as:

Vector v1 = new Vector( );
Vector v2 = new Vector( );

Vector v3 = v1 + v2;

And here I get the mentioned error CS0553.
How I should deal when I'm in need to implicitly convert one base
object to the inherited one?

Do you any of you have an idea how this could be solved?

thanks,
bsa
 
N

NaraendiraKumar R.R.

To solve that error, you will have to do this ...
Vector v3 = (Vector)(v1 + v2);

OR

you can override the + operator in your Vector class.

-Naraen


---
bsa said:
But not implementing this function, the code is still not compiled
because of error CS0029: Cannot implicitly convert type.
Which means although the compiler provide this function it doesn't use
it !

bsa

"NaraendiraKumar R. R." <[email protected]> wrote in message
I think it is saying you don't need to implement the below function in the
derived class. C# inherently provides this for you.

public static implicit operator Vector ( Space s )
{
return new Vector( s );
}


---
bsa said:
Hi,

This maybe a stupid question for which I apologies, but I couldn't
find a solutions so I'll ask.
I'm a newbie and I'm trying to do the following:

A have a base class called space that should accommodate all the
common operation over a dimensional object - vector, vertex or matrix.


public class Space
{
protected double [] data;
protected int columns;
protected int rows;

public Space( int _rows, int _columns )
{
rows = _rows;
columns = _columns;
data = new double[ rows * columns ];
}
public int Columns
{
get { return columns; }
}
public int Rows
{
get { return rows; }
}
public static Space operator +( Space rl, Space rr)
{
return new Space( rl.Rows, rl.Columns );
}
}

I also have a Vector class inherited from Space


public class Vector : Space
{
public Vector( ) : base( 1, 3 )
{
}
public static implicit operator Vector ( Space s )
{
return new Vector( s );
}
}

And I'm trying to use it as:

Vector v1 = new Vector( );
Vector v2 = new Vector( );

Vector v3 = v1 + v2;

And here I get the mentioned error CS0553.
How I should deal when I'm in need to implicitly convert one base
object to the inherited one?

Do you any of you have an idea how this could be solved?

thanks,
bsa
 
Ad

Advertisements

G

Guest

If it's any consolation, I came up with exactly your situation in an application quite similar to yours and tried the same solution (creating an implicit conversion from the base class to the child class) which resulted in the CS0553 error. I had a general Vector class and wanted to create a special case of it Vector3D. In doing this, none of the operators on the base class could be used to implicitly return values of the derived class, and I ended up writing all over again (albeit simpler) them in the Vector3D class.

It wouldn't have been so bad, except that Microsoft, in its help on this particular error message said that I don't need such a thing. Silly me.

-&&
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top