Why does for loop behave differently when migrating?
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I'm in the process of migrating a project from VB to C# and I've had to change how a for loop being used is declared.
In VB.NET the for loop is declared below:
Dim stringValue As String = "42"
For i As Integer = 1 To 10 - stringValue.Length
stringValue = stringValue & " " + CStr(i)
Console.WriteLine(stringValue)
Next
Which outputs:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
In C# the for loop is declared below:
string stringValue = "42";
for (int i = 1; i <= 10 - stringValue.Length; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
This obviously isn't correct so I had to change the code ever so slightly and included an integer variable that would hold the length of the string.
Please see the code below:
string stringValue = "42";
int stringValueLength = stringValue.Length;
for (int i = 1; i <= 10 - stringValueLength; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length
condition in the for loop even though each time the loop occurs the length of the string changes. Whereas in C# if I use the stringValue.Length
in the for loop condition it changes the initial string value each time the loop occurs, why is this?
c# vb.net
add a comment |Â
up vote
9
down vote
favorite
I'm in the process of migrating a project from VB to C# and I've had to change how a for loop being used is declared.
In VB.NET the for loop is declared below:
Dim stringValue As String = "42"
For i As Integer = 1 To 10 - stringValue.Length
stringValue = stringValue & " " + CStr(i)
Console.WriteLine(stringValue)
Next
Which outputs:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
In C# the for loop is declared below:
string stringValue = "42";
for (int i = 1; i <= 10 - stringValue.Length; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
This obviously isn't correct so I had to change the code ever so slightly and included an integer variable that would hold the length of the string.
Please see the code below:
string stringValue = "42";
int stringValueLength = stringValue.Length;
for (int i = 1; i <= 10 - stringValueLength; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length
condition in the for loop even though each time the loop occurs the length of the string changes. Whereas in C# if I use the stringValue.Length
in the for loop condition it changes the initial string value each time the loop occurs, why is this?
c# vb.net
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I'm in the process of migrating a project from VB to C# and I've had to change how a for loop being used is declared.
In VB.NET the for loop is declared below:
Dim stringValue As String = "42"
For i As Integer = 1 To 10 - stringValue.Length
stringValue = stringValue & " " + CStr(i)
Console.WriteLine(stringValue)
Next
Which outputs:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
In C# the for loop is declared below:
string stringValue = "42";
for (int i = 1; i <= 10 - stringValue.Length; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
This obviously isn't correct so I had to change the code ever so slightly and included an integer variable that would hold the length of the string.
Please see the code below:
string stringValue = "42";
int stringValueLength = stringValue.Length;
for (int i = 1; i <= 10 - stringValueLength; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length
condition in the for loop even though each time the loop occurs the length of the string changes. Whereas in C# if I use the stringValue.Length
in the for loop condition it changes the initial string value each time the loop occurs, why is this?
c# vb.net
I'm in the process of migrating a project from VB to C# and I've had to change how a for loop being used is declared.
In VB.NET the for loop is declared below:
Dim stringValue As String = "42"
For i As Integer = 1 To 10 - stringValue.Length
stringValue = stringValue & " " + CStr(i)
Console.WriteLine(stringValue)
Next
Which outputs:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
In C# the for loop is declared below:
string stringValue = "42";
for (int i = 1; i <= 10 - stringValue.Length; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
This obviously isn't correct so I had to change the code ever so slightly and included an integer variable that would hold the length of the string.
Please see the code below:
string stringValue = "42";
int stringValueLength = stringValue.Length;
for (int i = 1; i <= 10 - stringValueLength; i ++)
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
And the output:
42 1
42 1 2
42 1 2 3
42 1 2 3 4
42 1 2 3 4 5
42 1 2 3 4 5 6
42 1 2 3 4 5 6 7
42 1 2 3 4 5 6 7 8
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length
condition in the for loop even though each time the loop occurs the length of the string changes. Whereas in C# if I use the stringValue.Length
in the for loop condition it changes the initial string value each time the loop occurs, why is this?
c# vb.net
c# vb.net
edited 16 mins ago


Çöđěxěŕ
5,11751640
5,11751640
asked 1 hour ago


slee423
2951217
2951217
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago
add a comment |Â
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
10
down vote
accepted
In C#, the loop boundary condition is evaluated on each iteration. In VB.NET, it is only evaluated on entry to the loop.
So, in the C# version in the question, because the length of stringValue
is being changed in the loop, the final loop variable value will be changed.
In VB.NET, the final condition is inclusive, so you would use <=
instead of <
in C#.
Thanks for the reply. I now realise that using<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and inVB
I didn't have to. I'm going to update my question to show the same output.
– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length ofstringValue
is being changed in the loop, the final loop variable value will be changed.
– Andrew Morton
1 hour ago
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
add a comment |Â
up vote
5
down vote
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length condition in the for loop even though each time the loop occurs the length of the string changes.
According to the VB.NET documentation:
If you change the value of
counter
while inside a loop, your code might be more difficult to read and debug. Changing the value ofstart
,end
, orstep
doesn't affect the iteration values that were determined when the loop was first entered.
So, the value of To 10 - stringValue.Length
is evaluated once and reused until the loops exit.
However, look at c#'s for statement
If the
for_condition
is not present or if the evaluation yieldstrue
, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of thefor_iterator
, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of thefor_condition
in the step above.
Which basically means that the condition ; i <= 10 - stringValueLength;
is evaluated again each time.
So, as you saw, if you want to replicate the code, you need to declare the final counter in c# before starting the loop.
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
add a comment |Â
up vote
0
down vote
This is a more direct translation from VB
to C#
string stringValue = "42";
foreach (int i in Enumerable.Range(1, 10 - stringValue.Length))
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
1
This isn't a direct translation.foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent
– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
add a comment |Â
up vote
0
down vote
In order to make the example more understandable, I will convert both for loops into C#
while loops.
VB.NET
string stringValue = "42";
int min = 1;
int max = 10 - stringValue.Length;
int i = min;
while (i <= max)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
C#
string stringValue = "42";
int i = 0;
bool flag = (i < 10 - stringValue.Length);
while (flag)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
i++;
flag = (i < 10 - stringValue.Length);
The differences are then:
The
min
value is different.
Theflag
is updated at the end of each loop inC#
.
Themax
value is inclusive inVB.NET
.
add a comment |Â
up vote
-3
down vote
In your vb.net code you started your loop as 1 where as in c# you stared at 0.
2
this is not the question
– Matt
1 hour ago
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked whystringValue.Length
changes in C# and not in VB.
– Guy
1 hour ago
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior ofstringValue.Length
is different. It's a difference, but not what the OP asked about.
– Guy
1 hour ago
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
 |Â
show 6 more comments
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
In C#, the loop boundary condition is evaluated on each iteration. In VB.NET, it is only evaluated on entry to the loop.
So, in the C# version in the question, because the length of stringValue
is being changed in the loop, the final loop variable value will be changed.
In VB.NET, the final condition is inclusive, so you would use <=
instead of <
in C#.
Thanks for the reply. I now realise that using<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and inVB
I didn't have to. I'm going to update my question to show the same output.
– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length ofstringValue
is being changed in the loop, the final loop variable value will be changed.
– Andrew Morton
1 hour ago
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
add a comment |Â
up vote
10
down vote
accepted
In C#, the loop boundary condition is evaluated on each iteration. In VB.NET, it is only evaluated on entry to the loop.
So, in the C# version in the question, because the length of stringValue
is being changed in the loop, the final loop variable value will be changed.
In VB.NET, the final condition is inclusive, so you would use <=
instead of <
in C#.
Thanks for the reply. I now realise that using<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and inVB
I didn't have to. I'm going to update my question to show the same output.
– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length ofstringValue
is being changed in the loop, the final loop variable value will be changed.
– Andrew Morton
1 hour ago
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
add a comment |Â
up vote
10
down vote
accepted
up vote
10
down vote
accepted
In C#, the loop boundary condition is evaluated on each iteration. In VB.NET, it is only evaluated on entry to the loop.
So, in the C# version in the question, because the length of stringValue
is being changed in the loop, the final loop variable value will be changed.
In VB.NET, the final condition is inclusive, so you would use <=
instead of <
in C#.
In C#, the loop boundary condition is evaluated on each iteration. In VB.NET, it is only evaluated on entry to the loop.
So, in the C# version in the question, because the length of stringValue
is being changed in the loop, the final loop variable value will be changed.
In VB.NET, the final condition is inclusive, so you would use <=
instead of <
in C#.
edited 58 mins ago
answered 1 hour ago


Andrew Morton
14k42747
14k42747
Thanks for the reply. I now realise that using<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and inVB
I didn't have to. I'm going to update my question to show the same output.
– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length ofstringValue
is being changed in the loop, the final loop variable value will be changed.
– Andrew Morton
1 hour ago
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
add a comment |Â
Thanks for the reply. I now realise that using<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and inVB
I didn't have to. I'm going to update my question to show the same output.
– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length ofstringValue
is being changed in the loop, the final loop variable value will be changed.
– Andrew Morton
1 hour ago
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
Thanks for the reply. I now realise that using
<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and in VB
I didn't have to. I'm going to update my question to show the same output.– slee423
1 hour ago
Thanks for the reply. I now realise that using
<=
allows me to iterate and have the same output as the VB code. However, I'm more interested in knowing as to why I've had to declare the integer variable and in VB
I didn't have to. I'm going to update my question to show the same output.– slee423
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length of
stringValue
is being changed in the loop, the final loop variable value will be changed.– Andrew Morton
1 hour ago
@slee423 The reason is given in the first sentence of my answer. Because the length of
stringValue
is being changed in the loop, the final loop variable value will be changed.– Andrew Morton
1 hour ago
1
1
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
apologies, thanks for that answer. And thanks for elaborating it in more detail for me.
– slee423
1 hour ago
1
1
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
@slee423 I added that into the answer as it does indeed clarify it.
– Andrew Morton
58 mins ago
add a comment |Â
up vote
5
down vote
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length condition in the for loop even though each time the loop occurs the length of the string changes.
According to the VB.NET documentation:
If you change the value of
counter
while inside a loop, your code might be more difficult to read and debug. Changing the value ofstart
,end
, orstep
doesn't affect the iteration values that were determined when the loop was first entered.
So, the value of To 10 - stringValue.Length
is evaluated once and reused until the loops exit.
However, look at c#'s for statement
If the
for_condition
is not present or if the evaluation yieldstrue
, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of thefor_iterator
, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of thefor_condition
in the step above.
Which basically means that the condition ; i <= 10 - stringValueLength;
is evaluated again each time.
So, as you saw, if you want to replicate the code, you need to declare the final counter in c# before starting the loop.
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
add a comment |Â
up vote
5
down vote
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length condition in the for loop even though each time the loop occurs the length of the string changes.
According to the VB.NET documentation:
If you change the value of
counter
while inside a loop, your code might be more difficult to read and debug. Changing the value ofstart
,end
, orstep
doesn't affect the iteration values that were determined when the loop was first entered.
So, the value of To 10 - stringValue.Length
is evaluated once and reused until the loops exit.
However, look at c#'s for statement
If the
for_condition
is not present or if the evaluation yieldstrue
, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of thefor_iterator
, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of thefor_condition
in the step above.
Which basically means that the condition ; i <= 10 - stringValueLength;
is evaluated again each time.
So, as you saw, if you want to replicate the code, you need to declare the final counter in c# before starting the loop.
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length condition in the for loop even though each time the loop occurs the length of the string changes.
According to the VB.NET documentation:
If you change the value of
counter
while inside a loop, your code might be more difficult to read and debug. Changing the value ofstart
,end
, orstep
doesn't affect the iteration values that were determined when the loop was first entered.
So, the value of To 10 - stringValue.Length
is evaluated once and reused until the loops exit.
However, look at c#'s for statement
If the
for_condition
is not present or if the evaluation yieldstrue
, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of thefor_iterator
, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of thefor_condition
in the step above.
Which basically means that the condition ; i <= 10 - stringValueLength;
is evaluated again each time.
So, as you saw, if you want to replicate the code, you need to declare the final counter in c# before starting the loop.
Now my question resolves around how VB differs to C# in terms of VB using the stringValue.Length condition in the for loop even though each time the loop occurs the length of the string changes.
According to the VB.NET documentation:
If you change the value of
counter
while inside a loop, your code might be more difficult to read and debug. Changing the value ofstart
,end
, orstep
doesn't affect the iteration values that were determined when the loop was first entered.
So, the value of To 10 - stringValue.Length
is evaluated once and reused until the loops exit.
However, look at c#'s for statement
If the
for_condition
is not present or if the evaluation yieldstrue
, control is transferred to the embedded statement. When and if control reaches the end point of the embedded statement (possibly from execution of a continue statement), the expressions of thefor_iterator
, if any, are evaluated in sequence, and then another iteration is performed, starting with evaluation of thefor_condition
in the step above.
Which basically means that the condition ; i <= 10 - stringValueLength;
is evaluated again each time.
So, as you saw, if you want to replicate the code, you need to declare the final counter in c# before starting the loop.
answered 1 hour ago


Camilo Terevinto
16.1k63159
16.1k63159
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
add a comment |Â
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
The end condition evaluation has the corollary that even if it doesn't vary but it is expensive to calculate, then it should be calculated just once before the loop.
– Andrew Morton
36 mins ago
add a comment |Â
up vote
0
down vote
This is a more direct translation from VB
to C#
string stringValue = "42";
foreach (int i in Enumerable.Range(1, 10 - stringValue.Length))
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
1
This isn't a direct translation.foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent
– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
add a comment |Â
up vote
0
down vote
This is a more direct translation from VB
to C#
string stringValue = "42";
foreach (int i in Enumerable.Range(1, 10 - stringValue.Length))
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
1
This isn't a direct translation.foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent
– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This is a more direct translation from VB
to C#
string stringValue = "42";
foreach (int i in Enumerable.Range(1, 10 - stringValue.Length))
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
This is a more direct translation from VB
to C#
string stringValue = "42";
foreach (int i in Enumerable.Range(1, 10 - stringValue.Length))
stringValue = stringValue + " " + i.ToString();
Console.WriteLine(stringValue);
answered 1 hour ago
Matt
398110
398110
1
This isn't a direct translation.foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent
– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
add a comment |Â
1
This isn't a direct translation.foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent
– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
1
1
This isn't a direct translation.
foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent– Panagiotis Kanavos
54 mins ago
This isn't a direct translation.
foreach
works with iterators. The VB.NET code works with a simpel loop. The OP's code is the actual equivalent– Panagiotis Kanavos
54 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
In fact, using SharpLab.io to get the code generated by the compiler you'd get what the OP wrote
– Panagiotis Kanavos
53 mins ago
add a comment |Â
up vote
0
down vote
In order to make the example more understandable, I will convert both for loops into C#
while loops.
VB.NET
string stringValue = "42";
int min = 1;
int max = 10 - stringValue.Length;
int i = min;
while (i <= max)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
C#
string stringValue = "42";
int i = 0;
bool flag = (i < 10 - stringValue.Length);
while (flag)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
i++;
flag = (i < 10 - stringValue.Length);
The differences are then:
The
min
value is different.
Theflag
is updated at the end of each loop inC#
.
Themax
value is inclusive inVB.NET
.
add a comment |Â
up vote
0
down vote
In order to make the example more understandable, I will convert both for loops into C#
while loops.
VB.NET
string stringValue = "42";
int min = 1;
int max = 10 - stringValue.Length;
int i = min;
while (i <= max)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
C#
string stringValue = "42";
int i = 0;
bool flag = (i < 10 - stringValue.Length);
while (flag)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
i++;
flag = (i < 10 - stringValue.Length);
The differences are then:
The
min
value is different.
Theflag
is updated at the end of each loop inC#
.
Themax
value is inclusive inVB.NET
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
In order to make the example more understandable, I will convert both for loops into C#
while loops.
VB.NET
string stringValue = "42";
int min = 1;
int max = 10 - stringValue.Length;
int i = min;
while (i <= max)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
C#
string stringValue = "42";
int i = 0;
bool flag = (i < 10 - stringValue.Length);
while (flag)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
i++;
flag = (i < 10 - stringValue.Length);
The differences are then:
The
min
value is different.
Theflag
is updated at the end of each loop inC#
.
Themax
value is inclusive inVB.NET
.
In order to make the example more understandable, I will convert both for loops into C#
while loops.
VB.NET
string stringValue = "42";
int min = 1;
int max = 10 - stringValue.Length;
int i = min;
while (i <= max)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
C#
string stringValue = "42";
int i = 0;
bool flag = (i < 10 - stringValue.Length);
while (flag)
stringValue = stringValue + " " + stringValue.Length.ToString();
Console.WriteLine(stringValue);
i++;
flag = (i < 10 - stringValue.Length);
The differences are then:
The
min
value is different.
Theflag
is updated at the end of each loop inC#
.
Themax
value is inclusive inVB.NET
.
answered 48 mins ago
Maxime Recuerda
14511
14511
add a comment |Â
add a comment |Â
up vote
-3
down vote
In your vb.net code you started your loop as 1 where as in c# you stared at 0.
2
this is not the question
– Matt
1 hour ago
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked whystringValue.Length
changes in C# and not in VB.
– Guy
1 hour ago
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior ofstringValue.Length
is different. It's a difference, but not what the OP asked about.
– Guy
1 hour ago
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
 |Â
show 6 more comments
up vote
-3
down vote
In your vb.net code you started your loop as 1 where as in c# you stared at 0.
2
this is not the question
– Matt
1 hour ago
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked whystringValue.Length
changes in C# and not in VB.
– Guy
1 hour ago
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior ofstringValue.Length
is different. It's a difference, but not what the OP asked about.
– Guy
1 hour ago
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
 |Â
show 6 more comments
up vote
-3
down vote
up vote
-3
down vote
In your vb.net code you started your loop as 1 where as in c# you stared at 0.
In your vb.net code you started your loop as 1 where as in c# you stared at 0.
answered 1 hour ago
minimalist
524
524
2
this is not the question
– Matt
1 hour ago
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked whystringValue.Length
changes in C# and not in VB.
– Guy
1 hour ago
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior ofstringValue.Length
is different. It's a difference, but not what the OP asked about.
– Guy
1 hour ago
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
 |Â
show 6 more comments
2
this is not the question
– Matt
1 hour ago
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked whystringValue.Length
changes in C# and not in VB.
– Guy
1 hour ago
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior ofstringValue.Length
is different. It's a difference, but not what the OP asked about.
– Guy
1 hour ago
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
2
2
this is not the question
– Matt
1 hour ago
this is not the question
– Matt
1 hour ago
1
1
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
Please explain exactly how would this be the root problem. Not sure how 2 people upvoted this
– Camilo Terevinto
1 hour ago
This is not what the OP asked, s/he asked why
stringValue.Length
changes in C# and not in VB.– Guy
1 hour ago
This is not what the OP asked, s/he asked why
stringValue.Length
changes in C# and not in VB.– Guy
1 hour ago
1
1
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior of
stringValue.Length
is different. It's a difference, but not what the OP asked about.– Guy
1 hour ago
@PanagiotisKanavos I don't see anywhere in the question any reference to why the loop produced values 0 - 7 instead 1 - 8, only why the behavior of
stringValue.Length
is different. It's a difference, but not what the OP asked about.– Guy
1 hour ago
2
2
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
@CamiloTerevinto I suspect you too have seen many instances where the only way to understand a question here is to read the title too. ;)
– Andrew Morton
1 hour ago
 |Â
show 6 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52607611%2fwhy-does-for-loop-behave-differently-when-migrating%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
It is interesting that many only conversion utilities convert this incorrectly.
– Magnus
1 hour ago
@Çöđěxěŕ The question is about migration of code from one specific language to another -- important information that should be in the title. Otherwise it's unclear what kind of migration is occurring (from the question list anyway).
– Heretic Monkey
21 mins ago
Microsoft clearly outlines what your problem is...
– Çöđěxěŕ
18 mins ago
Questions should not contain tags, thats what tags are for. Each question provides the tags that are associated... It is clear with the tags what languages are used.
– Çöđěxěŕ
16 mins ago