Search code examples
c#objectnamespacesassign

.Net Conversion error CS0029 and CS0266 when trying to assign class


This question was raised by my son, and I could not give a precise answer.

We get an error when converting/mapping 2 identical classes but in a different namespace. I have created 1 class Astronaut and put the class in 2 different namespaces.

When you try to assign c = a we get this error? This is a very basic example, the real issue happens with 2 classes that have multiple classes inside and would be complex to write a mapper for.

For the record I fixed this using Automapper, just wanted to know the actual reason why this is not working.

using Apollo;
using Challenger;
void Main()
{
    var a = new Apollo.Astronaut();
    var b = new Challenger.Astronaut();
    // Cannot implicitely convert type 'Challenger.Astronaut' to 'Apollo.Astronaut'     
    var c = a;
        
    c = b; 
}

namespace Apollo{
    public class Astronaut
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    } 
}  

namespace Challenger{
     public class Astronaut
     {
         public string FirstName { get; set; }
         public string LastName { get; set; }
     }
 }

Solution

  • If you need two different Astronaut types you need to either define a base class or make an interface that they both implement.

    Base class:

    namespace Space
    {
        public class Astronaut
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        } 
    }
    
    namespace Apollo
    {
        public class Astronaut : Space.Astronaut
        {
        } 
    }  
    
    namespace Challenger
    {
        public class Astronaut : Space.Astronaut
        {
        } 
    }
    
    void Main()
    {
        Space.Astronaut a = new Apollo.Astronaut();
        Space.Astronaut b = new Challenger.Astronaut();
        var c = a;
        c = b; 
    }
    
    

    Interface

    namespace Space
    {
        public interface IAstronaut
        {
            string FirstName { get; set; }
            string LastName { get; set; }
        } 
    }
    
    namespace Apollo
    {
        public class Astronaut : Space.IAstronaut
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        } 
    }  
    
    namespace Challenger
    {
         public class Astronaut : Space.IAstronaut
         {
             public string FirstName { get; set; }
             public string LastName { get; set; }
         }
    }
    
    void Main()
    {
        IAstronaut a = new Apollo.Astronaut();
        IAstronaut b = new Challenger.Astronaut();
        var c = a;
        c = b; 
    }
    

    But I don't think it's the right way to go. An astronaut is an astronaut no matter the mission. So you should define one Astronaut class and a Mission class that defines a mission and who went on it. And by the way, you are not trying to "assign class". You are trying to assign an instance of one class to an instance of another. It very important to distinguish between classes and instances.