Property Default Value

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have a property which type is Mail.MailAddress.
I need to set a default value.

This is what I tried:

Private _From As Mail.MailAddress
<DefaultValue(New Mail.MailAddress("(e-mail address removed)", "Name"))> _
Public Property From() As Mail.MailAddress
Get
Return _From
End Get
Set(ByVal value As Mail.MailAddress)
_From = value
End Set
End Property

I get an error:
"Constant Expression is Required"

How can I solve this?

Thanks,
Miguel
 
Private _From As Mail.MailAddress

Public Property From() As Mail.MailAddress
Get
if _From is nothing then
_From = new Mail.MailAddress("whatever", "whatever")
end if
Return _From
End Get
Set(ByVal value As Mail.MailAddress)
_From = value
End Set
End Property
 
What is the diferrence between using

if _From is nothing then
_From = new Mail.MailAddress("whatever", "whatever")
end if

and

<DefaultValue( ... )> _ ...

Which approach will you advise me to use?

Thanks,
Miguel
 
Well I'll be honest; I am not familiar with default value, but I have a
feeling that you can't initialise objects in an attribute declaration
like that. I'd say the attribute usage is more for value types where a
literal can be supplied.

Otherwise, I'd also say that the main difference between the two
options is that you are currently getting an error with your code, but
mine won't cause an error.

I'll double check on the DefaultValue attribute later in the week, but
I'm about 70% certain that my above statement was correct.
 
Thanks Steven.

I saw the approach I use on a MSDN document.
I am not sure if this is the best way to do this but I just followed
that document.

Thanks,
Miguel
 
Can you post a link to that MSDN entry?
In that example, were they using a value type or a reference type for
their property?
 
Hi,

I wish I could but I didn't bookmarked it. I found it when I was
creating a custom control.
I have been fighting with the property default values since I started
creating an email class.

The problem is not when it is a string is more when it is a
mail.address.

Anyway, sometime ago I posted a similar problem but I gave the
mail.mailprioriry as a property example. This one was simples because
it acts like boolean and the approach I described works fine with it.
My main problems became when I started to need to created some more
complex default values, i.e., not constan.

Anyway, read this post:
http://groups.google.com/group/micr....aspnet/browse_thread/thread/c81a08963618c1a4

Thanks,
Miguel
 
The problem is not when it is a string is more when it is a
mail.address.

Yes you can provide a literal constant for strings but not any other
reference types.
So once again I think the problem comes down to the fact that you are
trying to instantiate an object inside an attribute, which I don't
think you are allowed to do.
Anyway, sometime ago I posted a similar problem but I gave the
mail.mailprioriry as a property example. This one was simples because
it acts like boolean and the approach I described works fine with it.
My main problems became when I started to need to created some more
complex default values, i.e., not constan.

I am guessing that Mail.Mailpriority is an enumeration, which means it
is a value type, and therefore is still a literal (it has a defined
value).

Anyways, try the code I suggested originally.

Steven
 
Back
Top