Override new

  • Thread starter Thread starter Hugo Wetterberg
  • Start date Start date
Hugo,

You can't do this. It would break all sorts of things. What if you had
another type, C, which derived from A? B and C would be incompatable, and
what you are trying to do would break, big time.

What you want to do is create a class factory, which would be a method
which will create the type for you, and return a common base type.

Hope this helps.
 
Hi Nicholas,
If I have...

A:object
B:A
C:A

In what way is B more incompatible with C than A is with C? Either A nor B
can be cast to C.
I don't want to make it a general rule to replace all classes with classes
that are derived from them, that might cause som problems.

What i have is a 1 to 1 mapping between specific types and types that
extends them. This mapping is made at run-time and is not known at design
time.

For a more detailed description see:
http://groups.google.se/[email protected]&frame=off
and:
http://www.codeproject.com/useritems/scripting.asp

This is somehow done in remoting. A proxy object is generated and used
instead of the real object. Can this be done in C#, or is it strictly magic
that works for things that are hard-coded into the framework?

/Hugo



Nicholas Paldino said:
Hugo,

You can't do this. It would break all sorts of things. What if you
had another type, C, which derived from A? B and C would be incompatable,
and what you are trying to do would break, big time.

What you want to do is create a class factory, which would be a method
which will create the type for you, and return a common base type.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hugo Wetterberg said:
Hi all,
I recently asked about how to intercept object creation and supply an
object that can be cast into the requested object.

If B extends A, when new A() is called an instance of B should be
returned.
To get an idea of what I'm trying to do and why, please read
http://www.codeproject.com/useritems/scripting.asp

/Hugo
 
Huge,

If you have the class heiarchy that you specified, then B is not able to
be cast to C. Try it. =)

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hugo Wetterberg said:
Hi Nicholas,
If I have...

A:object
B:A
C:A

In what way is B more incompatible with C than A is with C? Either A nor B
can be cast to C.
I don't want to make it a general rule to replace all classes with classes
that are derived from them, that might cause som problems.

What i have is a 1 to 1 mapping between specific types and types that
extends them. This mapping is made at run-time and is not known at design
time.

For a more detailed description see:
http://groups.google.se/[email protected]&frame=off
and:
http://www.codeproject.com/useritems/scripting.asp

This is somehow done in remoting. A proxy object is generated and used
instead of the real object. Can this be done in C#, or is it strictly
magic that works for things that are hard-coded into the framework?

/Hugo



Nicholas Paldino said:
Hugo,

You can't do this. It would break all sorts of things. What if you
had another type, C, which derived from A? B and C would be
incompatable, and what you are trying to do would break, big time.

What you want to do is create a class factory, which would be a method
which will create the type for you, and return a common base type.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hugo Wetterberg said:
Hi all,
I recently asked about how to intercept object creation and supply an
object that can be cast into the requested object.

If B extends A, when new A() is called an instance of B should be
returned.
To get an idea of what I'm trying to do and why, please read
http://www.codeproject.com/useritems/scripting.asp

/Hugo
 
class A
{
}

class B : A
{
double x;
public double X{ get{ return x;} }
}

class C:A
{
string foo = "blah";
public void Print()
{
Console.WriteLine(foo);
}
}

so

in what way are B and C compatible - if I instantiate a B then it has a double member in its allocated memory. if I create a C it has a string reference. The memory layoput of these two objects is different as are their method tables. However, they are both compatible with A in that they can both be treated as an A.

Regards

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

Hi Nicholas,
If I have...

A:object
B:A
C:A

In what way is B more incompatible with C than A is with C? Either A nor B
can be cast to C.
I don't want to make it a general rule to replace all classes with classes
that are derived from them, that might cause som problems.

What i have is a 1 to 1 mapping between specific types and types that
extends them. This mapping is made at run-time and is not known at design
time.
 
Hi Richard and Nicholas,
If we try to get to the bottom of this. We have the classes A,B and C.

A:object
B:A
C:A

I want to supply an instance of B when an instance of A is requested.
Nocholas then stated that this would break all sort of things. Causing B
and C to be incompatible. I don't get this, we must have misunderstood
eachother at some point.

In what way can it cause throubles that B is supplied instead of A when an
instance of B always can be treated as an A?

/Hugo
 
Back
Top