473,734 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Have there any function to check the null and empty at once

ad
I have a string like string sNo.
I want to cast it to int with int.Parse(sNo)
but if the string is null or empty, it will thow exception.

I must check if sNo is not null and is not empty before cast like:
if ((sNo<>null) && (sNo<>""))

Have there any function to check the null and "" at once
Nov 17 '05 #1
2 3960
ad wrote:
I have a string like string sNo.
I want to cast it to int with int.Parse(sNo)
but if the string is null or empty, it will thow exception.

I must check if sNo is not null and is not empty before cast like:
if ((sNo<>null) && (sNo<>""))

Have there any function to check the null and "" at once


Prior to .NET 2.0, you must provide your own solution.
In .NET 2.0, there's String.IsNullOr Empty().

Cheers,
--
http://www.joergjooss.de
mailto:ne****** **@joergjooss.d e
Nov 17 '05 #2
ad wrote:
I have a string like string sNo.
I want to cast it to int with int.Parse(sNo)
but if the string is null or empty, it will thow exception.

I must check if sNo is not null and is not empty before cast like:
if ((sNo<>null) && (sNo<>""))

Have there any function to check the null and "" at once


Well, first of all, it's
if ((sNo != null) && (sNo != "")

as <> is a syntax error. Also, there's not much sense is checking if a
string is less than "", since it can't be. I prefer
if ( sNo != null && sNo.Length > 0)
but that's just me.

As Joerg pointed out, in .Net 2.0 is could use:
if (String.IsNullO rEmpty(sNo))
But, let's no much less typing what you've got now. Ultimately, the problem
is that we really want to write :

if (sNo.IsNullorEm pty())

but there is no way to get that to work is sNo is null.
--
Truth,
James Curran [erstwhile-MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 17 '05 #3

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

Similar topics

7
2350
by: George Marshall | last post by:
Hi all, my question is what should I do with a pointer to be used inside a function ? The following function should take a pointer to a null terminated array of chars, do something with it, and write to the other pointer (*dest). Should I check if the *dest pointer is NULL or not before writing to it ? Should I realloc it before I write to it ? Should I free it if != NULL and
4
34556
by: Hamed | last post by:
Hello How can I check if a string is empty? Thanks
2
1885
by: Amil | last post by:
Hi all, I call a stored procedure that returns a bunch of output parameters. And all of those are nullable data. Is this the best approach to validating the value? I don't want the value checked using IF and I am not using SqlDataTypes as well. itemId = parms.Value != null || !parms.Value.ToString().Equals(string.Empty) ? (int)parms.Value : 0; itemGroupGuid = parms.Value != null || !parms.Value.ToString().Equals(Guid.Empty) ?...
4
3188
by: whisher | last post by:
Hi. I'm taking my first steps on regex I set up this simple function to check if a form field is empty or with only space. var onlySpaceRegexp = /^\s*$/; function isEmpty(val) { if (onlySpaceRegexp.test(val) || val == "") {
11
13166
Scieck
by: Scieck | last post by:
Hi everyone ! I got hold of a valid IWebBrowser2 object. I am passing it to a function that should return the value of href in a link tag in the IHTMLDocument3 object, here is the function: LPCTSTR CInternetExplorer::GetHref(IWebBrowser2 *pWb) { CString empty(""); CComQIPtr<IHTMLDocument3> doc3 = (IHTMLDocument3*)pWb; if (doc3 == NULL)
3
12808
by: Rowan | last post by:
When I run a small test script I get the following error. ___________________________ PHP Fatal error: Call to a member function prepare() on a non-object in /usr/local/lib/php5/pg_connect.php on line 73 <snippet_____________________> public static function db_update($sql) { $str_check = explode(" ", $sql); if($str_check == "INSERT") { if (!isset($dbh)){
4
3746
by: Chronictank | last post by:
Hi, as a bit of background (and seeing as it is my first post :)) i am a complete newbie at perl. I literally picked it up a week ago to do this project as it seemed like the best choice for a language to connect to a SQL database in a Unix envirment as well as script parsing so there is more than likely a ton of things i could have done a better way. I have been at this for 3 days now and cant for the life of me work out why the code is...
0
1581
by: perdoname | last post by:
Hello, I urgently need help, im trying to complete a class which will be able of multiple - clients but is not doing the required puprose. Can anyone be assistance of what is needed to add for handling the clients ?? Thanks in advance!! import java.util.ArrayList;
2
14658
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
0
8946
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
8776
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
8186
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...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.