Simple C# type conversion problem

  • Thread starter Thread starter Eric
  • Start date Start date
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
 
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!
 
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
 
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);
}
 
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
 
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
 
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
 
Back
Top