Re: Abstract class variables question



"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:uplYmP9JIHA.5860@xxxxxxxxxxxxxxxxxxxxxxx

No, they couldn't. The base class needs to know that this property
exists, because it uses it to validate the type when setting the data.
Without the abstract property, you couldn't write code in the base class
that relies on the property without having the base class be aware of
each and every derived class.

Which was what I was trying to figure out. And this is obviously the
answer as to how the Base Class can tell what the type is.

But I am confused as to how it works.

You actually set _TypeRequired in each class and _ValidateType seems to
know what _TypeRequired is. In _ValidateType, at the line:

Type typeRequired = _TypeRequired;

Before it is actually executed - it seems to know what _TypeRequired is
!!! Yet I can't see where it is actually being set - just defined (either
as abstract or overridden).

It's a property, not a variable, so it is computed each time it is
referenced by calling the getter. The getter is simply "{ return
typeof(bool); }".
Right.

As Peter suggested, I was confused by the Debugger which apparently did the
get itself or just figured it out. But that it was this statement that
actually set the _TypeRequired variable - doing the get the very first
thing.

Thanks,

Tom


.



Relevant Pages

  • Re: Abstract class variables question
    ... because the C# compiler will inline the property access code if it can. ... you couldn't write code in the base class ... You actually set _TypeRequired in each class and _ValidateType seems to know ... can include code that the compiler can verify as correct, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class variables question
    ... Without the abstract property, you couldn't write code in the base class ... You actually set _TypeRequired in each class and _ValidateType seems to ... referenced by calling the getter. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract class variables question
    ... you couldn't write code in the base class ... You actually set _TypeRequired in each class and _ValidateType seems to know ... As a virtual property, the base class can call it without knowing the concrete type of the instance, and the override will still be executed and return the right value. ... I am in the habit of using the underscore prefix for non-public members of classes. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: accessing static members through Type?
    ... | the base class and implement it in each class: ... | (It's my personal opinion that reflection generally is bad for program ... Therefore, in the absence of virtual static members, the simplest choice is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: declare variable in base class that must be set in dervied class?
    ... Could you use an abstract Property rather than an actual variable? ... Adam Clauss ... >I have a variable in a base class defined as: ... > cannot set variables to abstract. ...
    (microsoft.public.dotnet.languages.csharp)

Loading