Simple C# type conversion problem

E

Eric

Hello,

I'm trying to write a really simple C# test harness app, can someone
tell me why this doesn't work?

catch (Exception ex) {
string MyException = ex.ToString;
Console.WriteLine(MyException);
}

I get:

Error 10 Cannot convert method group 'ToString' to non-delegate type
'string'. Did you intend to invoke the method?

Thanks,
Eric
 
L

Larry Smith

I'm trying to write a really simple C# test harness app, can someone
tell me why this doesn't work?

catch (Exception ex) {
string MyException = ex.ToString;
Console.WriteLine(MyException);
}

I get:

Error 10 Cannot convert method group 'ToString' to non-delegate type
'string'. Did you intend to invoke the method?

ex.ToString(); // Brackets!
 
B

Barry Kelly

Eric said:
Hello,

I'm trying to write a really simple C# test harness app, can someone
tell me why this doesn't work?

catch (Exception ex) {
string MyException = ex.ToString;

ToString is a method. You need to put '()' at the end to call it.

If string was a delegate type, then you could create a delegate (a kind
of method pointer) with a similar expression:

delegate string MyDelegate();

// ....

MyDelegate toString = ex.ToString;
toString(); // call ex.ToString()
Console.WriteLine(MyException);
}

I get:

Error 10 Cannot convert method group 'ToString' to non-delegate type
'string'. Did you intend to invoke the method?

Invoking a method is done with the '()' operator.

-- Barry
 
T

Tariq Karim

Hi:
You are trying to call a method ToString. In C# language, you need to
enter braces whenever you are calling a method even if that method does
not take any parameter. Thus the right way to write the code would be:

catch (Exception ex) {
string MyException = ex.ToString();
Console.WriteLine(MyException);
}
 
?

=?iso-8859-2?Q?Cvijanovi=E6_=D0or=F0e?=

Hello,

I'm trying to write a really simple C# test harness app, can someone
tell me why this doesn't work?

catch (Exception ex) {
string MyException = ex.ToString;
Console.WriteLine(MyException);
}

I get:

Error 10 Cannot convert method group 'ToString' to non-delegate type
'string'. Did you intend to invoke the method?

Thanks,
Eric

Hi,
change line

string MyException = ex.ToString;

to

string MyException = ex.ToString();

ToString is method and must be called with (). I don't know why you do
this, but error message is contained in Message properties of class
Exception. So, your line should be

string MyException = ex.Message;

LP
 
C

Christof Nordiek

I don't know why you do
this, but error message is contained in Message properties of class
Exception. So, your line should be

string MyException = ex.Message;

LP

But exception.ToString() gives more information then exception.Message. It
contains also the type of the Exception (very usefull to lookup in docs) and
the callstack aswell as all inner exceptions.

Christof
 
M

Mythran

Eric said:
Hello,

I'm trying to write a really simple C# test harness app, can someone
tell me why this doesn't work?

catch (Exception ex) {
string MyException = ex.ToString;
Console.WriteLine(MyException);
}

I get:

Error 10 Cannot convert method group 'ToString' to non-delegate type
'string'. Did you intend to invoke the method?

Thanks,
Eric

To clarify some things...and hopefully I get it right...

First off, from the other posts, they mention the words "brackets" and
"braces"...for me, brackets are '[' and ']', while braces are '{' and '}'
which are also referred to as curly braces. Parenthesis are '(' and
')'...which is what you are missing. I know and understand that it's not
that important on what they are called, but I'm trying to avoid confusion
that occurs when calling 2 different syntactical elements the same name...

HTH,
Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top