?
Solved

Attribute Class - Constant Expression Required

Posted on 2011-09-13
2
Medium Priority
?
406 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

840 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