Solved

Attribute Class - Constant Expression Required

Posted on 2011-09-13
2
393 Views
Last Modified: 2012-05-12
I have an attribute class which I am using to check a business rule. The problem is it says "Constant expression required"

  <DisplayName("Schedule Start Date"), DateNotBefore(Date.Now)> _
    Public SchedStartDate As Date
 
....
  <AttributeUsage(AttributeTargets.Field Or AttributeTargets.Property)> _
    Public Class DateNotBeforeAttribute
        Inherits System.Attribute

        Implements ITest

        Private _value As Date

        Public Sub New(ByVal value As Date)
            _value = value
        End Sub

        Public Function TestCondition(ByVal value As Object, ByRef cls As Object) As Boolean Implements ITest.TestCondition

            Dim dt As Date = CType(value, Date)

            If dt < _value Then
                Return True
            Else
                Return False
            End If

        End Function



0
Comment
Question by:rocky050371
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36530054
Attributes are fixed at compilation time. So you cannot use Date.Now in the attribute call, because it is variable. You need a constant.

If you want to record now in the attribute, do not pass it as a parameter, simply set as such in the constructor:

Public Sub New()
    _value = Date.Now
End Sub
0
 

Author Comment

by:rocky050371
ID: 36530210
As I see thank you
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question