0

What my program looks like, take a look before reading please.

I'm making a project in Winforms that's supposed to take values from the t Array, add them with the value inside a C variable that I have yet to declare and show the results in the tf textbox array when I press the FIFO button. My problem is that I can't seem to do this properly. I've been trying regular additions and whatnot to make sure that the contents of ti or t show on tf, and yet nothing seems to work at all. My main issue is that the program only takes the first value of the array, instead of taking them all. I'll be posting my code below.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(TextBox))
            {
                ((TextBox)(c)).Text = "0";
            }
        }
    }

    private void fifo_Click(object sender, EventArgs e)
    {
        int c = 0;

        int[] ti = { 0, 1, 2, 3, 4, 5 };
        ti[0] = Convert.ToInt32(tiA.Text);
        ti[1] = Convert.ToInt32(tiB.Text);
        ti[2] = Convert.ToInt32(tiC.Text);
        ti[3] = Convert.ToInt32(tiD.Text);
        ti[4] = Convert.ToInt32(tiE.Text);
        ti[5] = Convert.ToInt32(tiF.Text);

        int[] t = { 0, 1, 2, 3, 4, 5 };
        t[0] = Convert.ToInt32(ta.Text);
        t[1] = Convert.ToInt32(tb.Text);
        t[2] = Convert.ToInt32(tc.Text);
        t[3] = Convert.ToInt32(td.Text);
        t[4] = Convert.ToInt32(te.Text);
        t[5] = Convert.ToInt32(tf.Text);

        int[] tf1 = { 0, 1, 2, 3, 4, 5 };
        tf1[0] = Convert.ToInt32(tfA.Text);
        tf1[1] = Convert.ToInt32(tfB.Text);
        tf1[2] = Convert.ToInt32(tfC.Text);
        tf1[3] = Convert.ToInt32(tfD.Text);
        tf1[4] = Convert.ToInt32(tfE.Text);
        tf1[5] = Convert.ToInt32(tfF.Text);

        for (int i = 0; i <= 0; i++)
        {

            ti[i] = tf1[i] + 5;
        }

    }
}
8
  • for (int i = 0; i <= 0; i++) Commented Nov 4, 2015 at 15:04
  • 1
    First of all, your for loop is never going to execute its code block. You are attempting to do a loop until i<=0 which is what you are initializing it as int i = 0;. As a side note, instead of using Convert.ToInt32(), you should look into int.TryParse() to ensure that you will always have a valid value. The way you have it right now could throw an exception if the user inputs anything other than a numeric value. Commented Nov 4, 2015 at 15:11
  • Change your for loop to: for (int i = 0; i <= 5; i++) to iterate 6 times. What are you trying to do inside the for loop? Add 5 to tf1[i] and put the result into ti[i]? Commented Nov 4, 2015 at 15:14
  • Looking at the loop is all well and good, but the function doesn't actually do anything. The changes to the arrays drop out of scope before they are written anywhere. Commented Nov 4, 2015 at 15:19
  • @Matt I am assuming (though I may be horribly wrong) that he has either stripped out the excess bloat to focus on the core problem, or he watching it in debugger. You are correct though, as is, that function really does nothing but burn CPU cycles, and not really much at that considering the loop code block never executes. =) Commented Nov 4, 2015 at 15:23

2 Answers 2

2

Since you always work with 6 elements, you can change your array creation to this:

const int size = 6;
int[] ti = new int[size];
int[] t = new int[size];
int[] tf1 = new int[size];

And the loop changes to

for (int i = 0; i < size; i++)
{
    ti[i] = tf1[i] + 5;
}

By the way, what does the +5 expression mean?

Sign up to request clarification or add additional context in comments.

1 Comment

Forget about that 5. I don't know what demon posessed me. :P I guess it should be best to just do the operations "under the hood" and convert them to text and show them in the textboxes?
0

I don't know what you think you are doing in fifo_click() but since you are doing it to variables inside the scope of the function it isn't going to appear to do anything. I don't want to do you work for you, so this only gives you a push in the right (?) direction, but if its not enough shout.

If you are define a number of identical objects then I think you ought to think about a class. Something along these lines:

class Element
{
  public int mValue;
  public TextBox mControl;
  public Element (TextBox control) {mControl = control;}
};
class FIFO
{
  public FIFO (Element[] elements) { ... }
  public void SetElement(int index, String value) {...}
  public Element[] mElements;
};

You want to store an array of FIFO instances as class members and then they will retain their data between calls.

Oh and comment your code, because it makes it so much easier to understand WTF is going on :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.