How to simulate Downcasting? 
Author Message
 How to simulate Downcasting?

How do I simulate downcasting in C#?

public class foo
{

Quote:
}

public class bar : foo
{

Quote:
}

foo f = new foo();
// The following line
// causes a run-time error.
// InvalidCastException
bar b = (bar)f;

************************
The following doesn't work either.
This gives a compile error.

 public class foo
 {
 }

 public class bar : foo
 {
      public bar(){}
      public bar(foo f)
      { this = f; }
      public static explicit operator bar(foo f)
      { return new bar(f); }
 }



Sun, 04 Jul 2004 07:18:48 GMT  
 How to simulate Downcasting?
If object "f" is not of type "bar", then you cannot put it in a variable of
type "bar".

If you made this change, then it would work...

Quote:
> foo f = new bar();

--
Jonathan Allen


Quote:
> How do I simulate downcasting in C#?

> public class foo
> {
> }

> public class bar : foo
> {
> }

> foo f = new foo();
> // The following line
> // causes a run-time error.
> // InvalidCastException
> bar b = (bar)f;

> ************************
> The following doesn't work either.
> This gives a compile error.

>  public class foo
>  {
>  }

>  public class bar : foo
>  {
>       public bar(){}
>       public bar(foo f)
>       { this = f; }
>       public static explicit operator bar(foo f)
>       { return new bar(f); }
>  }



Sun, 04 Jul 2004 08:18:56 GMT  
 How to simulate Downcasting?
This was an over-simplified example.  I realize that a new instance of
foo can be created from bar, but how can I convert an existing foo into
a bar?  It can be done in C++, it's dangerous, but it can be done.  I
realize that there will potentially be a loss of data doing a down-cast,
but for my purposes, that's ok.  I don't have any new methods or
properties defined in my derived class.  So a conversion would be
acceptable.  Any ideas?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 08:33:37 GMT  
 How to simulate Downcasting?
Better rethink your OOD principles. I'm sure there's another way of doing
whatever it is you're doing without downcasting. Since I'm sure that before
you asked your question, you already knew the answer, why not just complain
about it? Get to the point. "C# needs downcasting." It's not as simple as
C++ anyway. In C++ you simply had a pointer to a memory location, and that
was where the object instance resided. In C#, everything is managed. It's
not as simple as just accessing the data in memory you know is there.

Nate


Quote:
> This was an over-simplified example.  I realize that a new instance of
> foo can be created from bar, but how can I convert an existing foo into
> a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> realize that there will potentially be a loss of data doing a down-cast,
> but for my purposes, that's ok.  I don't have any new methods or
> properties defined in my derived class.  So a conversion would be
> acceptable.  Any ideas?

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 08:42:39 GMT  
 How to simulate Downcasting?
Perhaps you should implement an interface instead.

Nate


Quote:
> This was an over-simplified example.  I realize that a new instance of
> foo can be created from bar, but how can I convert an existing foo into
> a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> realize that there will potentially be a loss of data doing a down-cast,
> but for my purposes, that's ok.  I don't have any new methods or
> properties defined in my derived class.  So a conversion would be
> acceptable.  Any ideas?

> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 08:43:16 GMT  
 How to simulate Downcasting?
Unfortunately it's not my code so I don't have the luxury of rethinking my
OOD principles.  There is another way but it's equally ugly.  I don't
necessarily think C# needs downcasting, it should never really be used, I
was just wondering if it could be done.  And yes you can access memory in C#
by using the unsafe keyword.


Quote:
> Better rethink your OOD principles. I'm sure there's another way of doing
> whatever it is you're doing without downcasting. Since I'm sure that
before
> you asked your question, you already knew the answer, why not just
complain
> about it? Get to the point. "C# needs downcasting." It's not as simple as
> C++ anyway. In C++ you simply had a pointer to a memory location, and that
> was where the object instance resided. In C#, everything is managed. It's
> not as simple as just accessing the data in memory you know is there.

> Nate



> > This was an over-simplified example.  I realize that a new instance of
> > foo can be created from bar, but how can I convert an existing foo into
> > a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> > realize that there will potentially be a loss of data doing a down-cast,
> > but for my purposes, that's ok.  I don't have any new methods or
> > properties defined in my derived class.  So a conversion would be
> > acceptable.  Any ideas?

> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 08:56:27 GMT  
 How to simulate Downcasting?
Unfortunately it's not my code.  Otherwise that would be a good idea.


Quote:
> Perhaps you should implement an interface instead.

> Nate



> > This was an over-simplified example.  I realize that a new instance of
> > foo can be created from bar, but how can I convert an existing foo into
> > a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> > realize that there will potentially be a loss of data doing a down-cast,
> > but for my purposes, that's ok.  I don't have any new methods or
> > properties defined in my derived class.  So a conversion would be
> > acceptable.  Any ideas?

> > *** Sent via Developersdex http://www.developersdex.com ***
> > Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 08:57:00 GMT  
 How to simulate Downcasting?
Gerald,

Quote:
> This was an over-simplified example.  I realize that a new instance of
> foo can be created from bar, but how can I convert an existing foo into
> a bar?  It can be done in C++, it's dangerous, but it can be done.

True, which is one of the shinks in the C++ object model, as far as
typesafety goes. .NET, otoh, is much safer, so downcasts are always
controlled, since the real type of an object is always known at runtime.

Is there any particular reason why you need downcasting in your situation?
--
Tomas Restrepo



Sun, 04 Jul 2004 09:05:20 GMT  
 How to simulate Downcasting?
Please explain your situation and why you cannot change the code.

--
Jonathan Allen


Quote:
> Unfortunately it's not my code so I don't have the luxury of rethinking my
> OOD principles.  There is another way but it's equally ugly.  I don't
> necessarily think C# needs downcasting, it should never really be used, I
> was just wondering if it could be done.  And yes you can access memory in
C#
> by using the unsafe keyword.



> > Better rethink your OOD principles. I'm sure there's another way of
doing
> > whatever it is you're doing without downcasting. Since I'm sure that
> before
> > you asked your question, you already knew the answer, why not just
> complain
> > about it? Get to the point. "C# needs downcasting." It's not as simple
as
> > C++ anyway. In C++ you simply had a pointer to a memory location, and
that
> > was where the object instance resided. In C#, everything is managed.
It's
> > not as simple as just accessing the data in memory you know is there.

> > Nate



> > > This was an over-simplified example.  I realize that a new instance of
> > > foo can be created from bar, but how can I convert an existing foo
into
> > > a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> > > realize that there will potentially be a loss of data doing a
down-cast,
> > > but for my purposes, that's ok.  I don't have any new methods or
> > > properties defined in my derived class.  So a conversion would be
> > > acceptable.  Any ideas?

> > > *** Sent via Developersdex http://www.developersdex.com ***
> > > Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 09:31:47 GMT  
 How to simulate Downcasting?
It's in a .net assembly/dll.


Quote:
> Please explain your situation and why you cannot change the code.

> --
> Jonathan Allen



> > Unfortunately it's not my code so I don't have the luxury of rethinking
my
> > OOD principles.  There is another way but it's equally ugly.  I don't
> > necessarily think C# needs downcasting, it should never really be used,
I
> > was just wondering if it could be done.  And yes you can access memory
in
> C#
> > by using the unsafe keyword.



> > > Better rethink your OOD principles. I'm sure there's another way of
> doing
> > > whatever it is you're doing without downcasting. Since I'm sure that
> > before
> > > you asked your question, you already knew the answer, why not just
> > complain
> > > about it? Get to the point. "C# needs downcasting." It's not as simple
> as
> > > C++ anyway. In C++ you simply had a pointer to a memory location, and
> that
> > > was where the object instance resided. In C#, everything is managed.
> It's
> > > not as simple as just accessing the data in memory you know is there.

> > > Nate



> > > > This was an over-simplified example.  I realize that a new instance
of
> > > > foo can be created from bar, but how can I convert an existing foo
> into
> > > > a bar?  It can be done in C++, it's dangerous, but it can be done.
I
> > > > realize that there will potentially be a loss of data doing a
> down-cast,
> > > > but for my purposes, that's ok.  I don't have any new methods or
> > > > properties defined in my derived class.  So a conversion would be
> > > > acceptable.  Any ideas?

> > > > *** Sent via Developersdex http://www.developersdex.com ***
> > > > Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 09:43:48 GMT  
 How to simulate Downcasting?
I know about the unsafe keyword, heh. I was refering to variables.

Nate


