3 Replies - 274 Views - Last Post: 06 August 2011 - 10:12 AM Rate Topic: -----

#1 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Array outputs into nothing

Posted 06 August 2011 - 09:15 AM

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

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


        private void btnDisplay_Click(object sender, EventArgs e)
        {
            string Uno = txtFirst.Text;
            string Dos = txtSecond.Text;
            string Tres= txtThird.Text;
            string Quatro = txtFourth.Text;
            string Cinco = txtFifth.Text;


        string[] input = new string[5];
        input[0] = Uno;
        input[1] = Dos;
        input[2] = Tres;
        input[3] = Quatro;
        input[4] = Cinco;

            foreach (string i in input)
            {
                lblNormal.Text += i + '\n';
            }
            for (int j = input.Length - 1; j < 0; j--)
            {
                Array.Reverse(input);
                lblReverse.Text += input[j] + '\n';
            }
        }
    }
}


In case you were wondering, the foreach loop does what it's supposed to do. It's the other one, the one meant to show it all in reverse, that does nothing.

Is This A Good Question/Topic? 0
  • +

Replies To: Array outputs into nothing

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: Array outputs into nothing

Posted 06 August 2011 - 09:20 AM

for (int j = input.Length - 1; j < 0; j--)


The first part of the for loop initializes the counting variable. The second condition must be true for the loop to run. The third is, of course, the change in the counting variable with each loop. The initialization occurs first, followed by the check against the condition.

Now, given that information and the values in your code, will your loop ever run?

I really suggest you learn to debug your own code. In fact, I suggest you read the entire Learning C# Series
Was This Post Helpful? 3
  • +
  • -

#3 Goff256  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 93
  • Joined: 04-July 11

Re: Array outputs into nothing

Posted 06 August 2011 - 10:06 AM

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

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


        private void btnDisplay_Click(object sender, EventArgs e)
        {
            string Uno = txtFirst.Text;
            string Dos = txtSecond.Text;
            string Tres= txtThird.Text;
            string Quatro = txtFourth.Text;
            string Cinco = txtFifth.Text;


        string[] input = new string[5];
        input[0] = Uno;
        input[1] = Dos;
        input[2] = Tres;
        input[3] = Quatro;
        input[4] = Cinco;

            foreach (string i in input)
            {
                lblNormal.Text += i + '\n';
            }
            for (int j = input.Length - 1; j >= 0; j--)
            {
                lblReverse.Text += input[j] + '\n';
            }
        }
    }
}



Turned out to be the right thing
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: Array outputs into nothing

Posted 06 August 2011 - 10:12 AM

Expanding on what Jack said:

Line 40 reads like this:
for (int j = input.Length - 1; j < 0; j--)

j starts with a value equal to input.Length -1 ;
WHILE j is less than 0 ;
decrement j

It does NOT read like this
j starts with a value equal to input.Length -1 ;
UNTIL j is less than 0 ;
decrement j

Do you see the difference and how it will affect the loop?

Also: Line 42. This reverses your array. But you have it inside your loop. Meaning it is going to execute for every element in your array. If you have an array of 5 items, then you are going to keep reversing the array 5 times: once for each execution of the loop.

Jack is completely right about learning the language and the syntax before you start trying to build your own projects from scratch. Or worse yet, trying to learn the language by just creating your own projects and NOT even following a book or tutorial. Please see the recommendations after the FAQs.



See FAQ #5 for debugging tutorials
See FAQ #16 for how to ask a question so we can better help you

Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1