473,473 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Create 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 1776
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.com

<CO*********@lycos.comwrote in message
news:a2**********************************@28g2000h sw.googlegroups.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*********@lycos.comwrote in message
news:a2**********************************@28g2000h sw.googlegroups.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.netNoSpamMwrote:
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==null)

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.comwrote in message
news:01**********************************@2g2000hs n.googlegroups.com...
On Feb 28, 3:21 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamMwrote:
>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==null)

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.netNoSpamMwrote:
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.comwrote in message
news:f3**********************************@i12g2000 prf.googlegroups.com...
On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamMwrote:
>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.comwrote in message
news:f3**********************************@i12g2000 prf.googlegroups.com...
On Feb 28, 3:37 pm, "Cowboy \(Gregory A. Beamer\)"
<NoSpamMgbwo...@comcast.netNoSpamMwrote:
>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.comwrote:
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
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,...
19
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**...
6
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...
13
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
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...
32
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...
46
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
by: Carl Johansson | last post by:
Except for the syntax, what is the difference, if any, between this... /* ---------------------------------------------------------------------- */ private void Form1_Paint(object sender,...
5
by: iceman19860106 | last post by:
hello everyone! what is the difference between NULL and 0 in C language?
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...
1
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...
1
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
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
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
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.