Increase an int by one - c#

On another page I've said that nr = 0
var number = this.NavigationContext.QueryString["nr"];
int Nr = Convert.ToInt16(number);
And this works, Nr = 0
Now I want to upgrade Nr by one:
int next = Nr++;
Unfortunately this doesn't work... next = 0 too, but it supposed to be 1.
Could someone explain to me what I'm doing wrong?

Nr++ increments and returns the original value of Nr.
++Nr increments and returns the new value of Nr. So what you want is:
int next = ++Nr;

In C# (or C++), the statement
int next = Nr++;
translates to, "assign the value of Nr to variable next, and then increment Nr by 1.
If you want Nr to increment first, your statement should look like this:
int next = ++Nr;
Here is the definition of the ++ operator: http://msdn.microsoft.com/en-us/library/36x43w8w.aspx

the problem is ...Nr is incremented after Nr set to next use next=Nr+1;

There are two increment operators - pre-increment ++c and post-increment c++.
var x = ++c; is equivalent to
c = c + 1;
var x = c;
while var x = c++; is equivalent to
var x = c;
c = c + 1;
and therefore x either receives the value before or after c got incremented.

Related

Why my integer number assignment is invalid?

This is my test code,it's very simple:
class Program
{
static void Main(string[] args)
{
int number = 0;
int newNumber = number++;
Console.WriteLine("the old number is {0},the new number is:{1}", number, newNumber);
Console.Read();
}
}
whereas the output result is:'the old number is 1,the new number is:0',I think it's opposite with the result I want.
using the postfix increment ++ operator, it first returns the original value, then increments. To get what you want use the prefix increment operator like
int newNumber = ++number;
But if you don't want to change number, don't use an incrementing operator, use addition/subtraction instead.
That is because number++ updates the value of number by incrementing it (PostFix). This is done after using the original value in the expression it is used. To achieve the desired behaviour, You can use:
int number = 0;
int newNumber = number + 1;
Here, you have used number++ which is Post increment operator.
It assigns the value first and then increments its value.
You can achieve your desired output in two ways :
Use Pre increment operator
int newNumber = ++number;
Simply adding 1 to number variable and then assigning it to newNumber
int newNumber = number + 1;

Why is the output giving me a value of +1?

The code
public static void InitializeArray(ref double[] doubleA)
{
//array init using foreach
int i = 0;
foreach (double dub in doubleA)
{
doubleA[i++] = i + .5;
}
}
is giving me an output of
doubleA[0] = 1.5 instead of .5
doubleA[1] = 2.5 instead of 1.5
and so on
I'm not understanding why it's doing that. As I understand it, doubleA[0] should be getting 0.5 because I'm only adding 0.5 so where is the extra 1 coming from? I'm thinking it's from int i but for i=0, shouldn't it evaluate to doubleA[0] = 0 + .5?
So far I have tried writing doubleA[i++] = i + .5; instead which gives each element in the array just the value of 0.5.
So let's start with i = 0
doubleA[i++] = i + .5;
Here you are accessing position 0 of the array, but also adding 1 to the value of i. When i == 0, the expression i++ will be the value 0, but the value of the variable i is incremented such that the new value of i is 1.
On the right hand side, this new value of i is used (1) and added to 0.5. Giving a value of 1.5 which is assigned back to index 0 of the array (as indicated on the left hand side of the assignment).
I could go on, but I think you probably get it by now...
Further reading on operator ++
This is because i is being incremented in your key: doubleA[i++]. i++ will increment the key by one, AFTER returning it. When it's used later (as the value), its value has already been incremented. So now you're setting it to 1 + 0.5.
Let's break this down, and show you the functional equivalent, and you'll see exactly what this is happening:
public static void InitializeArray(ref double[] doubleA)
{
//array init using foreach
int i = 0;
foreach (double dub in doubleA)
{
int key = i;
i = i + 1;
doubleA[key] = i + .5;
}
}

MVC - Controller - Make a Simple Math Calculation - Variable + 1

