473,804 Members | 3,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is equivalent of 'null'

I'm a VB programmer learning C#. One thing I don't know is how to
test whether an object is 'null' or not. There is no 'null' keyword
in C#.
Why is that?
CM
Feb 27 '08 #1
8 1790
CM,

I think you have it the other way around. There most definitely is a
null keyword in C#, but not one in VB. In VB, the equivalent is 'Nothing'.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<CO*********@ly cos.comwrote in message
news:a2******** *************** ***********@28g 2000hsw.googleg roups.com...
I'm a VB programmer learning C#. One thing I don't know is how to
test whether an object is 'null' or not. There is no 'null' keyword
in C#.
Why is that?
CM

Feb 27 '08 #2
There is a null:

if(var == null)
//Do something

If you are talking nullable types, you have to test against the type, not
against null, as nullable types are a bit of a kludge. There are also
certain types that cannot be null, unless made with a nullable type.

//default = null, but not the keyword
int? nullableInt;

//default = 0
int nonNullableint;

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************** *************** *************** ****
| Think outside the box!
|
*************** *************** *************** ****
<CO*********@ly cos.comwrote in message
news:a2******** *************** ***********@28g 2000hsw.googleg roups.com...
I'm a VB programmer learning C#. One thing I don't know is how to
test whether an object is 'null' or not. There is no 'null' keyword
in C#.
Why is that?
CM

Feb 27 '08 #3
On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo... @comcast.netNoS pamMwrote:
Yep. I thought about that later and realized I was being overstressed, and,
therefore, stupid.

if(!nullableInt .HasValue)
{

}

is the correct value for nullable types.
It certainly gives the right result. I think I'd use

if (nullableInt==n ull)

for the sake of idiom, but each to their own :)
The rest of the answer was, in
fact, correct, as many types have no concept of null. An int is the perfect
example here.
Sure - but for every non-nullable type there's a nullable equivalent.

Jon
Feb 28 '08 #4
I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :-)

Thanks for watchdogging. I am using the newsgroups as a stress release and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************** *************** *************** ****
| Think outside the box!
|
*************** *************** *************** ****
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:01******** *************** ***********@2g2 000hsn.googlegr oups.com...
On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo... @comcast.netNoS pamMwrote:
>Yep. I thought about that later and realized I was being overstressed,
and,
therefore, stupid.

if(!nullableIn t.HasValue)
{

}

is the correct value for nullable types.

It certainly gives the right result. I think I'd use

if (nullableInt==n ull)

for the sake of idiom, but each to their own :)
>The rest of the answer was, in
fact, correct, as many types have no concept of null. An int is the
perfect
example here.

Sure - but for every non-nullable type there's a nullable equivalent.

Jon

Feb 28 '08 #5
On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo... @comcast.netNoS pamMwrote:
I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :-)
Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.

What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.
Thanks for watchdogging. I am using the newsgroups as a stress release and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.
So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)

Jon
Feb 28 '08 #6

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:f3******** *************** ***********@i12 g2000prf.google groups.com...
On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo... @comcast.netNoS pamMwrote:
>I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :-)

Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.
This is a bigger issue than many think, and not just on nullable types. When
you bounce from language to language, you often find that the different way
features are handled causes you to get into situations where you end up
answering a question incorrectly for one answer that would be correct in
another.

I spend so little time in VB any more, but there are occasions where someone
has me examine some VB code and I have to.
What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.
I am not sure it would do much for me, but perhaps I will get more time to
ponder it later. I have, however, set up reference types that had default
values, so perhaps that would be an reason for a non-nullable reference
types.

NOTE: I am not fond of hard coded initialization of values, but it was a
better option than the client had done intially, which was chain behavior to
a constructor.
>Thanks for watchdogging. I am using the newsgroups as a stress release
and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.

So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)

When I look back a year ago at some code, I think "man I sucked then". The
truth is, I will probably say the same in another six months to a year. And,
this is a good thing, as it means I am growing. The day I look at old code
and say "man, that is the best I have ever done" is the day I should go out
to pasture. :-)

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************** *************** *************** ****
| Think outside the box!
|
*************** *************** *************** ****
Feb 28 '08 #7

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:f3******** *************** ***********@i12 g2000prf.google groups.com...
On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo... @comcast.netNoS pamMwrote:
>I agree completely on the "for every non-nullable", but it is a bit of a
kludge in many ways. :-)

Sort of. I can't think of a more elegant solution though. Admittedly
it's slightly worrying that different languages have different
operator behaviour - equality of nullable types in VB is different to
that in C#, for example.

What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.
>Thanks for watchdogging. I am using the newsgroups as a stress release
and
sometimes the stress is way too high that my brain does not work. Between
the job and Miranda's cancer, it has been a rough few months. Sincerely,
thanks for keeping me honest on this one.

So long as you'll do me the favour in return :)

(I worry sometimes that people don't want to correct MVPs. Everyone is
highly fallible, IME.)

Jon
Oh, don't worry, Jon. We're all just sitting here waiting with bated breath
for you to make a mistake so we can correct you. <g>

Still waiting...
Still waiting... Sigh.

RobinS.
GoldMail, Inc.

Mar 6 '08 #8
On 28 Feb., 16:57, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
What I'd like to see is non-nullable reference types. That could be
useful in a number of ways - although given the large body of code
already out there, it's probably too late.
I think that would be a really useful feature, in addition to
parameters and return values, it would also be nice to be able to
specify it for properties.
Maybe you should add this to your C# 4 wishlist, who knows, they might
listen ;)

Kevin Wienhold
Mar 6 '08 #9

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

Similar topics

112
10372
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please, share your experience in using IDENTITY as PK .
19
1847
by: Jerry | last post by:
Hi,all I write a function to copy one memory buffer to anther with dynamic allocationg when the destination is not large enough. The code is below: void DSM_CC::fillBuf(unsigned char** pdstBuf,int* pdstBufBase,int* pdstTolalLen,char* pbufToWrite,int lenToWrite) { unsigned char* tmp; int step = 256;
6
5590
by: arkam | last post by:
Hi, I found a sample on internet : formMain.BeginInvoke(new MyClickHandler(this.OnMyClick)); I would like to do the same but in a class library where there is no forms ! Where can I find a BeginInvoke equivalent for a class library ?
13
5064
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
3
8374
by: Will Chamberlain | last post by:
I have looked around and found the equivalent for IIF (Access) to be a SELECT CASE in SQL. I have tried this with no success. I am also looking for the equivalent of MAX and have had no luck. The portion of the string I am trying to SQL'ize is: SELECT Max(IIf( Is Null,0,)) AS ADCN FROM ADCN INNER JOIN Sheet ON ADCN.RecordID = Sheet.RecordID WHERE (Sheet.Drawing = '" & x & "') AND (Sheet.SheetNumber = 0);
32
4063
by: Andrew Poulos | last post by:
I'm writing some ASP using js and I need to do a case sensitive SQL select. Googling gave me this: SELECT * FROM User WHERE Strcomp("Blue",,vbBinaryCompare)=0 Strcomp is from vbs. Is there a js equivalent, or some other way to handle this? Andrew Poulos
46
3699
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
2
1384
by: Carl Johansson | last post by:
Except for the syntax, what is the difference, if any, between this... /* ---------------------------------------------------------------------- */ private void Form1_Paint(object sender, PaintEventArgs e) { Pen pen = null; try { pen = new Pen(Color.MidnightBlue, 1.0F);
5
6913
by: iceman19860106 | last post by:
hello everyone! what is the difference between NULL and 0 in C language?
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
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
7622
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
6856
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
5525
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
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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

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.