Solved

500 points for these two simple questions:)

Posted on 2006-11-03
6
216 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 50
LINQ - C# to VB convertion 12 48
C# application error "Parameter Is Not Valid". 3 80
Turn on intranet settings 1 42
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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

27 Experts available now in Live!

Get 1:1 Help Now