[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

500 points for these two simple questions:)

Posted on 2006-11-03
6
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 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