Solved

500 points for these two simple questions:)

Posted on 2006-11-03
6
217 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

10 Experts available now in Live!

Get 1:1 Help Now