# C# '*' nightmare

1. May 19, 2014

I have made an app for calculating the area of a circle ($\pi r^2$) in VB. It works like a charm.
Then I have tried to make a similar one in C#.
This is my code:
Code (Text):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Area_Circle_C_Sharp
{
public partial class Form1 : Form
{
String wat = "Area";
public Form1()
{
InitializeComponent();
}
private void Area_Click(object sender, EventArgs e)
{
wat = "Area";
Name.Text = "Area=";
Name_wat.Text =System.Math.PI * (Input.Text) ^ 2;
}
}
}
When I click the area button, I want a text called name to be changed to "Area".-Works fine
I also made a variable "wat" to be changed to "Area"-Works fine
I now want to calculate the area using the radius in the Input text box. But Visual Studio says that Operator '*' cannot be applied to operands of type double and string.
What do I do? I used the same thing with VB. "Math.PI*(Input.Text)^2"
What is wrong here?

2. May 19, 2014

### Borg

You probably need to convert the String to a number first. The multipication operator won't do that for you.

3. May 19, 2014

But why does it work in VB?
Anyway, The radius can be a double right? Then does it mean that I can't use anything other than integers as a radius here?

4. May 19, 2014

### Staff: Mentor

Some languages especially scripting language treat all data as strings and convert automatically as needed. Sometimes of course the string data won't be a number and you'll get an error at runtime. To avoid that error compilers adopted a strategy of defined datatypes like integer or float or string and perform compile time checks to make sure they are being used in the correct context and in the long run the compilers produce code that is more compact with less checks that runs faster.

For C# string to number conversions:

http://msdn.microsoft.com/en-us/library/bb397679.aspx

5. May 19, 2014

### D H

Staff Emeritus
Your problem is (Input.Text) ^ 2. That is not doing what you think. There is no exponentiation operator in C#. x^y is the bitwise exclusive or of x and y. You need to convert that text to a number and then use Math.Pow (number, 2.0) instead (or just use number * number).

6. May 19, 2014

### Borg

+1
You can mix doubles and integers. There's no problem multiplying a double with an int to get a new double value. You just have to be careful about where you're putting them. For example, you wouldn't want to put a double value into a variable that's been assigned as in int even though some compilers may allow it. It isn't a good idea to do because ints typically can't be as large - casting large doubles as ints can cause inconsistent results.

7. May 19, 2014

Thanks guys.
C# is more difficult to learn than I previously thought.
Anyways,I really like the fact that I am able to program a little mow. I thought it's magic some year ago lol.

8. May 19, 2014

huh? I just tried to convert it to integers,Decimals,Floats, etc. Nothing works.
What do you normally use in these kinds of situations?

9. May 19, 2014

### D H

Staff Emeritus
We can't help you solve your problem if you don't tell us what the problem is. Show some code and tell us the error message.

10. May 19, 2014

### AlephZero

11. May 19, 2014

### Staff: Mentor

Use the methods in the Convert class in the System namespace.

Convert.ToDouble() takes a string argument and returns the value of that string as a double.
Code (Text):

See http://msdn.microsoft.com/en-us/library/zh1hkw6k(v=vs.100).aspx.
After you have computed the area, use the ToString() method (also in the Convert class) to convert your answer to a string for displaying in the textbox.

These functions are static functions, which means that you don't need to create an instance of the class to use them. Use the name of the class (Convert) followed by a period and then the name of the method.

One other thing: Since you need to square the radius to get the area, there's a temptation to find some exponent operator or function. That's not needed here. The expression radius * radius gives the result you need.

12. May 19, 2014

### Staff: Mentor

VB can maintain so-called polymorphic data. This is a datatype that is somewhat like a chameleon - it can be internally represented multiple ways. It can represent an integer as a string. And vice-versa. This makes for easier programming. It also makes for slower execution and more memory overhead.

VB does this trick by using late binding. Late binding actually enables you to implement a sort of ''pure'' polymorphism. Late binding means that your object variable has no specific data type, but becomes transformed as needed during runtime into the required datatype. Line by line. Rather than what languages like C, C++ and FORTRAN do - enforce strict datatypes from the get-go. This transformation requirement means the compiler cannot make many smart decisions about execution or about upcoming data conversions ahead of time, for one thing.

Faster compiler implementations do not employ that kind of data decision procrastination. Harder to program in the languages like these, but generally much faster execution times. E.g., C, FORTRAN, C++

13. May 19, 2014

Ok. This is what I did,as Mark suggested
Name_wat.Text=Convert.ToString(Sustem.Math.PI * Convert.TDouble(Input.Text) * Convert.ToDouble(Input.Text));

It works.
Now I had this problem probably because I don't understand some basic features of C# like namespace and class.
I will take a look at those.
What I think is Texts can only hold strings.So it has to be converted to a string,no matter what! And Arithmetic requires numbers so we need doubles.
But if I write a letter there,I get an error, because it can't be converted to a double? How do I fix this?
My approach would be something like this:
If (input is not a letter)
do what we did just now;
Else
Show an error message and do not let the user enter a letter again;?
I would like to disable the input of things other than numbers and if the user somehow writes a letter, display a warning message.
I'm not sure how to disable the letters input.

14. May 19, 2014

### Staff: Mentor

One option is to look for an exception mechanism to catch an error when you try to convert the number:

http://www.dotnetperls.com/exception

also you should break up your code into simpler chunks so you can block them with exception code as needed.

15. May 19, 2014

### AlephZero

You need to learn about exceptions eventually, but you don't need them for something this simple. There is a TryParse function which attempts to do the conversion and tells you if it failed.

http://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx

Also, don't get into the bad habit of duplicating bits of code like
... Convert.TDouble(Input.Text) * Convert.ToDouble(Input.Text));

Break up long statements using variables to store intermediate results, e.g

var r = Convert.ToDouble(Input.Text);
var a = System.Math.PI * r * r;
Name_wat.Text=Convert.ToString(a);

Note you can declare a variable at the same time as you give it a value, and also that if the compiler can figure out a sensible type for the variable, you can just give it type "var" rather than "double", "String", or whatever.

16. May 19, 2014

### harborsparrow

You have come across THE primary difference between Visual Basic and C#. C# is what computer scientists call "strongly typed"; that means, you always have to tell it--explicitly!--what type every variable is. Visual Basic OTOH will try to infer the type to be used from the context. While VB's type inference is convenient in 99% of the cases, there are a few cases where it will infer incorrectly, and you will suddenly start getting a very unintended result. It's a trade-off between convenience and safety. While it is more work for you to learn about variable types (which really suggests how the underlying data is stored in memory), it is better for you as a programmer to know this. So I think that learning C# will be worth more to you in the long run than sticking with Visual Basic.

I also second AlephZero's advice on style in the preceding post.

17. May 19, 2014

### TurtleMeister

To make VB a strongly typed language, just put the following three statements at the very beginning of your program (in Visual Studio):

Option Explicit On
Option Strict On
Option Infer Off

This will force you to declare and use your variables properly by warning you in the IDE or generating an error at build time. You're supposed to be able to make this a default setting from the Tools menu, but for some reason it doesn't work for me. I have to put it in the code.

18. May 20, 2014