Wrox Programmer Forums
|
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
 
Old May 2nd, 2011, 08:26 AM
Authorized User
 
Join Date: May 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Scope of variable i

Hello,

I'm on Chapter 5, page 111, doing the Try IT OUT Using an Array here's my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ch05Ex04
{
classProgram
{
staticvoid Main(string[] args)
{
string[] friendNames = { "Robert Barwell", "Mike Parry", "Jeremy Beacock" };
int i;
Console.WriteLine("Here are {0} of my friends:", friendNames.Length);
for (int i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
Console.ReadKey();
}
}
}
The error I get is this:
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

How do I fix this, or is it a book mistake?

Thanks in advance

Paul
 
Old May 2nd, 2011, 08:49 AM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 171
Thanks: 0
Thanked 14 Times in 14 Posts
Default

you've declare the variable i twice, once in your "Main" method and once in your For loop. Either get rid of the line
Code:
int i;
(which i would recommend) or change your For loop to
Code:
for (i = 0; i < friendNames.Length; i++)
and that should solve the problem :)
 
Old May 4th, 2011, 02:45 PM
Registered User
 
Join Date: Apr 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

int i;

for (int i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
you have declared i two times the correct form will be:
for (int i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}
OR
int i;

for (i = 0; i < friendNames.Length; i++)
{
Console.WriteLine(friendNames[i]);
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
The variable or parameter was duplicated within the same scope error collegian XSLT 2 January 6th, 2011 07:51 PM
Variable scope in XSLT? trufla XSLT 3 October 24th, 2008 01:45 PM
the scope of variable ccj_999 C++ Programming 9 October 26th, 2006 10:35 AM
Variable with application scope madhukp PHP How-To 0 January 8th, 2005 12:41 AM
Error: Intrinsic variable with Application scope danielh Classic ASP Basics 1 June 22nd, 2003 03:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.