473,386 Members | 1,753 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,386 software developers and data experts.

Variable scope bug?

I've got a problem with the scope of a variable, consider
the following example:
MyCOMObject.MyObjectClass obj;

try
{
obj = new MyCOMObject.MyObjectClass();
returnXml = obj.getEnquiryUsers();
}
catch (Exception e)
{
//handle error
}
finally
{
if (obj!=null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}

This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.

This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?

Thanks
Andrew

Jul 21 '05 #1
3 1032
If the statement "new MyCOMObject.MyObjectClass()" were to throw an
exception then obj would not have been assigned, and thus the warning in the
finally clause.

You need to decide whether exceptions resulting from creating the object
need to be handled here (in which case set obj to null beforehand), or
whether you aren't handling them here (in which case create the object
outside the try block).

HTH,

Stu
"Andrew Todd" <an*********@ukonline.co.uk> wrote in message
news:0b****************************@phx.gbl...
I've got a problem with the scope of a variable, consider
the following example:
MyCOMObject.MyObjectClass obj;

try
{
obj = new MyCOMObject.MyObjectClass();
returnXml = obj.getEnquiryUsers();
}
catch (Exception e)
{
//handle error
}
finally
{
if (obj!=null)
{
Marshal.ReleaseComObject(obj);
obj = null;
}
}

This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.

This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?

Thanks
Andrew

Jul 21 '05 #2
Andrew Todd <an*********@ukonline.co.uk> wrote:
I've got a problem with the scope of a variable, consider
the following example:
<snip>
This won't compile, it tells me that the variable obj is
unassigned when I test if it's null in the finally
statement.
Indeed, and it's right. Set it to null to start with. Local variables
don't have any default value; they need to be definitely assigned
before they're read.
This doesn't seem correct to me; I need to release the
COM object in the finally statement as if there is an
error in the try block the object will never be released.
These steps work in Java and are the steps we normally
take when interfacing with COM objects, is this a bug?


No, it wouldn't work in Java. Here's an example showing it:

public class Test
{
public static void main(String[] args)
{
String x;
try
{
x = "hello";
}
catch (Exception e)
{
System.out.println (e);
}
finally
{
if (x==null)
{
System.out.println ("Oops");
}
}
}
}

And compiling it:

Test.java:16: variable x might not have been initialized
if (x==null)
^
1 error

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
Indeed, and it's right. Set it to null to start with. No, it wouldn't work in Java. Here's an example showing

it:

Of course - you're right, I'd forgotten to set it to null
initially!

Thanks
Andrew
Jul 21 '05 #4

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

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.