How do you add numbers to an int? - c#

I'm trying to make a basic calculator in c#.
The only problem is, I don't know how I could add numbers to an int; for instance, if I wanted button1 to do something like this in a textbox, it'd be
textBox1.text += "1"
but this is for the operations, and the textbox displays the operator, so I couldn't convert it to an int. I'd really appreciate some help.

You can do it with something like (where s is a string):
s = (Int32.Parse(s) + 1).ToString();
Just make sure that s is actually a valid number, otherwise you'll have to cobble together something with TryParse and figure out what to do when it's not a number, like leave it alone:
int val;
if (Int32.TryParse(s, out val)) {
val++;
s = val.ToString();
}
You can also restrict user input so that they can only enter integers, have a look at MaskedTextBox and set the Mask property. See the documentation here.

C# is a strongly typed language. A textbox contains a string, which you must convert to an int before performing arithmetical operations.
Converting a string to an int can be done with int.Parse(), then you must convert back to a string to change the textbox contents:
int temp = int.Parse(textBox1.Text) + 1;
textBox1.Text = temp.ToString();
This will throw an exception if textBox.Text cannot be converted to an int. To deal with this, look up the int.TryParse() function.

Related

How to convert String to One Int

following problem in C# (working in VS Community 2015):
First off, i fairly new to C#, so excuse me if that question would be an easy fix.
I have a contact sensor giving me a string of numbers (length measurement). I read them with the SystemPort Methods and cut them down to the numbers that i need with substring (as the beginning of the string, the "SR00002" is useless to me).
In the end i end up with a string like : "000.3422" or "012.2345". Now i want to convert that string to one solid int-variable that i can work with, meaning subtract values from and such.
Bsp: I want to calculate 012.234 - 000.3422 (or , instead of . but i could change that beforehand)
I already tried Parse and ConvertToInt (while iterating through the string) but the endresult is always a string.
string b = serialPort2.ReadLine();
string[] b1 = Regex.Split(b, "SR,00,002,");
string b2 = b1[1].Substring(1);
foreach (char c in b2)
{
Convert.ToInt32(c);
}
textBox2.Text = b2 + b2.GetType();
I know that when b2 will be int it can not be printed in the Textbox but ill take care of that later.
When everything is converted accordingly, ill outsource the conversion to its own method =)
The GetType is just for testing and as said shows only System.String (which i dont want). Help would be much appreaciated. I also browsed the searchfunction and google but couldnt find anything of help. I wish any possible helpers a nice day, mfg Chris.
use the int.Parse
int.Parse("123")
You need to assign the converted values to a new variable or array that takes int or other numeric values that you want.
int[] numbers = new int[b1.length];
for(int i = 0; i < b2.length; i++)
{
numbers[i] = Convert.ToInt32(b2[i]);
}

Why use Parse to turn a string into an int?

