|
-
Mar 17th, 2008, 12:51 PM
#1
Thread Starter
Member
How do I validate a custome property value in a custom class?
I'm sure this has been answered before, but my search was fruitless.
I have a class, it has a property, which is a double. How should I be validating that when the property is set, the value is a double? I don't seem to be able to catch the error prior to it happening.
The scenario that causes this problem is allowing the user to put a value in a Property Grid (PropertyGridEx to be exact). Because the value is directly connected to the class property I seem to be unable to intercept rubbish data.
I have tried a Try/Catch in the Property Set method, but this seems to be too late in the process.
Code:
Public Class Rail
Private dblOffset As Double
Public Property offset() As Double
Get
Return dblOffset
End Get
Set(ByVal value As Double)
dblOffset = value
End Set
End Property
End Class
Thanks in advance!
-
Mar 17th, 2008, 01:01 PM
#2
Re: How do I validate a custome property value in a custom class?
Code:
Public Class Rail
Private dblOffset As Double
Public Property offset() As Double
Get
Return dblOffset
End Get
Set(ByVal value As Double)
If value < 0 Then value = 0
If value > 100 Then value = 100
dblOffset = value
End Set
End Property
End Class
-
Mar 17th, 2008, 02:14 PM
#3
Thread Starter
Member
Re: How do I validate a custome property value in a custom class?
Sorry, I think you missed the essential part...
How should I be validating that when the property is set, the value is a double?
If the value is "xyz", then you get an error that I have been unable to capture.
-
Mar 17th, 2008, 03:34 PM
#4
Re: How do I validate a custome property value in a custom class?
I can't really visualize the situation you're talking about. If a property is declared as double, you can't set it to "xyz". Period. If this class is a control and you set this property to "xyz" in design view, you'll automatically get a messagebox telling you that the property is invalid. If you trying to set it in code, the IDE will show error.
Can you tell us a little more on how you set the property with incorrect type and still getting away with it until runtime?
-
Mar 17th, 2008, 04:58 PM
#5
Thread Starter
Member
Re: How do I validate a custome property value in a custom class?
Firstly, so you understand my level of knowledge - I'm a seasoned programmer (mainframe styley), but VB is a hobby, and one that I don't get too much exposure to.
PropertyGridEx is a nice little extension of the standard Property Grid. It allows you to take any value (variable, property or control) and assign it to a Property Grid value. Essentially this means that the user can enter a value and the property value (in my example) gets updated automatically.
But - the Property grid allows you to type an alpha value into a text box (naturally) even though the attached property is a double. So somewhere in between the text being entered and it reaching the property which is incompatible, I need to catch the error. Unfortunately it seems to be in no-mans land, and I can't intercept it. You can see the result of adding an "x" to a similar, integer property in the image below (I hope it shows up).
I'm hoping there is a standard method for dealing with this situation.
-
Mar 17th, 2008, 05:24 PM
#6
Frenzied Member
Re: How do I validate a custome property value in a custom class?
I don't think you can. Like you said the PropertyGrid is catching it before your property.
You'd have to dig into the code for the p.g. class and see if you can copy it's behavior.
Other than that, you're out of luck because your base class is where the problem is.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|