 |
BOOK: Beginning Visual C# 2010
 | This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 3rd, 2012, 03:55 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Chapter 6 Page 140
Intellisense tells me that the int i; outside the loop should not be there:
Quote:
|
Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else C:\BegVCSharp\Chapter6\CH06Ex01\page140\Program.cs 14 22 page140
|
Is the int i; a typo?
|
|

January 4th, 2012, 10:05 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
Can you post your code (I don't have the book). And is this your own code, or code directly taken from the book?
Imar
|
|

January 4th, 2012, 10:29 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
Hi there,
Can you post your code (I don't have the book). And is this your own code, or code directly taken from the book?
Imar
|
Straight out of the book.
int i;
for (i=0, i<10....
Last edited by Frodo; January 4th, 2012 at 10:33 AM..
|
|

January 4th, 2012, 12:07 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
As I said, i don't have the book so I can't check how it's supposed to look.
What you just posted should compile fine, so maybe somewhere else you already defined another i variable? Can you post the full code for the class that contains this loop?
Cheers,
Imar
|
|

January 4th, 2012, 08:05 PM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Code:
int i;
string text = "";
for(i=0;i<10,i++)
{
text="Line "+Convert.ToString(i);
Console.WriteLine("{0}",text);
}
Console.WriteLine("Last text output in loop:{0}",text);
This is on page 141 of the book. Isn't i being defined twice?
|
|

January 4th, 2012, 08:21 PM
|
|
Wrox Author
|
|
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
|
|
Quote:
Originally Posted by Frodo
Code:
int i;
string text = "";
for(i=0;i<10,i++)
{
text="Line "+Convert.ToString(i);
Console.WriteLine("{0}",text);
}
Console.WriteLine("Last text output in loop:{0}",text);
This is on page 141 of the book. Isn't i being defined twice?
|
The code you've posted compiles fine with me if you change the "," in the for statment to ";" as it is in the book. The variable i is declared only once. Probably your code that doesn't compile uses something like this:
Code:
int i;
for (int i = 0; i < 10; i++)
which would create an error you described in the first post as here the variable i is declared a second time within the for statement.
|
|
The Following User Says Thank You to ChristianNagel For This Useful Post:
|
Frodo (January 9th, 2012)
|
|

January 9th, 2012, 10:46 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Sorry it's taken me this long to get back to you. Thank you for your responses. I checked my code: I did have a semicolon where you identified the comma in the code that I thought I copied here. I apologize for the typo. This is what I now copy from my file and I get the error I cited below:
Quote:
namespace page140
{
class Program
{
static void Main(string[] args)
{
int i;
string text="";
for (int i = 0; i < 10; i++)
{
text = "Line " + Convert.ToString(i);
Console.WriteLine("{0}",text);
}
Console.WriteLine("Last text output in loop: {0}", text);
}
}
|
}
|
|

January 9th, 2012, 10:58 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
As suggested earlier, you define i in the loop as well using int.
This:
for (int i = 0; i < 10; i++)
should be
for (i = 0; i < 10; i++)
Alternatively, don't declare i outside the loop.
Hope this helps,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
Frodo (January 9th, 2012)
|
|

January 9th, 2012, 11:03 AM
|
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 23
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
As suggested earlier, you define i in the loop as well using int.
This:
for (int i = 0; i < 10; i++)
should be
for (i = 0; i < 10; i++)
Alternatively, don't declare i outside the loop.
Hope this helps,
Imar
|
That's it. Thanks.
|
|
 |
|