Re: Abstract class variables question
- From: "tshad" <tfs@xxxxxxxxxxxxxx>
- Date: Thu, 15 Nov 2007 17:33:44 -0800
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:2007111515214511272-NpOeStPeAdM@xxxxxxxxxxxxxxxxxx
On 2007-11-15 13:45:12 -0800, "tshad" <tfs@xxxxxxxxxxxxxx> said:
[...]
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
!!!
That's probably just the debugger being clever. Nothing is actually known
until the getter for the property is executed, which doesn't happen until
that assignment is executed (or until the debugger tries to evaluate the
property).
Yet I can't see where it is actually being set - just defined (either as
abstract or overridden).
I added a line just to break on and see what _TypeRequired is and it knew
it
was system.bool. But where did it get set???
It's "set" basically as a hard-coded statement in the property
declaration. There's no data storing the value; just a line of code that
always returns the same value.
So the _TypeRequired is really nothing until the "get" which would be during
the assignment here:
Type typeRequired = _TypeRequired;
I assume that what happens when it gets to the this statement is that it
1) does the "get" on the _TypeRequired
2) Takes the returned value (system.boolean, system.string, system.integer
etc) and assignes it to typeRequired
And then when you actually do the assignment of Data, it doesn't really
matter what the type is as the object pointer is now pointing at:
public object Data
{
get { return _objCurrent; }
set
{
if (value != null)
{
_ValidateType(value);
}
_objCurrent = value;
_fChanged = true;
}
}
Since it would be something like:
bool temp;
BoolType boolTypeTemp = new BoolType();
boolTypeTemp.Data = true
temp = (bool)boolTypeTemp;
In the 3rd line, we are putting true into the object _objCurrent and
_objCurrent doesn't really care what it is. It is just an object. Of
course, before we actually assigned it we checked the type with
_ValidateType(value).
So now we have an object _objCurrent that is set to the value of true but
_objCurrent doesn't really know that it is a bool, just that it has a value
of true. Which, if I am not mistaken is just a place on the heap with a -1
one in it and _objCurrent is just a pointer to it. Correct?
And when I return it, I need to cast it to bool since we are just passing
back an object which could be anything.
Also, I assume that a null would be a problem if I tried to pass it back a
null. If I did this, I would have a problem since bool can't be null.
Right?
bool temp;
BoolType boolTypeTemp = new BoolType();
boolTypeTemp.Data = null
temp = (bool)boolTypeTemp;
Because the property is "abstract", it's basically a virtual property, but
without a default implementation (think pure virtual in C++). 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.
But the value per se isn't initialized or stored anywhere.
[...]
Just out of curiosity - I was wondering why you underscore _ValidateType
and
_TypeRequired. I understand why you do it for _objCurrent (memory
variable)
and why you use prefix the type (obj - hungarian). But I was wondering
about the other 2.
Actually, you don't understand why I do it for _objCurrent. :)
I am in the habit of using the underscore prefix for non-public members of
classes. That's what you're seeing there. That applies both to
_objCurrent field as well as methods and properties (like _ValidateType
and _TypeRequired).
I guess I didn't understand.
What I typically do is use camel case for my memory variables and Pascal
case for my objects, properties and functions.
Of course, this causes a problem in asp.net if I have a textbox named Name
and memory variable as name - now what do I do with my property. What I
typically do is at this point just change the textbox name to objName.
Not really very clean. I need to think it through better.
Thanks,
Tom
.
- Follow-Ups:
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- References:
- Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Abstract class variables question
- Prev by Date: Re: using windows icons in my winforms app
- Next by Date: Re: Abstract class variables question
- Previous by thread: Re: Abstract class variables question
- Next by thread: Re: Abstract class variables question
- Index(es):
Relevant Pages
|