Click here to Skip to main content
15,893,381 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generics, delegates and type inference Pin
Gideon Engelberth2-Oct-09 13:00
Gideon Engelberth2-Oct-09 13:00 
QuestionInheritance question Pin
xkrja1-Oct-09 23:35
xkrja1-Oct-09 23:35 
AnswerRe: Inheritance question Pin
stancrm1-Oct-09 23:49
stancrm1-Oct-09 23:49 
GeneralRe: Inheritance question Pin
xkrja2-Oct-09 0:22
xkrja2-Oct-09 0:22 
GeneralRe: Inheritance question Pin
Mirko19802-Oct-09 0:57
Mirko19802-Oct-09 0:57 
AnswerRe: Inheritance question Pin
Christian Graus2-Oct-09 0:40
protectorChristian Graus2-Oct-09 0:40 
AnswerRe: Inheritance question Pin
DaveyM692-Oct-09 0:46
professionalDaveyM692-Oct-09 0:46 
AnswerRe: Inheritance question Pin
dojohansen3-Oct-09 1:12
dojohansen3-Oct-09 1:12 
Say we have
class A { protected string field1; }
class B : A { protected string field2; }
class C : B { protected string field3; }

void foo(A a) {}
void bar(B b) {}
void foobar(C c) {}


Now consider what instances of these objects *actually* will be inside your hardware. (We can safely ignore the fact that A extends object for the purpose of looking at why your attempted cast doesn't make sense.)

First a word about references: A reference is what we might also call a "managed pointer": Like a pointer, it simply contains the address where the data is stored, but unlike a pointer, we cannot obtain this address and the memory manager can move stuff around and update pointers as needed without us knowing anything about it.

Since string is a reference type, an instance of A is a word of memory (4 bytes on a 32-bit system) containing a reference to string data. An instance of B however is two words of data, containing two references, to field1 and field2 data. And C of course is three words, laid out in memory like an instance of B appended with the additional reference to field3 data.

What you want to do is the equivalent of trying to use an instance of A and pass it to foobar, which takes a parameter of type C. Since an A is just the word for the field1 reference, foobar might access memory outside the bounds of the object and it could screw up other live objects (placed next to the instance of A in memory). So clearly the compiler shouldn't let you do that.

If you wanted the compiler to just go ahead and create an instance of C for you when you make the cast, it wouldn't know what to do with field2 and field3. There is no way *in general* to know that the MotorBike class (descendant type) works just fine with only the data it inherited from the Vehicle class. If there were, we could just go like this:
AmazingWebSerive s = (AmazingWebService)(new object());


Sadly, this doesn't work. Smile | :) Instead, it's you as a programmer who may know this and provide a way to construct a B with only A data, or a C with only A data, and so on. Then you decide what the defaults should be for the data that doesn't exist in A.
class A { protected string field1; }
class B : A { protected string field2; }
class C : B 
{ 
  protected string field3; 
  public C(A a) { field1 = a.field1; }
}


Now if I have an A and I want to "use it as a C" I can simply go C c = new C(a); and work from there.

BUT... if the reason you derived your own Tag class was because you wanted to add operations (methods) rather than data, there is another way. If you're using C# 2.0 you can write your methods as extension methods instead. For example, let's say I want to add a GoCrazy() method to A but A is defined in my generated web service proxy code and I don't want to touch that stuff (perhaps because I may need to regenerate it later). Then I can declare the extension method in any class I want, like this:
namespace MyExtentions
{
  public class Whatever
  {
    static public void GoCrazy(this A a) { ... }

    static public C ToC(this A a, string field2, string field3)
    {
      C c = new C(a);
      c.field2 = field2;
      c.field3 = field3;
    }
  }
}

// in some method in some other file that includes using MyExtensions;
a.GoCrazy();  // compiler finds the extension method and substitutes Whatever.GoCrazy(a);
C c = a.ToC("foo", "bar"); // Whatever.ToC(a, "foo", "bar");


The keyword "this" before the first parameter of a static method makes the method an extension method, so the compiler can find it when "a.GoCrazy()" cannot be resolved as an instance method of A.

Hope this helps!
QuestionStream System Sound to A GSM MODEM Pin
aghoshbabu1-Oct-09 23:09
aghoshbabu1-Oct-09 23:09 
AnswerRe: Stream System Sound to A GSM MODEM Pin
Rutvik Dave2-Oct-09 4:27
professionalRutvik Dave2-Oct-09 4:27 
QuestionDotNetOpenMail Authentication Pin
DotNetCoderJunior1-Oct-09 22:52
DotNetCoderJunior1-Oct-09 22:52 
AnswerRe: DotNetOpenMail Authentication Pin
Christian Graus1-Oct-09 23:05
protectorChristian Graus1-Oct-09 23:05 
GeneralRe: DotNetOpenMail Authentication Pin
DotNetCoderJunior1-Oct-09 23:23
DotNetCoderJunior1-Oct-09 23:23 
GeneralRe: DotNetOpenMail Authentication Pin
Tom Deketelaere2-Oct-09 0:25
professionalTom Deketelaere2-Oct-09 0:25 
GeneralRe: DotNetOpenMail Authentication Pin
dojohansen3-Oct-09 1:18
dojohansen3-Oct-09 1:18 
GeneralRe: DotNetOpenMail Authentication Pin
Christian Graus2-Oct-09 0:41
protectorChristian Graus2-Oct-09 0:41 
JokeRe: DotNetOpenMail Authentication Pin
Tom Deketelaere2-Oct-09 2:27
professionalTom Deketelaere2-Oct-09 2:27 
QuestionC# 4.0: Dynamic Programming - usage question Pin
Ekaterina Staykova1-Oct-09 21:47
Ekaterina Staykova1-Oct-09 21:47 
AnswerRe: C# 4.0: Dynamic Programming - usage question Pin
Rob Philpott1-Oct-09 21:51
Rob Philpott1-Oct-09 21:51 
AnswerRe: C# 4.0: Dynamic Programming - usage question Pin
Richard MacCutchan1-Oct-09 23:21
mveRichard MacCutchan1-Oct-09 23:21 
GeneralRe: C# 4.0: Dynamic Programming - usage question Pin
Ekaterina Staykova2-Oct-09 0:08
Ekaterina Staykova2-Oct-09 0:08 
AnswerRe: C# 4.0: Dynamic Programming - usage question Pin
Daniel Grunwald2-Oct-09 0:35
Daniel Grunwald2-Oct-09 0:35 
AnswerRe: C# 4.0: Dynamic Programming - usage question Pin
Pete O'Hanlon2-Oct-09 0:57
mvePete O'Hanlon2-Oct-09 0:57 
Questionwhen to use c# attributes Pin
mxdev1-Oct-09 19:54
mxdev1-Oct-09 19:54 
AnswerRe: when to use c# attributes Pin
Christian Graus1-Oct-09 20:31
protectorChristian Graus1-Oct-09 20:31 

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.