448,678 Members | 1,129 Online
Need help? Post your question and get tips & solutions from a community of 448,678 IT Pros & Developers. It's quick & easy.

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

 P: n/a 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 Nov 16 '05 #1
4 Replies

 P: n/a 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" wrote in message news:3d*************************@posting.google.co m... 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 Nov 16 '05 #2

 P: n/a 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." wrote in message news:... 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" wrote in message news:3d*************************@posting.google.co m... 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 Nov 16 '05 #3

 P: n/a 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" wrote in message news:3d**************************@posting.google.c om... 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." wrote in message news:... 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" wrote in message news:3d*************************@posting.google.co m... 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 Nov 16 '05 #4

 P: n/a 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. -&& Nov 16 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.