473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about try/catch for null variables

Is there a better way (faster, cleaner) of checking for a null than
the below? I tried various other ways (commented out) and they give
run-time errors.

treeView1.Node[999] does not exist, it's a null.

TX

RL

try
{
if (treeView1.Nodes[999] == null)
throw new ArgumentException("Node cannot be
null!");
}

//if ((this.treeView1.Nodes[999] == null)) //doesn't work,
compiles but run time error
//{
// TreeNode bogusTreeNode = treeView1.Nodes[999];
// MessageBox.Show("bogus node!!");
//}
//next line will will trigger exception "out of bounds",
run-time
//if (!(bogusTreeNode.Parent != null))
{ MessageBox.Show("bogus node!"); }

catch (ArgumentException ex)
{
// MessageBox.Show("exception! bogus node"); //works
}
Jul 25 '08 #1
2 960
On Jul 25, 1:45*pm, raylopez99 <raylope...@yahoo.comwrote:
Is there a better way (faster, cleaner) of checking for a null than
the below? *I tried various other ways (commented out) and they give
run-time errors.

treeView1.Node[999] does not exist, it's a null.

TX

RL

* * * * * * try
* * * * * * {
* * * * * * * * if (treeView1.Nodes[999] == null)
* * * * * * * * * * throw new ArgumentException("Nodecannot be
null!");
* * * * * * }

* * * * * * //if ((this.treeView1.Nodes[999] == null)) //doesn't work,
compiles but run time error
* * * * * * //{
* * * * * * // * *TreeNode bogusTreeNode = treeView1.Nodes[999];
* * * * * * // * *MessageBox.Show("bogus node!!");
* * * * * * //}
* * * * * * //next line will will trigger exception "out of bounds",
run-time
* * * * * * //if (!(bogusTreeNode.Parent *!= null))
{ MessageBox.Show("bogus node!"); }

* * * * * * catch (ArgumentException ex)
* * * * * * {
* * * * * * * // *MessageBox.Show("exception! bogus node"); //works
* * * * * * }
what about treeView.Nodes.Count < 999
Jul 25 '08 #2
On Jul 25, 12:09*pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Jul 25, 1:45*pm, raylopez99 <raylope...@yahoo.comwrote:
Is there a better way (faster, cleaner) of checking for a null than
the below? *I tried various other ways (commented out) and they give
run-time errors.
treeView1.Node[999] does not exist, it's a null.
* * * * * * }

what about treeView.Nodes.Count < 999
Good one, that makes sense!

Tx

RL

Jul 25 '08 #3

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

Similar topics

1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
12
by: Jeff B. | last post by:
If an object implements the IDisposable interface, should I always call the Dispose method or is just setting it to null and letting the GC handle it sufficient? Here is the pattern I've been...
5
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr =...
10
by: Nemisis | last post by:
HI everyone, I think i am on the right path but if someone could confirm it, that would be great. In my business Layer i have my business object, for this example, called User. User has...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
1
by: eddie | last post by:
I have a form. The form submits correctly and the php script GET's the ?variables=whatever&variables2=whatever2, but when I try and use AJAX, I receive no errors, my innerHTML is changed with the...
28
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " ...
0
by: Chris.Mcrae | last post by:
Hello and happy holidays all, I have a quick question. I just playing with interoperability, so I created a java web service (using Axis), which I am able to access with a java client, but when...
14
by: Jake K | last post by:
How do I access variables that are set within a try/catch block? The following code produces a use of unassigned local variable error: string varOne; try { //access database here, select,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.