Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 13:41
Colin Angus Mackay25-Aug-08 13:41 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 13:48
Vodstok25-Aug-08 13:48 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 13:56
Colin Angus Mackay25-Aug-08 13:56 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 14:13
Vodstok25-Aug-08 14:13 
QuestionCan an object cast itself to a derived class? Pin
Togakangaroo25-Aug-08 10:32
Togakangaroo25-Aug-08 10:32 
AnswerRe: Can an object cast itself to a derived class? Pin
Colin Angus Mackay25-Aug-08 10:48
Colin Angus Mackay25-Aug-08 10:48 
GeneralRe: Can an object cast itself to a derived class? Pin
Togakangaroo25-Aug-08 11:00
Togakangaroo25-Aug-08 11:00 
AnswerRe: Can an object cast itself to a derived class? Pin
chaiguy133725-Aug-08 14:50
chaiguy133725-Aug-08 14:50 
I agree with Colin for the most part. It seems as though you are looking to use the class itself as a way to represent some information about the instance. Although as you mentioned this can work in C++, C# works very differently, and the type of an object really represents two things: first of all, the actual type of the instance, and secondly (in the case of base classes) a common interface (since subclassing is really just a way of implementing the base class's interface, from the perspective of someone using the object).

Anyhow, if you were to cast the instance of ref Book b to a CheckedOutBook inside CheckOut(), this would actually only apply to the local representation of b, and even though it's marked as ref, would not affect the object passed into it, namely because that object in the calling method already has an interpretation (type), and ref parameters allow an object to be changed, not how it is interpreted (i.e. its local apparent type).

As far as it being a "DON'T DO THIS EVER" thing, I think you'll quickly realize that it won't work, so it's not so much a warning as there's just no point in doing it.

Now that said, you might actually be able to do something like what you want by creating a new object of type CheckedOutBook, setting it as somehow equivalent to the passed-in Book, and then setting the ref b parameter to equal the new instance (since it is still a Book). The key is that you will have to create a new object, and the old one will still be floating around so long as it is used (elsewhere). However you appear to be aware of this scenario.

The short answer is no, you cannot "transform" a reference to be some type other than how it was created. Not in C#.

“Time and space can be a bitch.”
–Gushie, Quantum Leap

{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’)            Built with home-grown CodeProject components!
-”-”-

QuestionHow can I add a button here ? Pin
Mohammad Dayyan25-Aug-08 10:01
Mohammad Dayyan25-Aug-08 10:01 
AnswerRe: How can I add a button here ? Pin
DaveyM6925-Aug-08 10:19
professionalDaveyM6925-Aug-08 10:19 
AnswerRe: How can I add a button here ? Pin
Thomas Stockwell26-Aug-08 2:16
professionalThomas Stockwell26-Aug-08 2:16 
QuestionPassing array to operator overload Pin
DaveyM6925-Aug-08 9:48
professionalDaveyM6925-Aug-08 9:48 
AnswerRe: Passing array to operator overload Pin
DaveyM6925-Aug-08 10:21
professionalDaveyM6925-Aug-08 10:21 
GeneralRe: Passing array to operator overload Pin
chaiguy133725-Aug-08 14:53
chaiguy133725-Aug-08 14:53 
QuestionEnum / Cast Question Pin
Kevin Marois25-Aug-08 7:56
professionalKevin Marois25-Aug-08 7:56 
AnswerRe: Enum / Cast Question Pin
User 665825-Aug-08 8:12
User 665825-Aug-08 8:12 
AnswerRe: Enum / Cast Question Pin
TheFM23425-Aug-08 8:22
TheFM23425-Aug-08 8:22 
AnswerRe: Enum / Cast Question Pin
Mark Salsbery25-Aug-08 8:22
Mark Salsbery25-Aug-08 8:22 
AnswerRe: Enum / Cast Question Pin
DaveyM6925-Aug-08 9:03
professionalDaveyM6925-Aug-08 9:03 
QuestionRe: Enum / Cast Question Pin
chaiguy133725-Aug-08 14:55
chaiguy133725-Aug-08 14:55 
AnswerRe: Enum / Cast Question Pin
N a v a n e e t h25-Aug-08 18:13
N a v a n e e t h25-Aug-08 18:13 
AnswerRe: Enum / Cast Question Pin
DaveyM6925-Aug-08 23:10
professionalDaveyM6925-Aug-08 23:10 
GeneralRe: Enum / Cast Question Pin
chaiguy133726-Aug-08 4:42
chaiguy133726-Aug-08 4:42 
Questionhow to drag and drop control on the form? Pin
mohamad108825-Aug-08 6:56
mohamad108825-Aug-08 6:56 
QuestionHosting Pin
ellllllllie25-Aug-08 5:12
ellllllllie25-Aug-08 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.