0

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

1

2 Answers 2

2

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

If the classes are within two different assemblies, then create a 3rd project (Class Library) to hold these common classes and reference this class library from both the web service and console app.
Yes, that's the right way to go. And in addition, if these needs to be sent over web servers and the like, serialization will likely be needed as well.
Yes , You are correct. WebService + Console application both are invoking same dll (namespace + class) AutoProvisionWebAPIClient.AutoProvisionTicketsResponse , but when i tried to pass "vvv" to the ss.GetAllTickets(vvv) method. I am getting error. Thats the reason, I created new instance for WebService class like AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse APTR = new AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse();
Error 56 Argument 1: cannot convert from 'AutoProvisionWebAPIClient.AutoProvisionTicketsResponse' to 'AutoProvisionController.SRMUserRegServiceReference.AutoProvisionTicketsResponse'
Like we said, it is because those two classes are different despite having same attributes. You need to have one common Namespace.Class, and then both projects need to reference it.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.