Passing exception to function.

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

Hi All,

I quite new to c# development but I'm getting to grips with it quite
well comming from a mainly C++ / Delphi background. I've been following
the msdn artical entitled "An Introduction to P/Invoke and Marshaling on
the Microsoft .NET Compact Framework" and in the section entitled
handling errors there is a vb function called HandleCeError which has
the exception passed to it, by value, which is then processed. I've been
trying to convert this to c# code with no luck as I can't seem to get
the parameter correct to pass the excption accross. My code looks like this:

private void HandleCeError(ref Exception MyException, string Method)
{
if(MyException is NotSupportedException){
// Show Message
}
else if(MyException is MissingMethodException){
// Show Message
}
else{
// Show Message
}
}

Then when I do:

try{
// Platform Invoke Code
}
catch(Exception e){
HandleCeError(ref e, "My Function");
}

and compile it I get:

An object reference is required for the nonstatic field, method, or
property 'HandleCeError(ref System.Exception, string)'

and this is the same with or without the Ref keyword. Can anyone point
me in the right direction as to where I'm going wrong.

Thanks
Ryan
 
and this is the same with or without the Ref keyword. Can anyone point
me in the right direction as to where I'm going wrong.

the problem is not in the Exception itself, but rather in your class
definition. note that the HandleCeError method is not static. the
compilation error says that you call the method from another method that is
static.

static IDoNotKnowTheNameOfYourMethodButItIsSurelyStatis()
{
// this is your code
try{
// Platform Invoke Code
}
catch(Exception e){
HandleCeError(ref e, "My Function");
}
}

and this is it. you cannot call non-static method from a static one without
an object reference.

Regards, Wiktor
 
and this is it. you cannot call non-static method from a static one without
an object reference.

Hi Wiktor,

Thanks for your fast response. It was a problem with the static keyword,
my calling function was defined as public static where as HandleCeError
was declared as private void. When I changed it to private static void
it worked like a charm.

Thanks again
Ryan.
 
Ryan said:
Thanks for your fast response. It was a problem with the static keyword,
my calling function was defined as public static where as HandleCeError
was declared as private void. When I changed it to private static void
it worked like a charm.

One thing about your code puzzles me, which is why you're passing the
exception reference *by* reference. Are you ever modifying the
parameter variable in HandleCeError? You don't in the code you posted,
which makes it odd that you're passing it by reference.
 
parameter variable in HandleCeError? You don't in the code you posted,
which makes it odd that you're passing it by reference.

Hi John,

Thats my mistake because as you say I don't modify the exception error.
When I couldn't get the code working I added the reference keyword
thinking it would help but it didn't. I started to re-read the
documentation and that combined with a few late nights got me in a
muddle. Without any success and confused by reading too many documents I
posted the code without changing it back. The original code does read
properly as I changed it back after getting the reply from Wiktor.

Thanks
Ryan
 
Back
Top