Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Property Default Value

Reply
Thread Tools

Property Default Value

 
 
shapper
Guest
Posts: n/a
 
      10-17-2006
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 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

 
Reply With Quote
 
 
 
 
Steven Nagy
Guest
Posts: n/a
 
      10-17-2006
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

 
Reply With Quote
 
 
 
 
shapper
Guest
Posts: n/a
 
      10-17-2006
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

Steven Nagy wrote:
> 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


 
Reply With Quote
 
Steven Nagy
Guest
Posts: n/a
 
      10-17-2006
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.

 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      10-17-2006
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

Steven Nagy wrote:
> 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.


 
Reply With Quote
 
Steven Nagy
Guest
Posts: n/a
 
      10-18-2006
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?

 
Reply With Quote
 
shapper
Guest
Posts: n/a
 
      10-18-2006
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/micro...1a08963618c1a4

Thanks,
Miguel

Steven Nagy wrote:
> 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?


 
Reply With Quote
 
Steven Nagy
Guest
Posts: n/a
 
      10-18-2006

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
<value> could not be set on property <property name> TonyDyer General Computer Support 0 02-14-2012 03:25 PM
Property File - property value break on multiple line Dev_SH Java 0 12-30-2010 09:34 AM
Setting the value of a Master page's control's property using a property of the Master page Nathan Sokalski ASP .Net 25 03-04-2010 03:42 AM
Default user control property value when used in web part ian ASP .Net 0 12-02-2005 04:22 PM
'property value' could not be set on property 'propertyname' KJ ASP .Net Web Controls 0 08-26-2003 06:08 PM



Advertisments