PC Review


Reply
 
 
juli jul
Guest
Posts: n/a
 
      28th Sep 2005
Hello,
I want to do casting of class A object to class B object.
How exactly can I do it in c#?
The casting I need is: The client will do a casting to a class instance
and I will cast it to another class instance with proper changes.
How to do it?
Thanks!




*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      28th Sep 2005
"juli jul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello,
> I want to do casting of class A object to class B object.
> How exactly can I do it in c#?
> The casting I need is: The client will do a casting to a class instance
> and I will cast it to another class instance with proper changes.
> How to do it?
> Thanks!
>


How are the classes related:

1. the one the client receives
2. the one they cast it to
3. the one you will cast it to

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

 
Reply With Quote
 
=?Utf-8?B?dGJhaW4=?=
Guest
Posts: n/a
 
      28th Sep 2005
If I understand you correctly: ((B)oA).<property or method>
But I would use the "as" operator instead and then test for null:

B oB = oA as B;
if (oB != null)
{
<do something with oB>
}

Hope this helps.
--
Thom


"juli jul" wrote:

> Hello,
> I want to do casting of class A object to class B object.
> How exactly can I do it in c#?
> The casting I need is: The client will do a casting to a class instance
> and I will cast it to another class instance with proper changes.
> How to do it?
> Thanks!
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
>

 
Reply With Quote
 
=?Utf-8?B?dGJhaW4=?=
Guest
Posts: n/a
 
      28th Sep 2005
I forgot to state the obvious, A and B should be related in some way like
derived from the same class or interface or each other.
--
Thom


"tbain" wrote:

> If I understand you correctly: ((B)oA).<property or method>
> But I would use the "as" operator instead and then test for null:
>
> B oB = oA as B;
> if (oB != null)
> {
> <do something with oB>
> }
>
> Hope this helps.
> --
> Thom
>
>
> "juli jul" wrote:
>
> > Hello,
> > I want to do casting of class A object to class B object.
> > How exactly can I do it in c#?
> > The casting I need is: The client will do a casting to a class instance
> > and I will cast it to another class instance with proper changes.
> > How to do it?
> > Thanks!
> >
> >
> >
> >
> > *** Sent via Developersdex http://www.developersdex.com ***
> >

 
Reply With Quote
 
Jeti [work]
Guest
Posts: n/a
 
      29th Sep 2005
> B oB = oA as B;
> if (oB != null)
> {
> <do something with oB>
> }
>



OR

if (oA is B)
{
(oA as B).Some_B_Method(...);
}




--
hello world


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Sep 2005
Jeti [work] <(E-Mail Removed)> wrote:
> > B oB = oA as B;
> > if (oB != null)
> > {
> > <do something with oB>
> > }

>
> OR
>
> if (oA is B)
> {
> (oA as B).Some_B_Method(...);
> }


The advantage to the former, of course, is that the cast/check only
gets done once.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Mark Wilden
Guest
Posts: n/a
 
      30th Sep 2005
> > B oB = oA as B;
> > if (oB != null)
> > {
> > <do something with oB>
> > }


I would prefer

((B oA).<do something>

as it will throw a meaningful exception if oA is not a B, instead of
doing nothing.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Sep 2005
Mark Wilden <(E-Mail Removed)> wrote:
> > > B oB = oA as B;
> > > if (oB != null)
> > > {
> > > <do something with oB>
> > > }

>
> I would prefer
>
> ((B oA).<do something>
>
> as it will throw a meaningful exception if oA is not a B, instead of
> doing nothing.


Yes - so would I, very definitely, *if* it really should be that type.
(That's actually the more common case at the moment, I find - generics
may well change that though.)

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Down-casting of Typed Collection (Casting Generic Types?) conchur Microsoft C# .NET 1 3rd Jul 2006 11:45 AM
casting juli jul Microsoft C# .NET 3 19th Oct 2005 06:08 PM
Need help with casting Keith Selbee Microsoft C# .NET 6 7th May 2004 08:12 AM
Casting!!!?!?!?!? André Almeida Maldonado Microsoft ASP .NET 1 14th Jan 2004 06:44 PM
Re: Casting Gregory Persson Microsoft C# .NET 0 7th Aug 2003 03:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 AM.