0

( I don't have any problem to accept an VB.NET or C# solution )

I've created a new empty WinForms project with this content written in VB.NET only to test the RangeAttribute, but the range and the error message are totally ignored (any error):

Public Class Form1

    Private Shadows Sub Load() Handles MyBase.Load

        Dim Test As New Test With {.MyInt32 = Integer.MaxValue}

        MessageBox.Show(Test.MyInt32) ' Result: 2147483647

    End Sub

End Class

Public Class Test

    <System.ComponentModel.DataAnnotations.Range(1I, 10I, errormessage:="something")>
    Public Property MyInt32 As Integer
        Get
            Return Me._MyInt32
        End Get
        Set(ByVal value As Integer)
            Me._MyInt32 = value
        End Set
    End Property

    Private _MyInt32 As Integer = 0I

End Class

Why happens this?

Searching for an alternative solution I've done the same using PostSharp to create an Aspect as described in one of the answers of this question, but I don't like this solution because I should not depend on a third party lib to do this in case that the .NET Framework Class Library exposes a way to do the same (and I think is much better 'cause the overloads of the attribute for DateTime types, etc).

Community
  • 1
  • 1
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

2 Answers2

0

No wonder it's not working because the RangeAttribute is not supported by WinForms. Everything you'll find in the namespace System.ComponentModel.DataAnnotations are for web applications.

"The System.ComponentModel.DataAnnotations namespace provides attribute classes that are used to define metadata for ASP.NET MVC and ASP.NET data controls." - MSDN

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
0

You just need to perform the validation. Although System.ComponentModel.DataAnnotations are commonly used for the Web that doesn't mean they will only work there. A good console demonstration of this is available on Code Project.

Below is a quick modified version of your code that works:

Imports System.ComponentModel.DataAnnotations

Public Class Form1

    Private Shadows Sub Load() Handles MyBase.Load

        Dim Test As New Test
        Test.MyInt32 = Int32.MaxValue

        MessageBox.Show(Test.MyInt32) ' Result: 1, because it got set to this in the exception handler of the property setter

    End Sub

End Class

Public Class Test

    <Range(1, 10, errormessage:="something")>
    Public Property MyInt32 As Integer
        Get
            Return Me._MyInt32
        End Get
        Set(ByVal value As Integer)
            Try
                Validator.ValidateProperty(value, New ValidationContext(Me, Nothing, Nothing) With {.MemberName = "MyInt32"})
                Me._MyInt32 = value
            Catch ex As ValidationException
                Me._MyInt32 = 1
            End Try
        End Set
    End Property

    Private _MyInt32 As Integer

End Class
Bart Sipes
  • 921
  • 8
  • 21
  • I down voted your answer because I don't believe it's correct. My understanding of theses classes is that they were built for the web but not exclusively and I wrote the code to prove it and included an link of someone else doing the same. Here are two more links of others mentioning that it can be done as well. http://sanjeevtechblogs.blogspot.com/2010/08/dataannotations-and-aspnet-mvc.html and http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx. If you believe my answer is wrong please demonstrate that. – Bart Sipes Jan 07 '15 at 16:49
  • Well, you're *wrong*. It's *not* supported nor used in `winforms`. It's clearly stated in [this document](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx): `... used to define metadata for ASP.NET MVC and ASP.NET data controls`. Also, OP clearly stated that he did *not* want to depend on a third party lib (your answer). – Bjørn-Roger Kringsjå Jan 07 '15 at 16:58
  • I don't disagree that that is what the MSDN documentation says. However, if you look at what Brad Wilson (former dev at MS on the ASP.NET team) about this you will see that it is not the full story: "The purpose of this DLL is to provide UI-agnostic ways of annotating your data models with semantic attributes like [Required] and [Range]... The UI-agnostic bit is important, and is why the functionality exists in the System.ComponentModel.DataAnnotations namespace, rather than somewhere under System.Web." http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html – Bart Sipes Jan 07 '15 at 17:24
  • So what? It's still not supported. Note that I'm not guessing. I know. I've been developing both web and desktop application in .net for years. If you want to know how user input is handled in winforms then I suggest this link: [User Input Validation in Windows Forms](http://msdn.microsoft.com/en-us/library/ms229603%28v=vs.110%29.aspx). – Bjørn-Roger Kringsjå Jan 07 '15 at 17:36