Form Validation From Business Object

  • Thread starter Thread starter Dave Wurtz
  • Start date Start date
D

Dave Wurtz

All,

Not sure if I'm taking the correct approach or not, but here it is...

I have my business object with a property. I want to validate the Setting
of this property with the business object. This works fine. I then create
a Form, put a TextBox control on it, add a DataBinding to the control to the
business object's property. When the control is displayed, it sets the text
to the property's get - perfect. When I type something in the textbox and
tab off of it, it calls the Set part of the property, which validates the
value and throws an exception. Where does this exception go? It doesn't
seem to go anywhere - as if the internal .net code has a try...catch, but is
not doing anything with the exception.

Is this even the correct approach for control validation? I have to have
the validation in the business object and I don't want to have to re-write
it again in the GUI.

Any help is appreciated!

Dave
 
Sorry,

I meant to post some source code:

Public Class MyTestClass
Public Property Test() As String
Get
Return "Test"
End Get
Set(ByVal Value As String)
Throw New System.Exception("This is an exception")
End Set
End Property
End Class

Create a window with a button and a textbox. Here's my binding code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs Handles Button1.Click
Dim o As New MyTestClass
Me.TextBox1.DataBindings.Add("Text", o, "Test")
End Sub

Thanks in advance...
Dave
 
Dave,

As a suggestion you might want to look at Rockfort Lhota's book on Business
Objects. I like and have used his method (abiet modified) for a BrokenRules
class.

Dan
 
Dan,

Thanks for the suggestion.

What is the title of this book? Do you have a ISBN number?

Dave
 
Visual Basic 6 Business Object (I believe he has one for dotNet, but the
methods still apply) ISBN 1-861001-07-X

Dan
 
Sorry,

I meant to post some source code:

Public Class MyTestClass
Public Property Test() As String
Get
Return "Test"
End Get
Set(ByVal Value As String)
Throw New System.Exception("This is an exception")
End Set
End Property
End Class

Create a window with a button and a textbox. Here's my binding code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs Handles Button1.Click
Dim o As New MyTestClass
Me.TextBox1.DataBindings.Add("Text", o, "Test")
End Sub

Thanks
Dave
 
Dave... I'm going to guess you took too many shortcuts.

Start off by adding a private property to MyTestClass and actually Get and
Set it (we want it to work properly.)

Then you are declaring your object in the Button_Click handler. It isn't
going to be visible outside of there right? And you are adding the binding
there too, each time you click.

So declare the instance of MyTestClass as a private property of the form,
assign the value of Test and do the binding in form_load. You shouldn't
need to be clicking the button to test it.

Tom
 
Back
Top