Property Default Value

Discussion in 'ASP .Net' started by shapper, Oct 17, 2006.

  1. shapper

    shapper Guest

    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("", "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
    shapper, Oct 17, 2006
    #1
    1. Advertising

  2. shapper

    Steven Nagy Guest

    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
    Steven Nagy, Oct 17, 2006
    #2
    1. Advertising

  3. shapper

    shapper Guest

    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
    shapper, Oct 17, 2006
    #3
  4. shapper

    Steven Nagy Guest

    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.
    Steven Nagy, Oct 17, 2006
    #4
  5. shapper

    shapper Guest

    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.
    shapper, Oct 17, 2006
    #5
  6. shapper

    Steven Nagy Guest

    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?
    Steven Nagy, Oct 18, 2006
    #6
  7. shapper

    shapper Guest

    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

    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?
    shapper, Oct 18, 2006
    #7
  8. shapper

    Steven Nagy Guest


    > 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
    Steven Nagy, Oct 18, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian
    Replies:
    0
    Views:
    385
  2. shapper

    Default property value

    shapper, Oct 12, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    359
    shapper
    Oct 12, 2006
  3. Nathan Sokalski
    Replies:
    25
    Views:
    1,423
    Nathan Sokalski
    Mar 4, 2010
  4. Dev_SH
    Replies:
    0
    Views:
    655
    Dev_SH
    Dec 30, 2010
  5. KJ
    Replies:
    0
    Views:
    143
Loading...

Share This Page