473,396 Members | 1,921 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,396 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 1691
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.