473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding validation in Property

RP
I am using following code to validate the value that is being assigned
via property. But it is not working:

----------------------------------------
public string vProductName
{
get
{
return ProductName;
}
set
{
ProductName = value;
if (ProductName == null)
{ ProductName = "-"; }
}
}
-----------------------------------------

It is not taking ProductName = -. Even after declaring the if
condition, null is being returned.

Nov 8 '07 #1
4 1584
Can you illustrate with an example? Perhaps the ProductName property
is doing something interesting...

For info, another option here would be the null-coalescing operator:
set {
ProductName = value ?? "-";
}

Marc

Nov 8 '07 #2
RP
You may refer my post in this forum:
http://forums.microsoft.com/MSDN/Sho...62415&SiteID=1

While initializing values in a property, I want to check whether it is
a null. If yes, then I want the value to be "-".

----------------------------------------------------------
On Nov 8, 4:44 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Can you illustrate with an example? Perhaps the ProductName property
is doing something interesting...

For info, another option here would be the null-coalescing operator:
set {
ProductName = value ?? "-";

}

Marc

Nov 8 '07 #3
On 8 Nov., 15:10, RP <rpk.gene...@gmail.comwrote:
You may refer my post in this forum:http://forums.microsoft.com/MSDN/Sho...62415&SiteID=1

While initializing values in a property, I want to check whether it is
a null. If yes, then I want the value to be "-".

----------------------------------------------------------
On Nov 8, 4:44 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Can you illustrate with an example? Perhaps the ProductName property
is doing something interesting...
For info, another option here would be the null-coalescing operator:
set {
ProductName = value ?? "-";
}
Marc- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
The code looks feasible, it should work (although Marc's suggestion of
using ?? is more elegant imho).
Looking at the code you posted in the MSDN forums you should be aware
of one thing though:
If someone calls the constructor with "null" as "_ProductName", your
property "vProductName" will return null, since the set-part of the
property is never called in this case.
You should set the property in the constructor (vProductName =
_ProductName) if you want your input to be validated, not the field.
Depending on the situation this may be the cause of the problem you
are seeing, unfortunately the conditions of your test aren't clear in
your post.

hth,
Kevin Wienhold

Nov 8 '07 #4
On 8 Nov., 15:10, RP <rpk.gene...@gmail.comwrote:
You may refer my post in this forum:http://forums.microsoft.com/MSDN/Sho...62415&SiteID=1

While initializing values in a property, I want to check whether it is
a null. If yes, then I want the value to be "-".

----------------------------------------------------------
On Nov 8, 4:44 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Can you illustrate with an example? Perhaps the ProductName property
is doing something interesting...
For info, another option here would be the null-coalescing operator:
set {
ProductName = value ?? "-";
}
Marc- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
Forgot to mention:
A similar problem will crop up if you call the parameterless
constructor, since "ProductName" will remain at its initial value.
Since String is a reference type, its initial value is "null".

hth,
Kevin Wienhold

Nov 8 '07 #5

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

Similar topics

14
6268
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
2
4836
by: D Sheldon | last post by:
I am creating a server control that adds web controls (i.e. textboxes, etc) to a form. I use HtmlTable to build the table and insert the controls. Now I want to add validators to the textbox. Here...
3
1514
by: VB Programmer | last post by:
Can you make a required field validator optional? I know it sounds crazy, but.... Let's say I have a signup form. It consists of 2 sections. The top section is a "GOLD MEMBER" section. It...
1
4709
by: Jack | last post by:
Hi, I have a page with a repeater control that contains textboxes. I'm trying to create a validation class that is called by my page. A method in this class iterates through all the controls...
5
3799
by: Micky | last post by:
VB v7.1.3088 NET v1.1.4322 SP1 My mate has a strange problem regarding the ESC key and validation. When he hits the Cancel button on his form, the form does not validate. This is correct...
2
1186
by: tshad | last post by:
I have a page that has no Validation objects, but I also have the following code: if not Page.IsValid exit sub end if For some reason, the page is hitting the "exit sub" statement? How...
1
1809
by: Niclas | last post by:
Hi, How do I indicate what field fails a validation, usually you see a red star next to the faild field on a form. Is this built in functionality in the valdaition controls or shall I code this...
5
4575
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
11
2961
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
1
2556
by: DotNetNewbie | last post by:
Hello, I have a validation summary tag in my .aspx page, and when a user clicks on a submit button I check the database for a duplicate username that the user entered into a textbox control. If...
0
7278
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
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.