0

So I've been trying to figure out how to get my code to work all night. I've been reading up on all kinds of stuff and trying to identify what I'm doing wrong, but everything I try I end up at the same issue. I'm trying to change a variable in my class by referencing it in a method so it will change in the class and not just locally. But I don't know what to put as a parameter for the ref Storyboard SB. Can someone tell me what should be done, I've tried setting it to null, even through a variable and it doesn't work. Also 'StoryBoard' is the class that I'm writing the code in.

public class StoryBoard
{
    public string[] TextBoxes = new string[10];
    public int Counter = 0;
    private void RtClickButton_Click(object sender, EventArgs e)
    {
        RtClickButton_ClickImpl(sender, e, "what would I put here?");
    }

    private void RtClickButton_ClickImpl(object sender, EventArgs e, ref StoryBoard SB)
    {
        string TBT = TxtBox.Text;
        switch(Counter)
        {
            case 0:
                TextBoxes[Counter] = TBT;
                break;
        }
        SB.Counter++; // Adds 1 to the counter.
        LtClickButton.Enabled = true;
        TxtBox.Clear(); // Clears the text box.
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Donut
  • 7
  • 2

2 Answers2

1

Try simply

Counter++;

or if in doubt you can use the this keyword to refer to instance members of this class, e.g

this.Counter++; // Adds 1 to the counter.

To expand upon this, all variables from the current object will always be accessible in a normal method (i.e. not static) unless a variable of the same name exists in the same scope, where the scope can be the method or a single block between curly braces.

If you use the this keyword it will always reference the variable that belongs to the object/class and not an inline variable that is defined in a different scope.

codemonkeh
  • 2,054
  • 1
  • 20
  • 36
-1

But I don't know what to put as a parameter for the ref Storyboard SB.

Keep a private member variable for SB:

private StoryBoard _SB = null;  //A member variable to hold the StoryBoard object

public class Form1WhatEver
{
  public Form1WhatEver()
  {
      //Instantiate a reference to the StoryBoard and hold it in the private member variable
      _SB = new StoryBoard();
  }

  public string[] TextBoxes = new string[10];
  public int Counter = 0;
  private void RtClickButton_Click(object sender, EventArgs e)
  {
      RtClickButton_ClickImpl(sender, e, ref _SB); //Pass the instance of StoryBoard byRef.

      //Check that our _SB Counter variable was incremented (+1)
      System.Diagnostics.Debug.WriteLine(_SB.Counter.ToString()); 
  }

  private void RtClickButton_ClickImpl(object sender, EventArgs e, ref StoryBoard SB)
  {
      string TBT = TxtBox.Text;
      switch(Counter)
      {
        case 0:
            TextBoxes[Counter] = TBT;
            break;
       }
       SB.Counter++; // Adds 1 to the counter.
       LtClickButton.Enabled = true;
       TxtBox.Clear(); // Clears the text box.
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • You should add the fact that ˋrefˋ is not needed in this case – Sir Rufo Apr 21 '17 at 06:03
  • 1
    I'm glad you mentioned it, I dont really feel like going into all the details about Value & Ref Types and ByVal and ByRef parameters. OP can read up it. Its a topic larger than this question. – Jeremy Thompson Apr 21 '17 at 06:07
  • @Donut read http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c and the answer from Jon Skeet and the linked article from him – Sir Rufo Apr 21 '17 at 06:11
  • @SirRufo I added what you said, but the only problem is that in the method where I need to instantiate the reference, I get a stackOverflowException when I run the program. Inside that method is InitalizeComponent(); When I hover over it, it says 'Required method for designer support - do not edit the contents of this method with the code editor. So I removed the reference code and it worked again, but how else can I reference StoryBoard? Also I read the link you posted, it was very helpful, thanks :) Also, I don't know if it matters, but I'm making a windows forms project – Donut Apr 21 '17 at 08:32
  • @JeremyThompson I realized I mentioned the wrong person in comment before this whoops – Donut Apr 21 '17 at 09:03
  • Wierd how it's been down voted, anyone care to explain? – Jeremy Thompson Oct 12 '17 at 08:34