Hi - should the following compile? I ask because a Guid is a struct and can
never be null... The worst the compiler throws at me is an "unreachable
code" warning.
public string GetData(Guid id)
{
if (id == null)
throw new ArgumentNotNullException("");
string s = null;
// do stuff...
return s;
}
Thanks,
/Peter 7 23217
On Aug 8, 2:08 pm, Peter K <xdz...@hotmail.comwrote:
Hi - should the following compile? I ask because a Guid is a struct and can
never be null... The worst the compiler throws at me is an "unreachable
code" warning.
Yes, it should compile - at least in C# 2. It's actually effectively
doing this:
if ((Guid?)id == null)
Unfortunate, but that's the way the lifted operators for nullable
types work.
Jon
Acutally, in 2.0, you can declare nullable types as such:
Nullable<Guidg = null;
Nullable<boolb = null;
This way you can be sure the code will work.
On Aug 8, 2:48 pm, "madhatter2...@gmail.com" <madhatter2...@gmail.com>
wrote:
Acutally, in 2.0, you can declare nullable types as such:
Nullable<Guidg = null;
Nullable<boolb = null;
This way you can be sure the code will work.
The point is that the OP doesn't *want* a nullable GUID - he's just
surprised that you can compare a value type with null, which is quite
a reasonable thing to be surprised about.
(Personally I prefer the C# syntax for nullables - Guid? instead of
Nullable<Guidetc.)
Jon
Peter,
Two things. First, as Jon mentioned, the code will compile. However,
whether or not it will run correctly is another story. As the compiler has
pointed out to you, the code that throws the exception will never be
reached. This is a warning (which the compiler does not emit in C# 3.0),
and if you have the "treat warnings as errors" flag set in your project, it
will not compile.
The reason, of course, is because the id parameter can never be null,
since Guid is the type (which is a value type) and it is not nullable.
Now, at runtime, you are going to have a problem further up in the call
stack if you try and pass null.
The second thing is that there is actually a "null guid" which is a guid
with all zeros for values.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Peter K" <xd****@hotmail.comwrote in message
news:e%***************@TK2MSFTNGP02.phx.gbl...
Hi - should the following compile? I ask because a Guid is a struct and
can
never be null... The worst the compiler throws at me is an "unreachable
code" warning.
public string GetData(Guid id)
{
if (id == null)
throw new ArgumentNotNullException("");
string s = null;
// do stuff...
return s;
}
Thanks,
/Peter
On Aug 8, 3:20 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Two things. First, as Jon mentioned, the code will compile. However,
whether or not it will run correctly is another story. As the compiler has
pointed out to you, the code that throws the exception will never be
reached. This is a warning (which the compiler does not emit in C# 3.0),
and if you have the "treat warnings as errors" flag set in your project, it
will not compile.
The reason, of course, is because the id parameter can never be null,
since Guid is the type (which is a value type) and it is not nullable.
Agreed.
Now, at runtime, you are going to have a problem further up in the call
stack if you try and pass null.
No, that error will occur at compile time instead.
The second thing is that there is actually a "null guid" which is a guid
with all zeros for values.
I think it's clearer to call that the "empty" GUID because:
1) It's available using Guid.Empty
2) It allows you to talk about a null Guid? vs a Guid? with a value of
Guid.Empty
Jon
Jon,
> Now, at runtime, you are going to have a problem further up in the call stack if you try and pass null.
No, that error will occur at compile time instead.
If you pass null directly to the method, yes, it will occur at compile
time, but I was thinking of this case:
object o = null;
string s = GetData((Guid) o);
Granted, the OP isn't going to do that directly, but anything that
involves a cast to Guid will potentially make this a run-time error.
> The second thing is that there is actually a "null guid" which is a guid with all zeros for values.
I think it's clearer to call that the "empty" GUID because:
1) It's available using Guid.Empty
2) It allows you to talk about a null Guid? vs a Guid? with a value of
Guid.Empty
I agree, I'm thinking of the COM context of referring to a null Guid,
which is where it all originated, of course. Thankfully, they didn't name
the null guid property on Guid Null.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
>
Jon
On Aug 8, 3:46 pm, "Nicholas Paldino [.NET/C# MVP]"
Now, at runtime, you are going to have a problem further up in the
call
stack if you try and pass null.
No, that error will occur at compile time instead.
If you pass null directly to the method, yes, it will occur at compile
time, but I was thinking of this case:
object o = null;
string s = GetData((Guid) o);
Ah, yes. That would go bang regardless of the body of GetData, of
course.
Granted, the OP isn't going to do that directly, but anything that
involves a cast to Guid will potentially make this a run-time error.
True.
The second thing is that there is actually a "null guid" which is a
guid
with all zeros for values.
I think it's clearer to call that the "empty" GUID because:
1) It's available using Guid.Empty
2) It allows you to talk about a null Guid? vs a Guid? with a value of
Guid.Empty
I agree, I'm thinking of the COM context of referring to a null Guid,
which is where it all originated, of course. Thankfully, they didn't name
the null guid property on Guid Null.
Indeed. It wouldn't have seemed *too* bad in .NET 1.1, but with
nullable types it would have been pretty awful.
Jon This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: BigDadyWeaver |
last post by:
I am using the following code in asp to define a unique and
unpredictable record ID in Access.
<%
'GENERATE UNIQUE ID
Function genguid()
Dim...
|
by: Rene |
last post by:
I am using the Guid.Empty value ("00000000-0000-0000-0000-000000000000") to
represent a special meaning. The problem is that I don't know if there...
|
by: Steve Richter |
last post by:
this code compiles ok:
public string AssureAnonUID( )
{
if (Session == null)
Session = Guid.NewGuid();
return Session.ToString();
}
but this...
|
by: Geoff Jones |
last post by:
Hi
How do I set a primary key to be a GUID?
That is, using the following code, I can set an integer primary key, but how
do I change it to a...
|
by: mPiccoli |
last post by:
how I can a Guid field on Null setting (DATASET)
|
by: AlveenX |
last post by:
Hi,
I am trying to pick a Guid from a data row using the following code:
foreach(DataRow row in MyDataTable.Rows)
{
(Guid)row
}
|
by: Matt Michler |
last post by:
I have a WebUserControl with a custom event, when this event is fired,
the delegate or event object is throwing a null reference exception.
The odd...
|
by: sawaipurush |
last post by:
I am trying to write an application that will detect all the USB devices connected to my computer. For this I have declared GUID and called the...
|
by: shapper |
last post by:
Hello,
I am inserting an item on a XML file and I have something like this:
paper.ID ?? Guid.NewGuid()
Basically my idea was that if...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
| |