I have made a little calculator:
private void button1_Click(object sender, EventArgs e)
{
int input1 = int.Parse(textBox1.Text);
int input2 = int.Parse(textBox2.Text);
int Result = input1 + input2;
textBox3.Text = Result.ToString();
}
Why can't I just do textBox3.Text = Result; when I already told them that the result is int type in the line before?
When I need to explain data types, I often use the analogy with shapes.
A data type is a shape, that forms the behaviour of the variable of the specified type. When a variable of a given type is created it holds the description of the type and a value. Variables with the same shape can be connected together with the help of operators (for example +).
Two variables from a different type (shape) can not be connected directly together - they need a converter / wrapper.
A converter makes for example from the shape triangle a circle or a wrapper masks the triangle as a circle.
Back to your example. TextBox controls can hold only the data type string, meaning they are incompatible with the shape of an int. You already have converted the content of the string to an int using int input1 = int.Parse(textBox1.Text);.
That is why you can not simply assign the Result variable to the Text property - their shapes are different, so you need to convert the int back to string using the ToString() method. You could also write textBox3.Text = (input1 + input2).ToString();.
The shape analogy can be used to understand parameters passing - as long you know the signature of a method int add(int a, int b), you know exactly what you need to put when you are calling a method (two int's) and what kind of type to expect as a result (an int). For example String result = add(1,2); will not work because the signature is not as expected, result should be of type int in order to work (int result = add(1,2);) or the call to add(1,2) should be converted to string (string result = add(1,2).ToString();).
Same goes with classes and objects - a class is a custom data type, so a custom shape. As long two instances of a class (objects) have the same shape they can be connected or assigned to each other.
This is a very simplified explanation (but still it helped my trainee in the past) so take your time and have a look at the C# MSDN documentation for a thorough explanation of data types.
Why can't I just do textBox3.Text = Result; when I already told them
that the result is int type in the line before?
Even though you 'told' that the result would be of type int, the compiler needs to know how to interpret/transform the string into an int.
int.Parse is one way of doing just that.
Sounds like you may want to start here with C# Data Types. As for your question. TextBox.Text is of type string. Check out MSDN articles for the different controls that you're using and their properties, such as this MSDN Article on TextBox.Text
textBox3.Text is a type of string while Result is a type of int and in your code you are assigning int value to a string which is wrong. You need to transform ones type to the other.

convert variable in asp.net

I wrote a piece of simple code that I dont to find what the problem.
the code is:
var sortSecurities="SELECT * FROM securities";
int total=0;
var value="";
foreach(var row in db.Query(sortSecurities))
{
value=row.lastGate;
total=Convert.ToInt32(value)*100;// here the problem with compilation..
db.Execute("INSERT INTO holding(IDgrossPortfolio,IDSecurity,totalHolding,units,buyGate) "+"VALUES (#0,#1,#2,#3,#4)",row.category,row.number,total,"100",row.lastGate);
}
what the problem with the convert?
the error is:
Exception Details: System.FormatException: Input string was not in a correct format.
value does not hold a value that can be converted to Int32. If you could do some debugging and see what the value of it is from row.lastGate, you might see what the problem is.
Also, not sure what is returned by db.Query(sortSecurities) (or really what kind of object row.lastGate is), but you can also try to change value=row.lastGate; to value=row.lastGate.ToString();
you can use try parse to check if the value actually contains a number
int total;
bool result = Int32.TryParse(value, out total);
if (result)
{
db.Execute("INSERT INTO holding(IDgrossPortfolio,IDSecurity,totalHolding,units,buyGate) "+"VALUES (#0,#1,#2,#3,#4)",row.category,row.number,total,"100",row.lastGate);
}
Your value isn't successfully being parsed by Convert.ToInt32()
Alternatively, consider using Int32.TryParse() and validate if the data is indeed the type of data you're expecting.
int result;
if(Int32.TryParse(row.lastGate, out result))
{
//row.lastGate was a valid int
total = result * 100;
}
else
{
//row.lastGate wasn't a valid int
}
Thanks you for all... I try now and found elegant answer.
Like I wrote in the comments, becouse I know that the value of row.lastGate
represent a number I don't need to check it.
So I try this and it works:
var sortSecurities="SELECT * FROM securities";
int total=0;
double value=0;
foreach(var row in db.Query(sortSecurities))
{
value=Convert.ToDouble(row.lastGate);
total=Convert.ToInt32(value)*100;//100 is default
db.Execute("INSERT INTO holding(IDgrossPortfolio,IDSecurity,totalHolding,units,buyGate) "+"VALUES (#0,#1,#2,#3,#4)",row.category,row.number,total,"100",row.lastGate);
}
Probably I needed to change the value first of all to double and then to int
Becouse when I try to change it directly to int the Compiler did'nt interpret the
string right, becouse of the dot in the number (type double).
thanks about the the intention..

How to create a generic Add Method for two values

For example if both values are int type it adds them.... ie 2+2=4
if both values are float....ie 2.2+2.3=4.5
or if one value is string and second is int...ie 1 + Pak=1Pak
We will get these two values from user using tfwo textboxes
This would be one way of doing it. Without having to convert to string and than back to numeric.
public object Add(IConvertible a, IConvertible b)
{
if(IsNumeric(a) && IsNumeric(b))
return a.ToDouble(CultureInfo.InvariantCulture) + b.ToDouble(CultureInfo.InvariantCulture);
return a.ToString() + b.ToString();
}
public static bool IsNumeric(object o)
{
var code = (int)Type.GetTypeCode(o.GetType());
return code >= 4 && code <= 15;
}
You can't do it using generics. You'll receive string from your textbox anyway. The only thing to do is to implement it "manually" exactly this way as you said:
public string TrySumTwoStrings(string input1, string input2)
{
double numeric1, numeric2;
if (Double.TryParse(input1, out numeric1) && Double.TryParse(input2, out numeric2))
return (numeric1 + numeric2).ToString();
return input1 + input2;
}
There's no way to use generics if we have no different types (everything is typed as string here).
You wouldn't, generics cannot be constrained in a way to support arithmetic operators (or concatenation). You would need to create overloads.
public int Add(int x, int y)
public double Add(double x, double y)
public decimal Add(decimal x, decimal y)
// etc.
Of course, you still have the problem of determining how exactly to parse your data. The source being a TextBox, the data will inherently be strings. You will have to determine which type of number it should be, if any.
If doing this for a real application, you shouldn't have this problem. Your textbox should be expected to receive input from the user in the form of an integer, or a decimal, or a string, etc. If it's not convertible to the proper type, it's an invalid input from your user. You wouldn't want the input to have to be magically deduced.
string Str1 = textBox1.Text.Trim();
string Str2 = textBox2.Text.Trim();
double Num1,num2;
bool isNum1 = double.TryParse(Str1, out Num1);
bool isNum2 = double.TryParse(Str2, out Num2);
if (isNum1 && isNum2)
MessageBox.Show((isNum1 + isNum2).ToString());
else
MessageBox.Show( Str1 + Str2);
Check out http://www.yoda.arachsys.com/csharp/miscutil/ The MiscUtil library. It contains some very clever Expression Tree stuff to allow operators with generics. It's not going to work exactly how you want (as others have stated, you can't constrain types to have operators) but it should do exactly what you want.
I don't know how it handles adding different types together though, I've not tried that.
I would think it would take some processing the values before hand in order with things like String.PArse, Int.Parse, etc...
Take them in order of compplexity first because 1 will convert to string, however x will not convert to integer or float.
Officially changed my answer to same as comment...
Best suggestion I have on that would be allow the user to select what type to interpret the data as and pass to the appropriate function based on what the user meant, there would be too many ways to interprets char strings to know what the users intention was, code processes logic not intent.

C# Input string was not in a correct format

Working on parsing from a text box to int to get into the incrementHour method as shown.
if (txtHourPlus.Text != String.Empty)
{
time1.incrementHour(int.Parse(txtHour.Text));
}
And in the time class: (the time1 objects class)
public int incrementHour(int step)
{
if (step > 0 && hour < 24)
{
//step = step % hour;
hour = (hour + step) % 24;
time.AddHours(hour);
return hour;
}//end of if
else
{
MessageBox.Show("Please enter a positive number.");
return 0;
}//end of else
}//end of incrementHour
not sure why i'm getting this error. I'm converting it to the corrent data type. Because it accepts an int variable.
Alright well i got it to take the value (small mistake >.> Don't even wanna say it)
However as someone already said the method probably needs work because i'm trying to change the Datetime value that i get in the first place and add an hour or subtract and etc etc.
That will happen if someone has typed "foo" into the text box, for example. You should use int.TryParse instead, which lets you detect the error without an exception being thrown.
I notice that you're not actually using the return value of your method though - just like you're not using the return value of time.AddHours. You are aware that DateTime.AddHours doesn't actually modify the value you're calling it on, aren't you? (I suspect you'll need to tweak that method quite a bit, actually... there are various potential problems with it, depending on exact what you're trying to do.)
(Finally, I'd change the method name to IncrementHour to comply with .NET conventions.)
you are testing txtHourPlus for emptiness, but then parsing and passing txtHour. typo (?)
If your input isn't parsable as an integer, attempting to parse it will raise an exception. Validate or use Int32.TryParse()
Change this part of your code:
if (txtHour.Text != String.Empty)
{
int parsedValue;
if (int.TryParse(txtHour.Text, out parsedValue))
{
time1.incrementHour(parsedValue);
}
else
{
// non-numeric value was entered into the textbox - handle accordingly.
}
}

Categories