Attributes

  • Thread starter Thread starter zsilence_is
  • Start date Start date
Z

zsilence_is

I have created my attribute with the constructor as
follows:

Public Sub New(ByVal value As Object)
MyBase.New(value)
End Sub

now when I try to use it like this
<Att(1)>
it get the following error: conversion from integer to
system.object cannot occur in a constant experssion!

Note that this code runs perfectly on C#.. Any explenation
and solution plz.

Regards
 
Hi zsilence,

|| Public Sub New(ByVal value As Object)
|| MyBase.New (value)
|| End Sub

This will work
Public Sub New (Value As Integer)

As to why there is a conversion error, I do not know.

I'm puzzled by your MyBase.New (value). What Attribute class are your
inheriting from ? The base class Attribute doesn't take parametres in its
New().

You say it works in C#. Can you show me your Attribute class in C# and in
VB?

Regards,
Fergus
 
Here is my structure:
I have this attribute:
<AttributeUsage(AttributeTargets.Property Or
AttributeTargets.Field)> Public NotInheritable Class
GreaterThan
Inherits ComparisonConstraint

here is its constructor:
Public Sub New(ByVal value As Object)
MyBase.New(value)
End Sub 'New
I need to pass an object cause it is a generic one.

The base class is as follows:
<AttributeUsage(AttributeTargets.Property Or
AttributeTargets.Field)> _
Public MustInherit Class ComparisonConstraint
Inherits Constraint

it has this constuctor:
Public Sub New(ByVal value As Object)
Me.value = value
End Sub

and the last one in the chain is :
<AttributeUsage(AttributeTargets.Property Or
AttributeTargets.Field)> _
Public MustInherit Class Constraint
Inherits Attribute
with the default constructor


The probelm as I can c, is that int is a premitive type
which can not be converted to object type because
attribute require a constant ... But this all structure
works in C# .... This is driving me crazy ... can you
suggest.

Thankx in advance.
 
I have created my attribute with the constructor as
follows:

Public Sub New(ByVal value As Object)
MyBase.New(value)
End Sub

VB.NET and the CLS doesn't support Object attribute parmeters, so if
possible you should avoid using that.



Mattias
 
Back
Top