where to declare variables

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

Here's my full code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PatternMatcher
{
class Program
{
private const string path = @"C:\switches.txt";

static void Main(string[] args)
{
RunPatternMatcher();
}

private static void RunPatternMatcher()
{
InitializeArrays();
}

private static void InitializeArrays()
{
//FileStream stream = new FileStream(path, FileMode.Open,
FileAccess.Read);
//StreamReader readSwitchValues = new StreamReader(stream);

StreamReader readSwitchValues = new StreamReader(path);

string[] allSwitchValues =
readSwitchValues.ReadToEnd().Split('|');
readSwitchValues.Close();

string[][,] panelOne = FillPanel(allSwitchValues, 0);
string[][,] panelTwo = FillPanel(allSwitchValues, 8 * 2 * 10);
string[][,] panelThree = FillPanel(allSwitchValues, 8 * 2 *
10 * 2);
string[][,] panelFour = FillPanel(allSwitchValues, 8 * 2 *
10 * 3);

//string[][][,] allPanels = new string[4][][,];
//allPanels[0] = panelOne;
//allPanels[1] = panelTwo;
//allPanels[2] = panelThree;
//allPanels[3] = panelFour;
}

private static string[][,] FillPanel(string[] switches, int
startIndex)
{
string[][,] panel = new string[8][,];

for (int i = 0; i < 8; i++)
{
panel = new string[2, 10];

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
panel[x, y] = switches[startIndex++];
}

return panel;
}
}
}


The thing is, I need access to the panel arrays after the
InitializeArrays() method is done, so I guess I can't declare them
within the method. Should they be declared in the RunPatternMatcher
method? Or in the class itself? Should they be static?
 
What you want are called fields.

Here's how you declare them:

namespace PatternMatcher {

class Program {

private const string path = @"c:\switches.txt";

string[][,] panelOne; // these are fields, just as
string[][,] panelTwo; // your const above is a field.
string[][,] panelThree;

//... your code continues.

}
}
 
jeremiah said:
string[][,] panelOne; // these are fields, just as
string[][,] panelTwo; // your const above is a field.

John should also note that he would need to declare the fields static,
because all of his routines are static. Either that or create a real
object and make all the methods (except Main) non-static.

I would do the latter. Statics have a nasty habit of causing code to
become inflexible as it grows. Static variables are basically just
global variables encapsulated in a class. They're not quite as evil as
completely global variables in old languages like C, but they do share
most of the disadvantages. I avoid statics except where they are
absolutely needed, e.g. for singletons.

-- Peter Gummer
 
Peter said:
jeremiah johnson wrote:

string[][,] panelOne; // these are fields, just as
string[][,] panelTwo; // your const above is a field.


John should also note that he would need to declare the fields static,
because all of his routines are static. Either that or create a real
object and make all the methods (except Main) non-static.

I would do the latter. Statics have a nasty habit of causing code to
become inflexible as it grows. Static variables are basically just
global variables encapsulated in a class. They're not quite as evil as
completely global variables in old languages like C, but they do share
most of the disadvantages. I avoid statics except where they are
absolutely needed, e.g. for singletons.

-- Peter Gummer

Thanks Jeremiah, your method worked great.

Peter, if I use your way, how exactly would that go? Would I create an
object of type Program? Or do I need another class?
 
John said:
Peter, if I use your way, how exactly would that go? Would I create
an object of type Program? Or do I need another class?

Either. The simplest thing would be to create an object of type Program.

-- Peter Gummer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top