userde
userde

Reputation: 11

cannot implicitly convert type class to another class C#

Console Application : I have one object which has all ticket details, I am trying to pass this object through webservice.

Basically, I want assign "VVV" (list of ticket details values) to "APTR" object then, i will pass "APTR" to the method.

Console Application :

   public string SendAllticketDetailsToService(AutoProvisionWebAPIClient.AutoProvisionTicketsResponse vvv)
    {                         
        // bool result = false;
        string xmlStringResult;
        xmlStringResult = ""; 
        try
        {        
            AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse APTR = new AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse();

I want assign vvv class object values to APTR, Can you guide me ?

            xmlStringResult = ss.GetAllTickets(APTR);

            Console.WriteLine(xmlStringResult); 
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.Message);
        }
       return xmlStringResult;           
    }
    //end 

Both are different namespaces but the same class and attributes.. when i tried to compile, i am getting the error like

Error 55 Cannot implicitly convert type 'AutoProvisionWebAPIClient.AutoProvisionTicketsResponse' to 'AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse' \ARMWebAPIClient\AutoProvisionController\SRMUserRegServiceProxy.cs 44 24 AutoProvisionController

Upvotes: 0

Views: 3918

Answers (2)

Sach
Sach

Reputation: 10393

If they are two classes within two different namespaces, they are essentially treated as two different classes even if they have the same attributes and methods.

You should use a common namespace to define the class, and use it instead.

EDIT

If you MUST use two different namespaces (although I don't see why), you could individually copy attributes of one class objects to the other, which is the same idea as the one suggested in the other answer.

namespace NamespaceOne
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

namespace NamespaceTwo
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

namespace MainNamespace
{
    class Program
    {
        public static void Main()
        {
            NamespaceOne.Person p1 = new NamespaceOne.Person() { FirstName = "Zaphod", LastName = "Beeblebrox" };
            NamespaceTwo.Person p2 = new NamespaceTwo.Person() { FirstName = p1.FirstName, LastName = p1.LastName };

            Console.ReadLine();
        }
    }
}

Again, this is really pointless and I advice against it. Correct approach is to have just one common namespace in which your class (in this case, Person) resides, and use it when needed. Like so:

namespace CommonNamespace
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

namespace MainNamespace
{
    class Program
    {
        public static void Main()
        {
            CommonNamespace.Person p1 = new CommonNamespace.Person() { FirstName = "Zaphod", LastName = "Beeblebrox" };
            CommonNamespace.Person p2 = p1;

            Console.ReadLine();
        }
    }
}

Upvotes: 2

Jon Hanna
Jon Hanna

Reputation: 113392

Both are different namespaces but the same class and attributes

That means both are completely different classes.

One solution is to have a a single class (perhaps in yet another namespace both assemblies use) for both cases.

Another is to create a translation method that creates takes an AutoProvisionWebAPIClient.AutoProvisionTicketsResponse and returns an AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse built by examining the argument's members and assigning to the created object.

Possibly this could be a casting operator defined in one of those casts, which would allow a cast rather than a method call to be used.

Upvotes: 0

Related Questions