Inheriting events from an object?

N

Nigel V Thomas

Hi (sorry a bit long winded)


I do not know if I'm barking up the wrong tree here but...

Summary:
I have UserControl which has a textbox on it. I use a class (to which the
text box is bound) to perform some non-standard validation and raise events
and logs errors. Works fine.

Now I need to extend the UserControl to display a CheckBox or a TextBox. I
can't bind the CheckBox to the validation class because it's expecting a
TextBox.

I've changed the declaration in the Class from:
Dim objValidation as TextBox

....to

Dim objValidation as Object

Now, naturally, objValidation does not expose the events of TextBox (which
it did when it was created as a textBox.

So the question is this:
Is it possible to create an object using the declaration Dim obj as Object
which inherits (is that the right term) either the TextBox or CheckBox events?

Nigel
 
J

Jack Jackson

Hi (sorry a bit long winded)


I do not know if I'm barking up the wrong tree here but...

Summary:
I have UserControl which has a textbox on it. I use a class (to which the
text box is bound) to perform some non-standard validation and raise events
and logs errors. Works fine.

Now I need to extend the UserControl to display a CheckBox or a TextBox. I
can't bind the CheckBox to the validation class because it's expecting a
TextBox.

I've changed the declaration in the Class from:
Dim objValidation as TextBox

...to

Dim objValidation as Object

Now, naturally, objValidation does not expose the events of TextBox (which
it did when it was created as a textBox.

So the question is this:
Is it possible to create an object using the declaration Dim obj as Object
which inherits (is that the right term) either the TextBox or CheckBox events?

Nigel

You don't say what events you are exposing, but I would define generic
events in the UserControl (maybe DataChanged, etc.), then have the
UserControl handle the appropriate events from the textbox or checkbox
and then raise its own events.
 
N

Nigel V Thomas

Jack Jackson said:
You don't say what events you are exposing, but I would define generic
events in the UserControl (maybe DataChanged, etc.), then have the
UserControl handle the appropriate events from the textbox or checkbox
and then raise its own events.

Jack

No, I can't do that because the validation is being performed in the class
and not the UserControl.
 
F

Family Tree Mike

Nigel said:
Hi (sorry a bit long winded)


I do not know if I'm barking up the wrong tree here but...

Summary:
I have UserControl which has a textbox on it. I use a class (to which the
text box is bound) to perform some non-standard validation and raise events
and logs errors. Works fine.

Now I need to extend the UserControl to display a CheckBox or a TextBox. I
can't bind the CheckBox to the validation class because it's expecting a
TextBox.

I've changed the declaration in the Class from:
Dim objValidation as TextBox

...to

Dim objValidation as Object

Now, naturally, objValidation does not expose the events of TextBox (which
it did when it was created as a textBox.

So the question is this:
Is it possible to create an object using the declaration Dim obj as Object
which inherits (is that the right term) either the TextBox or CheckBox events?

Nigel

Object would be the nearest common ancestor of those two objects, so it
would have to come in as an object. You can however cast back to what
it actually is, as follows:


Function Validate(ByVal ctl As Object) As Boolean
If TypeOf ctl Is TextBox Then
Dim tb As TextBox = CType(ctl, TextBox)
' treat tb for validation
ElseIf TypeOf ctl Is CheckBox Then
Dim cb As CheckBox = CType(ctl, CheckBox)
' treat cb for validation
End If
End Function
 
J

Jack Jackson

Jack

No, I can't do that because the validation is being performed in the class
and not the UserControl.

You can test for what kind of object objValidation is and then cast
it.

If objValidation Is Textbox Then
Dim txtbox As Textbox = DirectCast(objValidation, Textbox)

Also, I would declare objValidation as Control rather than Object.
That might get you some events that are defined in Control without
having to cast.

If you explain more about where and how this validation class is used
there might be other ways to accomplish this in a cleaner way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top