OO question..

  • Thread starter Thread starter Zoury
  • Start date Start date
Z

Zoury

Hi folks! :O)

I've implemented a DateTimePickerEx with inherits from a DateTimePicker
control.


I've declared these properties in it :
//**
new public string Text
{
get {return _label.Text;}
set {_label.Text = value;}
}
public Int32 ILanguage.LanguageID // comes from an ILanguage interface..
{
get {return _languageID;}
set {_languageID = value;}
}
//**
where _label is a Label control type private field and _languageID is an
Int32 private field.


the ILanguage interface looks like this :
//**
public interface ILanguage
{
Int32 LanguageID {get; set;}
}
//**


In a form we loop through all controls in it in orher to set the Text
property :
//**
foreach (Control control in this.Controls)
{
ILanguage il = control as ILanguage;
if (il != null) // we got a ILanguage compatible control
control.Text = MyApp.GetString(control.LanguageID); // gets the
string
}
//**

The problem i got is that control.Text seems be referring to the
DateTimePicker.Text property and not DateTimePickerEx.Text.. I mean it tries
to parse the given string into a DateTime type which cause an exception to
be thrown.

I can solves my problem by checking the control's type, then cast it and
then use the Text property as I want to... but this really sucks. We did use
the ILanguage interface to prevent a huge switch case..

Have I missed something or is this totaly a normal OO behavior ? Isn't there
a way to force the use of the latest Text property's implementation in the
hierarchy ?

Thanks a lot!
 
Zoury,

In your declaration of the Text property, do this:

public override string Text
{
// Rest of implementation.
}

You were shadowing the property. Since you cast to Control, it took the
implementation from the Control class, and not your derivation.

Hope this helps.
 
Hi Nicholas! :O)
You were shadowing the property. Since you cast to Control, it took the
implementation from the Control class, and not your derivation.

man i feel stupid.. :O)
I didn't even though of that. I first tried to [override] the Value property
but since it's not virtual I kind of thought all properties were the same,
so I choose the [new] way.

anyway the difference between [new] and [override] modifier is clearer now..
thanks again for your explanation.
 
Back
Top