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

Passing exception to function.

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

Nov 15 '05 #1
4 1951
> 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
Nov 15 '05 #2
> 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.

Nov 15 '05 #3
Ryan <Ry**@lmp.co.uk> wrote:
and this is it. you cannot call non-static method from a static one without
an object reference.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
> 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

Nov 15 '05 #5

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

Similar topics

8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
8
by: Drew | last post by:
I have recently converted all of my native C++ dll's to be compiled with the /CLR switch. After doing this, I notcied a very strange and frustrating issue. One of my managed dll's calls another...
13
by: anonymike | last post by:
Hello, I started working with the ObjectDataSource today. I have the select, and have been working on getting the update method to work. Here is the asp code for my Data source: ...
5
by: Sridhar | last post by:
Hi, I have created a web application. I have a data access dll to interact with database. I added this dll as a reference to the web application. Now let's say if I am calling a function in data...
0
by: kencana | last post by:
hi All, I got problem in passing data (more than one) from soap client to the soap server. but if i only passing one data at a time, it works successfully.. The following is the error message i...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
18
by: Carl Forsman | last post by:
there are 2 ways to return a value from a function ==================== 1) passing a reference as parameter - the following will return time void Table::Get(char* FieldName, *SYSTEMTIME time) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.