Type casting in classes

G

Guest

Hi,
I have a question regarding the typecasting.

I have a class A with some properties and methods... I also have a copy of
same class as class B. Class A and B are having every thing same just a name
change and namespace change.

Now I want to have a code like this

A a = new A();
B b= new B();

b= a;
obviously this will give me error..

so I am writing b = (B) a;

This also gives me type cast error. how ever I assume that I need to write
some code in both classes so that they can be converted to each other. I am
thinking to serilize and de-serilize but this would be unnecessary overhead.
I am sure there must be some way exists which will allow me to do this.

Regards,
Kishor
 
A

Arunkumar Keserla

Not sure what you are trying to fully, however you can use (abstract)baseclass
or interfaces to do assignments. Classes are supposed to be templates and
if you have something like name & namespace change your classes still stay
the same. Only the instances are different, anyway here is a code snippet.

namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
ClassBase a = new ClassA();
ClassBase b = new ClassB();
//ClassB b = new ClassB();

b = (ClassB)a;
}
}

// this could be an interface or abstract class
internal class ClassBase
{
}

internal class ClassB : ClassBase
{
}

internal class ClassA : ClassBase

{
}
}

-Arun
 
J

Jon Skeet [C# MVP]

kishor said:
I have a question regarding the typecasting.

I have a class A with some properties and methods... I also have a copy of
same class as class B. Class A and B are having every thing same just a name
change and namespace change.

Now I want to have a code like this

A a = new A();
B b= new B();

b= a;
obviously this will give me error..

so I am writing b = (B) a;

This also gives me type cast error. how ever I assume that I need to write
some code in both classes so that they can be converted to each other. I am
thinking to serilize and de-serilize but this would be unnecessary overhead.
I am sure there must be some way exists which will allow me to do this.

Well, if possible I'd try to get rid of the duplication. However, if
you can't do this, just write a ToA method in B, and a ToB method in A.

And no, generics wouldn't make any difference.
 
P

Peter Duniho

Jon said:
kishor said:
I have a question regarding the typecasting.

I have a class A with some properties and methods... I also have a copy of
same class as class B. Class A and B are having every thing same just a name
change and namespace change.
[...]

Well, if possible I'd try to get rid of the duplication.

And I'll second that.

It's not clear from your post why you've simply copied one class to
somewhere else, especially given that it seems you want them to be used
as equivalent to each other.

A better solution than duplicating the class in both namespaces and then
having to write conversion methods would be to declare the class as
public in one namespace and then refer to it from the other (or put the
class into yet another namespace that is referred to by both places that
want to use the class...this would be especially appropriate if you
anticipate having even more places where you use the class).

If there's some particular reason you think the class needs to be
duplicated, perhaps you could try to explain that. Then we can try to
talk you out of that with more specific details. :)

One of my primary rules of coding: if you find yourself copying and
pasting code in anything except in the most trivial one (_maybe_ two)
line situations, you've almost always done something wrong. :)

Pete
 
G

Guest

Hi,
I understand that there is some unclearity in my post. I am telling you
actual problem in detailed manner.

I have a solution which contains multiple C# projects which includes
1 class library (Entity)
2 WebService (Some business logic)
3 windows application (or console or any thing)

Webservice has reference to entity project and winapp has reference to
webservices as well as entity project.

Now say I have a class A in entity project. which is accessible in
webservice as well as in winapp.

In win App application I can write a ClassA a = new ClassA();
in webservice also I can create ClassA wa = new ClassA();
in WebService I have a webmethod method which returns a entity of type
classA.

[webmethod]
public classA method1()
{

ClassA wa = new ClassA();
.... some code
retuurns wa;

}

now in calling application ie winApp or console I am creating a object like
this

ClassA a = new ClassA();

a = webservice....method1();

since entity is same logically it should be possible but; since
webservice....method1(); entity is coming from webservices it has a name
space of webservices and throwing type caste error.


I can actually solve a problem by creating a instance of proxy class .. but
dont want to do that way.

It should be converted as it is. I hope it is clear now.

Regards,
Kishor
 
P

Peter Duniho

kishor said:
[...]
since entity is same logically it should be possible but; since
webservice....method1(); entity is coming from webservices it has a name
space of webservices and throwing type caste error.


I can actually solve a problem by creating a instance of proxy class .. but
dont want to do that way.

It should be converted as it is. I hope it is clear now.

It's still not entirely clear to me. I'm up too late. But, can't you
just qualify the class name as used in the non-entity namespaces, to
refer directly to the entity namespace? That way, the class always
comes from the original namespace?

Barring that, if you could put together a concise-but-complete example
of code that demonstrates the issue, that would be helpful. I think you
should be able to put together a console application with multiple
namespaces that exhibits the same issue, right?

