Property Default Value

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
 
S

Steven Nagy

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
 
S

shapper

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
 
S

Steven Nagy

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.
 
S

shapper

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
 
S

Steven Nagy

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?
 
S

shapper

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
 
S

Steven Nagy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Property default value 1
Default value 1
Property Default Value 1
Default property value 4
Check property value 1
Property 1
Property 1
Property 0

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top