473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Property Default Value

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("ma**@domain.com", "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

Oct 17 '06 #1
7 1688
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

Oct 17 '06 #2
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
Oct 17 '06 #3
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.

Oct 17 '06 #4
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.
Oct 17 '06 #5
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?

Oct 17 '06 #6
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?
Oct 18 '06 #7
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

Oct 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick Patavalis | last post by:
Why does the following print "0 0" instead of "0 1"? What is the canonical way to rewrite it in order to get what I obviously expect? class C(object): __val = 0 def set_value(self, val): if...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
27
by: Just Me | last post by:
I made a Usercontrol that must have AutoScroll set to true when it is used. So I set it to True in the Load event. However the property still shows in the properties window when the control is...
5
by: Brent Ritchie | last post by:
Hello, This is my first attempt at template programming so please bear with me. This is what I have so far: Property.h -------------------------- #ifndef PROPERTY_H_ #define PROPERTY_H_
8
by: Jordi Rico | last post by:
Hi, I've made the next inherited class in Visual Studio 2005: Public Class LabelEx Inherits System.Windows.Forms.Label Sub New() MyBase.New() Me.ForeColor = Color.Black Me.AutoSize = False...
2
by: Lespaul36 | last post by:
I have a control that I have made that uses a custom class for storage of some of the information. I want it to display in the VS.Net 2003 property grid in the IDE like the Font property does, so...
9
by: Eric | last post by:
Hi Everyone, I'm writing a UserControl that exposes a property of the type System.Drawing.Image, like this: Public Property DefaultImage() As Image Get Return propDefaultImage End Get...
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
10
by: Derek Hart | last post by:
I can set focus to my property grid by using propgrid.Focus - but how can I set the default property that is always first? Or just set focus to the default property? Can this be done?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.