I have a little problem to make a simple math calculation in the controller.
what I try to do is add +1 to a number of a variable.
Here is an example for you to understand better what I try to do:
var a= formcollection["Id_this"];
var next = a + 1;
Note: the value of "Id_this" is "1".
The result I need for the variable next is 2
My problem is that the result of the variable next is "12".
a is a string. Adding a number to a string results in the number being converted to a string and being concatenated.
To make it work, you first need to convert a to a number:
var next = Convert.ToInt32(a) + 1;
Reason is you are doing string concatenation. Try this safe approach:
int number;
int next = 0;
if(Int32.TryParse(formcollection["Id_this"], out number))
{
next = number + 1;
}
else
{
//formcollection["Id_this"] is not a number
}
like this :
var next = int.Parse(a) + 1;

2d array,adding it values in a weird pattern

i started learning C# and programming a few months ago and have some problems. The idea here is we create a 2 dimensional array (the number of rows / columns are added by the user), the numbers need to be between 1 and 10.
Then when the array is created the number sequence ( 3-5-7-9-11 etc) is started in the first and finishes in the last column. The rest of the numbers in the columns are added via keyboard by the user starting with the first row (ignoring column 1 and the last column cause we have that added).
The questions are :
What will be the best way to check if the numbers of rows/columns are between 1 and 10? (I was thinking of IF-else but isn't there a better way ?)
How will i make it so that the number sequence 3-5-7 etc is started in the first and finishes in the last column?
Yeah i feel lost.
Where i am at the moment :
Console.WriteLine("Add row value of 1-10");
string s1
s1 = Console.ReadLine();
int k = int.Parse(s1);
Console.WriteLine("Add column value of 1-10");
string s2;
s2 = Console.ReadLine();
int p = int.Parse(s2);
int[,] M = new int[k, p];
Example : we added k(row) & p(coulmn) value of 4.So the array should look like :
3 x x 11
5 x x 13
7 x x 15
9 x x 17
Then the X's should be added again manually without overwriting the existing numbers .The value of the numbers doesnt matter.
So... If I get it right you want to ask user the "length and width" of dynamical 2d array?
To check if entered number is between 1 and 10 there's only 1 method:
int [,] M;
if (k >= 1 && k <= 10 && p >= 1 && p <= 10)
{
M = new int[k,p];
}
And better is to do int.TryParse() for case if user enters characters there instead of numbers, or else you can easily get an Exception.
Filling with numbers:
int num = 3;
for (int i = 0; i < k; ++i)
{
M[i,0] = num;
num+=2;
}
for (int i = 0; i < k; ++i)
{
M[i,p] = num;
num+=2;
}
This adds numbers in 1st and last column in each row. After that to fill other cells manually you check every cell thet it is not in firs or last column.
I hope I understood you correctly. Provided code may be simplified, but provided in such way for better understanding.
if(k>0 && k<11 && p>0 && p<11)
{
int i;
int M[,] = new int[k,p];
for (i=0;i<k;i++)
{
M[i,0]=i*2+3;
M[i,p-1]=(i+k)*2+3;
}
}

Order of operations when working with array index

Consider this loop:
int[] myArray = new int[10];
int myIndex = 0;
for (int i = 0; i < 10; i++)
{
myArray[myIndex++] = myIndex;
Console.WriteLine(myArray[i]);
}
This yields:
1
2
3
...
Because myIndex is post-incremented, and the right side is evaluated first, shouldn't the array index 0 contain 0?
Can someone explain this order-of-operations misunderstanding for me?
The right side isn't necessarily evaluated first. Similar to:
foo.Bar.Baz = a + b;
In the above code, foo.Bar is evaluated first, then a + b, then the set_Baz method is called to set the Baz property to whatever is evaluated on the right.
So in your code, if you break it into pieces, it looks like this:
var index = i;
// post-incremented in the original code means this comes after the line above,
// but not after the line below it.
i += 1;
myArray[index] = i;
first run:
myArray[myIndex++] = myIndex;
* *
| |
zero one
myIndex++ gets executed after myArray[myIndex++], but any subsequent calls with have the already incremented variable.
The myIndex++ is executing before the value is being set because the array index takes precident so it knows what array index to set the value to.
The...
myArray[myIndex++] = myIndex;
...is equivalend to...
int tmp = myIndex;
++myIndex;
myArray[tmp] = myIndex;
The myIndex++ is evaluated first, followed by the left side and finally the assign operator, according to precedence

Categories

Resources