Pete
 
G

Guest

Hi all,
Here I can tell you one more thing all projects are having only one
namespaces.
I even tried using qualified names for namespaces.

Regards,
Kishor






Peter Duniho said:
kishor said:
[...]
since entity is same logically it should be possible but; since
webservice....method1(); entity is coming from webservices it has a name
space of webservices and throwing type caste error.


I can actually solve a problem by creating a instance of proxy class .. but
dont want to do that way.

It should be converted as it is. I hope it is clear now.

It's still not entirely clear to me. I'm up too late. But, can't you
just qualify the class name as used in the non-entity namespaces, to
refer directly to the entity namespace? That way, the class always
comes from the original namespace?

Barring that, if you could put together a concise-but-complete example
of code that demonstrates the issue, that would be helpful. I think you
should be able to put together a console application with multiple
namespaces that exhibits the same issue, right?

Pete
 
C

Chris Dunaway

kishor said:
[...]
since entity is same logically it should be possible but; since
webservice....method1(); entity is coming from webservices it has a name
space of webservices and throwing type caste error.
I can actually solve a problem by creating a instance of proxy class .. but
dont want to do that way.
It should be converted as it is. I hope it is clear now.

It's still not entirely clear to me. I'm up too late. But, can't you
just qualify the class name as used in the non-entity namespaces, to
refer directly to the entity namespace? That way, the class always
comes from the original namespace?

Barring that, if you could put together a concise-but-complete example
of code that demonstrates the issue, that would be helpful. I think you
should be able to put together a console application with multiple
namespaces that exhibits the same issue, right?

Pete

I think the problem is caused by the proxy that is generated for the
web service when it is referenced. The web methods return instances
of the proxy class.

To solve the problem, just change the proxy class to return an
instance of ClassA. You would have to remove the auto-generated code
of the proxy and replace it with code to return the correct class.

Hope this makes sense.
 
G

Guest

It really looks to me like you want to convert an instance of A to an
instance of B. If that is the case, then you need to write an implicit
conversion method in b that takes an argument of type A and returns an
instance of B.

For example:

public static implicit operator B(A a)
{
B b = new B();

b.prop1 = Convert.ToInt32(a.propX);
b.prop2 = a.propY.ToString();

return b;
}

Now when you cast the instance of a to type B, the implicit method will be
called and your b object will get the converted values based on the code you
wrote in the implicit operator method.
 
G

Guest

Hi Chris,
Yes I have tried modifying the proxy generated but it also failed to...Just
for time saving I will be putting my solution so that you all can have a look
at my code.


my observation is webservice has some limitation to pass some datatypes..I
think.

Regards,
Kishor



Chris Dunaway said:
kishor said:
[...]
since entity is same logically it should be possible but; since
webservice....method1(); entity is coming from webservices it has a name
space of webservices and throwing type caste error.
I can actually solve a problem by creating a instance of proxy class .. but
dont want to do that way.
It should be converted as it is. I hope it is clear now.

It's still not entirely clear to me. I'm up too late. But, can't you
just qualify the class name as used in the non-entity namespaces, to
refer directly to the entity namespace? That way, the class always
comes from the original namespace?

Barring that, if you could put together a concise-but-complete example
of code that demonstrates the issue, that would be helpful. I think you
should be able to put together a console application with multiple
namespaces that exhibits the same issue, right?

Pete

I think the problem is caused by the proxy that is generated for the
web service when it is referenced. The web methods return instances
of the proxy class.

To solve the problem, just change the proxy class to return an
instance of ClassA. You would have to remove the auto-generated code
of the proxy and replace it with code to return the correct class.

Hope this makes sense.
 
G

Guest

Hi,
Yes this could be an answer for the problem.. but I need to write such
methods for all classes....

Regards,
Kishor
 
G

Guest

Hi,
I have tried also .. this works in normal scenerio but in case of webservice
it fails.. and I get same error.

Kishor

Arunkumar Keserla said:
Not sure what you are trying to fully, however you can use (abstract)baseclass
or interfaces to do assignments. Classes are supposed to be templates and
if you have something like name & namespace change your classes still stay
the same. Only the instances are different, anyway here is a code snippet.

namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
ClassBase a = new ClassA();
ClassBase b = new ClassB();
//ClassB b = new ClassB();

b = (ClassB)a;
}
}

// this could be an interface or abstract class
internal class ClassBase
{
}

internal class ClassB : ClassBase
{
}

internal class ClassA : ClassBase

{
}
}

-Arun
Sorry forgetted to ask one thing.. will generics solves my problem ?
 

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

Similar Threads


Top