3

I need to read data from an array located in another class. I have already read a few threads explaining the same problem, but can't get it to work with my code...

Included example: I need to read the data in the ParticipantX array from Parsedata class to the Form1 class, as shown in my example.

I would be really grateful for any help. Best off all would be if you could help me with the code I need. I just got stuck now. Thanks.

public class Parsedata
{
    public void ParsedataMethod()
    {
    ...
        //Neccesary data are added to this array
        string[,] ParticipantX = new string[40, 4];

In the same namespace I have the Form1 class:

using Crestron.ActiveCNX;
public partial class Form1 : Form
{
    ActiveCNX cnx;
    cnx = new ActiveCNX();

    private void SerialSend_Click(object sender, EventArgs e)
    {
        int number = 8;
        for (int i = 1; i < number; i++)
            cnx.SendSerial(i, ParticipantX[i, 1]); //
    }
2
  • If your formatting is correct, then ParticipantX is local array for ParsedataMethod() and you cannot access local variables. Can you make it static probably? Commented Mar 11, 2013 at 10:40
  • I guess refactoring your code will be a way easier solution than trying to access a method local variable Commented Mar 11, 2013 at 10:48

5 Answers 5

1

Try this:

public class Parsedata
{
     private string[,] m_ParticipantX;
     public void ParsedataMethod()
     {
       ...
       m_ParticipantX = new string[40, 4];//Neccesary data are added to this array
     }

     public string[,] ParticipantX
     {
          get { return m_ParticipantX; }
     }
}

using Crestron.ActiveCNX;
public partial class Form1 : Form
{
    ActiveCNX cnx;
    cnx = new ActiveCNX();
    Parsedata data = new Parsedata();

    private void SerialSend_Click(object sender, EventArgs e)
    {
        data.ParseDataMethod();
        int number = 8;
        for (int i = 1; i < number; i++)
            cnx.SendSerial(i, data.ParticipantX[i, 1]); //
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

First, keep in mind that property are part of class, and class has property (or field). You cannot get a property without the class owner.

In static, you can achieve as such:

public static class Parsedata{
  public static string[,] StringX{get;set;}
}

However when your class and property is not static, you need to instantiate the class to an object first. Example:

public class Parsedata{
  public string[,] StringX{get;set;}
}
public class Caller{
  Parsedata p = new Parsedata();
  public void SetStringX(string[,] stringX){
    p.StringX = stringX;
  }
  public string[,] GetStringX(){
    return p.StringX;
  }
}

However you must keep in mind that instantiated object's value are different between instances. Example (just illustration):

WebSite a = new WebSite();
a.Name = "Stack";
WebSite b = new WebSite();
b.Name = "Overflow";
Console.WriteLine(a.Name); // will result Stack
Console.WriteLine(b.Name); // will result Overflow

Comments

1

The ParticipantX array is declared inside a method, which means that it will be local to that method.

You must place the declaration outside the method:

public class Parsedata
{
    public string[,] ParticipantX;

    public void ParsedataMethod()
    {
        ...
        ParticipantX = new string[40, 4];

I've marked ParticipantX as public, so that you can access it from your form:

ParseData parseData = new ParseData();
parseData.ParticipantX[x, y] ...

A better method is to make it a public property:

private string[,] _participantX;
public string [,] ParticipantX
{
    get { return _participantX; }
}

Comments

1

You could pass it in via a delegate + event handler, for example:

public partial class Form1 : Form
{
    public string[,] ParticipantX;
    public Form1()
    {
        InitializeComponent();
        Class1.SendArray += new EventHandler<MyArgs>(GetArray);
    }

    public void GetArray(object sender, MyArgs ea)
    {
        this.Invoke(new MethodInvoker(
            delegate()
            {
              ParticipantX = ea.myArray;
            }));
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Class1 t = new Class1();
        t.ParsedataMethod();
    }
}

public class MyArgs : EventArgs
{
    public string[,] myArray { get; set; }
}

And inside of your parse data class, call OnArraySend when you want to pass the array to your form:

public static event EventHandler<MyArgs> SendArray;
    public void ParsedataMethod()
    {
        string[,] ParticipantX = new string[40, 4];
        OnArraySend(new MyArgs() { myArray = ParticipantX });
    }

    protected virtual void OnArraySend(MyArgs ea)
    {
        if (SendArray != null)
        {
            SendArray(this, ea);
        }
    }

This is a crude example but you get the general idea

Comments

-1

You can either make Parsedata and ParsedataMethod static methods or create an instance of Parsedata in your Form:

var pd = new ParseData();

Then you can use:

pd.Participant(...);

3 Comments

Would those people voting down please elaborate - voting down without a comment so helps no-one.
There is a ParseDataMethod() in his class ParseData. You need to call that method from the ParseData class instance first, then your line pd.Participant(...) is not right as the ParticipantX array is declared in the method not in the class.
This doesn't address the fact that the variable is declared inside a method's body, which makes it unaccessible in the first place.

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.