473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

where to declare variables

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[i] = new string[2, 10];

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
panel[i][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?
Nov 17 '05 #1
5 4697
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.

}
}

John Salerno wrote:
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[i] = new string[2, 10];

for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
panel[i][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?

Nov 17 '05 #2
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
Nov 17 '05 #3
Peter Gummer wrote:
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?
Nov 17 '05 #4
John Salerno wrote:
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
Nov 17 '05 #5
Peter Gummer wrote:
John Salerno wrote:

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


Thanks.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
9
by: vivekian | last post by:
Have a couple of global variables of a class type which i have declared and defined in the application. 1. Not sure where should the global variables be placed ? 2. Would i need to forward...
15
by: esha | last post by:
I need to have a Public variable in my project. In VB it can be declared in a standard module. Where can I do it in C# ? I tried to do it in default class Program.cs and I tried it in an added by...
1
by: ares.lagae | last post by:
- I have a typelist and I want to declare a member variable for each of the types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to declare...
9
by: johnlim20088 | last post by:
Hi, Hi, currently I developing an application in C# NET, let say i have a page call viewrecord.aspx, in this page, i have a global variables declared before the page_Load, so all function can...
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
3
by: sravik | last post by:
how and where to declare global variables in C#. Like Conection strings and some file paths etc.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.