Community Forums Archive

Go Back

Subject:Checking for 'null' in a SF script (C#)
Posted by: cyberbeat
Date:3/31/2008 12:19:41 PM

I've been going through the C# scripts and Im a little confused about something.

Take for example, the following code.

--
ISfDataWnd wnd = app.ActiveWindow;
if (null == wnd)
return "Open one or more files before running this script.";
--

Shouldn't it be written as:

--
ISfDataWnd wnd = app.ActiveWindow;
if (wnd == null)
return "Open one or more files before running this script.";
--

Does it matter if it's null == wnd or wnd == null? Is there a difference?

Thanks.

Subject:RE: Checking for 'null' in a SF script (C#)
Reply by: ForumAdmin
Date:4/1/2008 6:55:02 AM

No difference. It is a comparison, not an assignment.

Crusty C/C++ developers are in the habit of using "if (null == x)" because it catches a common typo. In C/C++, "if (null = x)" is illegal, but "if (x = null)" is legal (and almost always unintended).

C# doesn't allow either of these cases, so it doesn't really matter which side of the comparison the null is on.

J.

Subject:RE: Checking for 'null' in a SF script (C#)
Reply by: cyberbeat
Date:4/1/2008 1:03:10 PM

Awesome, thanks! I thought that was the case, but I wasn't sure because I'm new to C#. Thanks for clearing that up for me.

Go Back