Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, so i have 2 labels. one of them is a fixed number and doesn't change but the other one changes every 5 seconds. Now i want to multiply them automatically and show them in another label as Results.

what should i do? what am i doing wrong?

What I have tried:

i tried this code but it says "operator * cannot be applied to string and string".

label1.Text = BTC_A.Text * BTCPrice_Label.Text;


then i tried

double txt1 = Convert.ToDouble(BTC_A.Text);
            double txt2 = Convert.ToDouble(BTCPrice_Label.Text);

            double sum = txt1 * txt2;

            label1.Text = sum.ToString();


but it says "Input string was not in a correct format"
Posted
Updated 16-Oct-20 21:58pm

1 solution

It can't.
What do you get when you multiply "bread is great" by "in a sandwich"?
Nothing - it's a silly thing to try and do.

But the framework doesn't know what the user is going to type - it might be a number, it might not. So your second attempt is better, but also flawed because users make mistakes.

Try this:
C#
double price;
if (!double.TryParse(BTCPrice_Label.Text, out price))
   {
   ... report problem to user ...
   return;
   }
double btcA;
if (!double.TryParse(BTCPrice_Label.Text, out btcA))
   {
   ... report problem to user ...
   return;
   }
label1.Text = (price * btcA).Tostring();

That way, your app won't crash when there is a problem!
 
Share this answer
 
Comments
brandon1999 17-Oct-20 4:09am    
thank you for explaining. i used the code and now i can debug my program but it doesn't show the results!!

this is exactly what i did with the code. i put // cause i got errors and i don't know what should i do with that. ( i know this is probably really easy but i'm still learning a lot of basic things )

double AA;
if (!double.TryParse(BTC_A.Text, out AA))
{
// ... report problem to user ...

return;
}
double btcA;
if (!double.TryParse(BTCPrice_Label.Text, out btcA))
{
// ... report problem to user ...
return;
}
label1.Text = (AA * btcA).ToString();
OriginalGriff 17-Oct-20 4:24am    
The idea is that you replace the line you commented out with code that reports a problem: Console.WriteLine perhaps, or Messagebox.Show maybe, even an Alert is you are coding a website - we have no idea what environment you are running in, so I can;t be specific about what to put there - that's why it was just "... report problem to user ..." so you would realize you had to do something there. If you just comment it out, it won't do anything!

Put a breakpoint on the two return statements and let the debugger stop when it hits them - you can then see what it was processing.
brandon1999 17-Oct-20 4:41am    
oh, now i understand that. thank you so much.
so i did the things you said, and also i put two breakpoints but i'm not getting any errors! it just doesn't multiply the numbers! and it keeps showing me the message boxes.

my application is in winforms ( https://imgur.com/U5nxs1J ).
label1.Text is the Result label, BTC_A.Text is the "0.01741294" label and BTCPrice_Label.Text is the label that changes every 5 seconds and shows the btc price
this is what it looks like now:

double AA;
if (!double.TryParse(BTC_A.Text, out AA))
{
MessageBox.Show("There is a Problem");
return;
}
double btcA;
if (!double.TryParse(BTCPrice_Label.Text, out btcA))
{
MessageBox.Show("There is a Problem");
return;
}
label1.Text = (AA * btcA).ToString();
OriginalGriff 17-Oct-20 4:52am    
So look at exactly what is in the values before you convert them.
Which breakpoint did it hit?
What was the value in the string?

You are using the debugger, yes?

Also change your message:

MessageBox.Show($"Unable to convert the BTC_A \"{BTC_A.Text}\" to a floating point number");

And

MessageBox.Show($"Unable to convert the price \"{BTCPrice_Label.Text}\" to a floating point number");

Any error which just says "it don't work" isn't a lot of use without piles of context!
brandon1999 17-Oct-20 5:32am    
thank you so much. i found the problem it was the dollar sign that i put before the number i just deleted the sign and it showed the results. thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900