473,657 Members | 2,594 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 1587
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...@g mail.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...@gm ail.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...@g mail.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 "_ProductNa me", your
property "vProductNa me" 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...@gm ail.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...@g mail.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 "ProductNam e" 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
6291
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) show the error message next to the control. For example, if the text field is empty with RequiredField Validator control, it can show the value in ControlToValidate property in two ways as I mentioned. Please advise. Thanks!
2
4848
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 is the code that I am using bool lastnamerequired=false public bool LastNameRequire get{return lastnamerequired; set{lastnamerequired = value; if(lastnamerequired
3
1526
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 has required validators for name, address, etc... The next section is the "BRONZE MEMBER" section. It also has required validators for the same fields. The user only has to fill out 1 set of info. Any thoughts on how to accomplish this?
1
4719
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 on the page. On encountering a TextBox control, it looks at the ID to discern the field the TextBox represents, and then creates the appropriate validation controls and adds them to the page. The controlToValidate property of each validation...
5
3806
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 behaviour of course. However, he also wants this same behaviour when hitting ESC on the keyboard. It was my understanding that when you assign a button to a form's CancelButton property you automatically gained this functionality. However,
2
1194
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 can that be? What else would invalidate a page and how can I find out
1
1825
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 myself ? Niclas
5
4589
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 to add a dictionary of name-value pairs. In other words, instead of a single name-value, I want to add a name-value collection in there. I could do this:
11
2986
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 certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
1
2568
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 I find a duplicate username in the database, I want to insert a message into the validationsummary control like "username already exists, please enter a different username". How can I do this?
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1607
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.