Solved

500 points for these two simple questions:)

Posted on 2006-11-03
6
215 Views
Last Modified: 2010-04-16
Hi my friends,

I read the C# Bible partially....am I right concluding this programming language beats most of the other languages like C & C++ & Delphi?       

I just set my first steps into C# and I'm stumbling upon the first example I encounter which I fail to complete. All steps seem simple and I checked all I did thoroughly...nevertheless I need some help here >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i > 10; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (int i = 10; i < 10; i++)
            {
                this.textBox1.Text += i.ToString() + ",";
            }

        }
    }
}

According to this tutorial (and I quote) >

"Now build and execute by hitting F5 and when you click the button you should see the suite of 1 to 9 and then 10 to 1 in textBox2".

In practice however I'm left with this error message >

"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"

Now in my opinion the error message is legitimate, but after more than a hour of debating with the tutorial itself :) I turn to you folks.

Help me out so I can continue and please comment properly!

Kind regards,

Paul



0
Comment
Question by:PeterdeB
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17870980
Hi,

This error looks correct - now this occurs because with your for loop the counter i is initilised outside the for loop - then the loop is entered and then the variable still exists.

There are two approaches for this.

            for (int i = 0; i > 10; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (i = 10; i < 10; i++)
            {
                this.textBox1.Text += i.ToString() + ",";
            }

or you could do

            for (int i = 0; i > 10; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (int j = 10; j < 10; j++)
            {
                this.textBox1.Text += j.ToString() + ",";
            }

I like C# but it does have limitations (requiring the .NET framework for one) and although it has been shown to run on *nix this is still not perfect as opposed to c and c++
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17871035
I don't get an error with that code using C# 2005 Express.

You would get that error if you had this though:

        private void button1_Click(object sender, EventArgs e)
        {
            int i;  //  <---------- THIS WOULD CAUSE THE ERROR YOU POSTED
            for (int i = 0; i > 10; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (int i = 10; i < 10; i++)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
        }

Did you remove some code before posting?

Also, the description you quoted states:

    "Now build and execute by hitting F5 and when you click the button you should see the suite of 1 to 9 and then 10 to 1 in textBox2".

To get that you would need to change the logic in your for loops:

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (int i = 10; i > 0; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
        }
0
 

Author Comment

by:PeterdeB
ID: 17871057
Hi raynard7,

Tnx for your rapid response!

I modified my code to >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i > 10; i--)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
            for (i = 10; i < 10; i++)
            {
                this.textBox1.Text += i.ToString() + ",";
            }
           

        }
    }
}

Now it complains that "i" does not exist in the current context (4 times)

Still abacadabra to me....

Regards Paul
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:PeterdeB
ID: 17871066
Idle_Mind > I'm using express edition also and tnx for your rapid response!

Regards Paul
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17871072
Try deleting the "bin" subfolder in your project and running it again...
0
 

Author Comment

by:PeterdeB
ID: 17871081
It works now using your code Idle_Mind....tnx a lot. Right...now i see what you mean by the change of logic. Also the rebuild tip did some work here :)

Regards Paul
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now