Quote:
> Unfortunately it's not my code so I don't have the luxury of rethinking my
> OOD principles.  There is another way but it's equally ugly.  I don't
> necessarily think C# needs downcasting, it should never really be used, I
> was just wondering if it could be done.  And yes you can access memory in
C#
> by using the unsafe keyword.



> > Better rethink your OOD principles. I'm sure there's another way of
doing
> > whatever it is you're doing without downcasting. Since I'm sure that
> before
> > you asked your question, you already knew the answer, why not just
> complain
> > about it? Get to the point. "C# needs downcasting." It's not as simple
as
> > C++ anyway. In C++ you simply had a pointer to a memory location, and
that
> > was where the object instance resided. In C#, everything is managed.
It's
> > not as simple as just accessing the data in memory you know is there.

> > Nate



> > > This was an over-simplified example.  I realize that a new instance of
> > > foo can be created from bar, but how can I convert an existing foo
into
> > > a bar?  It can be done in C++, it's dangerous, but it can be done.  I
> > > realize that there will potentially be a loss of data doing a
down-cast,
> > > but for my purposes, that's ok.  I don't have any new methods or
> > > properties defined in my derived class.  So a conversion would be
> > > acceptable.  Any ideas?

> > > *** Sent via Developersdex http://www.developersdex.com ***
> > > Don't just participate in USENET...get rewarded for it!



Sun, 04 Jul 2004 12:14:02 GMT  
 How to simulate Downcasting?
Obviously the author didn't mean for you to do what you are trying to do, so
what reason do you have for trying to compromise the design. Please be
specific.

--
Jonathan Allen


Quote:
> It's in a .net assembly/dll.



> > Please explain your situation and why you cannot change the code.

> > --
> > Jonathan Allen



> > > Unfortunately it's not my code so I don't have the luxury of
rethinking
> my
> > > OOD principles.  There is another way but it's equally ugly.  I don't
> > > necessarily think C# needs downcasting, it should never really be
used,
> I
> > > was just wondering if it could be done.  And yes you can access memory
> in
> > C#
> > > by using the unsafe keyword.



Sun, 04 Jul 2004 15:41:05 GMT  
 How to simulate Downcasting?
Yes, you are correct.  I didn't necessarily want to get into a debate about
whether or not downcasting is bad.  I know it's bad.  I just wanted to know
IF it could be done in C#.

Thanks for your responses.

-Gerald


Quote:
> Obviously the author didn't mean for you to do what you are trying to do,
so
> what reason do you have for trying to compromise the design. Please be
> specific.

> --
> Jonathan Allen



> > It's in a .net assembly/dll.



> > > Please explain your situation and why you cannot change the code.

> > > --
> > > Jonathan Allen



> > > > Unfortunately it's not my code so I don't have the luxury of
> rethinking
> > my
> > > > OOD principles.  There is another way but it's equally ugly.  I
don't
> > > > necessarily think C# needs downcasting, it should never really be
> used,
> > I
> > > > was just wondering if it could be done.  And yes you can access
memory
> > in
> > > C#
> > > > by using the unsafe keyword.



Mon, 05 Jul 2004 01:04:52 GMT  
 How to simulate Downcasting?
That's what I thought.  Thanks Tomas.


Quote:
> Gerald,

> > This was an over-simplified example.  I realize that a new instance of
> > foo can be created from bar, but how can I convert an existing foo into
> > a bar?  It can be done in C++, it's dangerous, but it can be done.

> True, which is one of the shinks in the C++ object model, as far as
> typesafety goes. .NET, otoh, is much safer, so downcasts are always
> controlled, since the real type of an object is always known at runtime.

> Is there any particular reason why you need downcasting in your situation?
> --
> Tomas Restrepo




Mon, 05 Jul 2004 01:06:26 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Downcasting

2. Question about using GetDlgItem & other downcasting

3. Simulating include files

4. simulate xp's selection tool

5. Simulating C++ destructors

6. simulated diffusion

7. simulating shuffling a deck of cards

8. How can I simulate a function call?

9. Simulating Keystroks in a TextBox ?

10. Simulating http headers

11. simulate dragDrop of ListView Item -- Please Please Help!!!!

12. Formating Input :: simulating 2 prompts on 1 screen

 

 
Powered by phpBB® Forum Software