?
Solved

500 points for these two simple questions:)

Posted on 2006-11-03
6
Medium Priority
?
226 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

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

Regards Paul
0
 
LVL 86

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

862 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