Solved

Attribute Class - Constant Expression Required

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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