Solved

Attribute Class - Constant Expression Required

Posted on 2011-09-13
2
364 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now