vb.net and c# issue in for loop

  • Thread starter Benjamin Fallar III
  • Start date
B

Benjamin Fallar III

hi,

i have some weird result using for loop...

here it is in vb.net.........

Module Module1

Sub Main()

For i As Integer = 1 To 3
Dim total As Integer
total += i
Console.WriteLine(total)
Next

End Sub

End Module

here it is in c#

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
int total = 0;
total += i;
Console.WriteLine(total);
}
}
}

the results are different...

i think because in c#, we cannot use the variable total if uninitialized...
how to go about this?
 
P

PvdG42

Benjamin Fallar III said:
hi,

i have some weird result using for loop...

here it is in vb.net.........

Module Module1

Sub Main()

For i As Integer = 1 To 3
Dim total As Integer
total += i
Console.WriteLine(total)
Next

End Sub

End Module

here it is in c#

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
int total = 0;
total += i;
Console.WriteLine(total);
}
}
}

the results are different...

i think because in c#, we cannot use the variable total if
uninitialized... how to go about this?

In both cases, your accumulator should be declared and initialized once
*before the loop begins*. In the C# example it is reinitialized to zero on
each iteration.
 
J

Jack Jackson

hi,

i have some weird result using for loop...

here it is in vb.net.........

Module Module1

Sub Main()

For i As Integer = 1 To 3
Dim total As Integer
total += i
Console.WriteLine(total)
Next

End Sub

End Module

here it is in c#

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
int total = 0;
total += i;
Console.WriteLine(total);
}
}
}

the results are different...

i think because in c#, we cannot use the variable total if uninitialized...
how to go about this?

In VB the DIM statement is scoped to the entire method. It is the
same as if you said:

Sub Main()
Dim total As Integer

For i As Integer = 1 To 3
total += i
Console.WriteLine(total)
Next

End Sub

In VB it is not possible to have block-scoped variables.

In the C# example, 'total' exists only inside the for loop. The
variable 'total' is reinitialized each time through the loop. If you
want the C# code to work like the VB code, move the definition of
'total' outside the for loop.
 
B

Benjamin Fallar III

thanks jack! that confirms my code :)

Jack Jackson said:
hi,

i have some weird result using for loop...

here it is in vb.net.........

Module Module1

Sub Main()

For i As Integer = 1 To 3
Dim total As Integer
total += i
Console.WriteLine(total)
Next

End Sub

End Module

here it is in c#

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
int total = 0;
total += i;
Console.WriteLine(total);
}
}
}

the results are different...

i think because in c#, we cannot use the variable total if
uninitialized...
how to go about this?

In VB the DIM statement is scoped to the entire method. It is the
same as if you said:

Sub Main()
Dim total As Integer

For i As Integer = 1 To 3
total += i
Console.WriteLine(total)
Next

End Sub

In VB it is not possible to have block-scoped variables.

In the C# example, 'total' exists only inside the for loop. The
variable 'total' is reinitialized each time through the loop. If you
want the C# code to work like the VB code, move the definition of
'total' outside the for loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top