ToString does not work in my example

T

Tony Johansson

Hello!

This works fine but if I for example replace
this statement
result = firstOperand + secondOperand;
with this
SetTextValue((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture));
it doesn't work because ToString doesn't appear in intellisense when I
enter the . after
writing ...+ secondOperand).

Can somebody explain why it doest work in my case ?

You have all the relevant methods below.
private void Calculate()
{
decimal result=0;
switch (selectedOperator)
{
case '+': result = firstOperand + secondOperand;
break;
case '-': result = firstOperand - secondOperand;
break;
case '*': result = firstOperand * secondOperand;
break;
case '/': result = firstOperand / secondOperand;
break;
}

SetTextValue(result.ToString(CultureInfo.InvariantCulture));
selectedOperator = '\0';
}

private void SetTextValue(string textValue)
{
txtDisplay.Text = textValue;
}

//Tony
 
S

Scott M.

If when you type the dot (.), you don't get intelliSense, it's because the
compiler doesn't expext an object returned by your statement.

Your "SetTextValue" method is declared as "void" below and that means that
it is a method that does not return a value, so there is no object to call
ToString() on.

Change the method declaration from void to the type you want returned and
you'll be able to access intelliSense on the result.

-Scott
 
T

Tony Johansson

Hello!

I mean that in this expression
SetTextValue((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture));
should firstOperand be added to secondOperand which
firstOperand + secondOperand is saying then
the decimal value you get from firstOperand + secondOperand should be casted
to string by using ToString
then this string should be passed into method SetTextValue

So I don't still understand why I can't use ToString on firstOperand +
secondOperand by using
ToString in this way
SetTextValue((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture));

//Tony
 
S

Scott M.

Try adding another level of parenthesis so that your addition will be
evaluated first, the ToString() call second and the SetTextValue third:

SetTextValue(((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture)));
 
T

Tony Johansson

Hello!

This seems to be a bug or something like that.
I have to keep it in this way
result = firstOperand + secondOperand;
and then use
SetTextValue(result.ToString(CultureInfo.InvariantCulture));


//Tony
 
G

Göran Andersson

Tony said:
Hello!

This works fine but if I for example replace
this statement
result = firstOperand + secondOperand;
with this
SetTextValue((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture));
it doesn't work because ToString doesn't appear in intellisense when I
enter the . after
writing ...+ secondOperand).

Can somebody explain why it doest work in my case ?

Either the data type of the variables are not decimal, or you have some
syntax error somewhere else in your code that keeps the intellisense
from working properly.

It would just fine when I try it like this:

decimal firstOperand = 1;
decimal secondOperand = 2;

(firstOperand + secondOperand).
 
G

Göran Andersson

Scott said:
Try adding another level of parenthesis so that your addition will be
evaluated first, the ToString() call second and the SetTextValue third:

SetTextValue(((firstOperand +
secondOperand).ToString(CultureInfo.InvariantCulture)));

There is already parentheses enough. Also, you added another pair around
the entire expression, which doesn't change anything inside the expression.
 
T

Tony Johansson

Hello!

Here I have a very simple example

decimal d1 = 1;
decimal d2 = 2;
string s = (d1+d2).ToString();

This simple example above doesn't work for some reason that I don't know.
I hope that somebody can explain for me why ?

//Tony
 
S

Scott M.

Yes, you have a problem all right. Did you try this in a brand new project?


Tony Johansson said:
Hello!

Here I have a very simple example

decimal d1 = 1;
decimal d2 = 2;
string s = (d1+d2).ToString();

This simple example above doesn't work for some reason that I don't know.
I hope that somebody can explain for me why ?

//Tony
 
T

Tony Johansson

Hello!

It works if I just check string s when debugging I can see that the result
is correct.

It was just the intellisense that wasn't smart enough to display the
ToString

//Tony


Scott M. said:
Yes, you have a problem all right. Did you try this in a brand new
project?
 
S

Scott M.

Here's a silly question: are you sure you haven't changed your intellisense
settings in Visual Studio?
 
T

Tony Johansson

Hello!

No.

If I for example do the following I get the intellisense
decimal d = 1;
string s = d.ToString();
when writing d. the intellisense popup showing all the alternatives.

//Tony
 
T

Tony Johansson

Hello!

I mean that why will not the intellisense popup when having this expression
decimal d1 = 1;
decimal d2 = 2;
string s = (d1+d2).Here should the intellisense popup showing all the
alternatives.

If I instead have the following.
decimal d1 = 1;
string s = d1.Here the intellisense work and show me all the alternatives.

So why does the intellisense popup in the second example but not in the
first?

//Tony

Peter Duniho said:
[...]
decimal d1 = 1;
decimal d2 = 2;
string s = (d1+d2).ToString();

This simple example above doesn't work for some reason that I don't know.
I hope that somebody can explain for me why ?

Nope. You haven't explained in what way it "doesn't work", so there's no
way for anyone to explain _why_ it "doesn't work".

Post a concise-but-complete code example that reliably demonstrates the
problem, AND be very clear about what the code does, and what you expected
it to do instead. Then someone can explain it for you.

Pete
 
R

Rick Lones

Michael said:
I tried to replicate his problem in VS 2008 with the program quoted
below in its entirety -- and the problem did not appear.

When typing the dot immediately preceding "ToString" in each of the 2
places, I got IntelliSense and picked ToString as a menu option.

// VS 2008 console application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Decimal d1, d2;
String s1 = d1.ToString();
String s2 = (d1 + d2).ToString();
}
}
}

In VS 2005, Intellisense does not figure it out as written above. But it does like:

String s2 = ((decimal)(d1 + d2)).ToString();

FWIW,
-rick-
 

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