0
  private void frmExecute_Load(object sender, EventArgs e)
    {
        string[] data = File.ReadAllLines("c:\\toyotoconsole\\tssetup.txt");

        // Loop through each line, split, add if server
        for (int i = 0; i < data.Length; i++)
        {
                var serverValues = data[i].Split('|');
        }
    }

Okay first C# windows project, which is cool since I have never really touched this beyond the odd debugging foray.

My problem is the array serverValues is built in a function, how do I make this array available for all functions (not sure if using the right term here) in the same form script (probably the wrong word there as well). Is there a public declaration?

2
  • 1
    Turn local variable serverValues into a field: declare it outside: private string[] serverValues = null; ... private void frmExecute_Load(object sender, EventArgs e) {...serverValues = data[i].Split('|');...} Commented Dec 30, 2019 at 7:03
  • Even though you turn it as a local variable, variable will overwrite everytime, so at last your serverValues will contain only value of data[data.length - 1].Split('|') Commented Dec 30, 2019 at 7:12

2 Answers 2

3

Technically, you should turn local variable serverValues into a field (or property):

  private string[] serverValues = new string[0];

  private void frmExecute_Load(object sender, EventArgs e) {
    ...
    serverValues = data[i].Split('|'); 
    ...
  } 

However, as one can see, you rewrite serverValues within the loop; that's why serverValues will contain the last line splitted only. Another issue is mixing Business logic (ServerValues) and UI (form loading).

It seems you want something like this:

using System.Linq;

...

private string[] m_ServerValues = null;

// Pure buiness logic: ServerValues without any UI (form)
public string[] ServerValues {
  get { 
    // If we have the array we return it
    if (m_ServerValues != null)
      return m_ServerValues;

    // otherwise we compute it
    m_ServerValues = File
      .ReadLines("c:\\toyotoconsole\\tssetup.txt")
      .SelectMany(line => line.Split('|'))
      .ToArray();

    // And return it
    return m_ServerValues;
  } 
}

// UI: form loading
private void frmExecute_Load(object sender, EventArgs e) {
  // If you want to prefetch (it's not necessary)
  var values = ServerValues;
} 
Sign up to request clarification or add additional context in comments.

Comments

0
// Declare a private property in you class 
private serverValues = Array.Empty<string>();

And then use within any event

private void frmExecute_Load(object sender, EventArgs e)
{
    string[] data = File.ReadAllLines("c:\\toyotoconsole\\tssetup.txt");

    // Loop through each line, split, add if server
    for (int i = 0; i < data.Length; i++)
    {
        serverValues = data[i].Split('|');
    }
}

2 Comments

private string[] severValues = new string[0];: typo (severValues's type)
Thanks @DmitryBychenko, Upvoting your detailed answer

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.