473,396 Members | 1,799 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,396 software developers and data experts.

another shot at this

private static void InitializeArrays()
{
FileStream stream = new FileStream(path, FileMode.Open,
FileAccess.Read);
StreamReader readSwitches = new StreamReader(stream);
int i = 0;

using (readSwitches)
{
string row = readSwitches.ReadToEnd();
string[] switches = row.Split('|');

string[, ,] panelOne = FillPanel(switches);
}
}

private static string[, ,] FillPanel(string[] switches)
{
string[, ,] panel;

for (int z = 0; z < 8; z++)
{
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 10; y++)
panel[x, y, z] = switches[i++];
}
}

return panel;
}

Ok, I made it to where the switches array contains all the information
that I then need to put into the panel arrays. Each panel array (four of
them) will consist of 8 different 2x10 arrays. I'm still trying to
figure out a good way to fill them up. The above code won't compile, but
I feel like I'm getting closer. I'd appreciate any comments.
Nov 17 '05 #1
8 1069
I replied to this in another post, but since you posted a more
complete code sample, here is my reply.

static string[][,,] panels = new string[4][,,];
// or
// string[,,] panelOne;
// string[,,] panelTwo;
// ...

private static void InitializeArrays()
{
// Streamreader has an overload that takes a path, so there is
// no need to first get the filestream.
using(StreamReader readSwitches = new StreamReader(path))
{
string completeFile = readSwitches.ReadToEnd();
string[] switches = completeFile.Split('|');

if(switches.Length != 8*2*10*4)
throw new Exception("Invalid data file.");

panels[0] = FillPanel(switches,0);
panels[1] = FillPanel(switches,8*2*10);
panels[2] = FillPanel(switches,8*2*10*2);
panels[3] = FillPanel(switches,8*2*10*3);
// or
// panelOne = FillPanel(switches,0);
// panelTwo = FillPanel(switches,8*2*10);
// ...
}
}

private static string[, ,] FillPanel(string[] switches,int startIndex)
{
string[,,] panel = new string[2,10,8];
for (int z = 0; z < 8; z++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
panel[x, y, z] = switches[startIndex++];
return panel;
}
--
Marcus Andrén
Nov 17 '05 #2
Marcus Andrén wrote:
// Streamreader has an overload that takes a path, so there is
// no need to first get the filestream.
I created the filestream because I wanted to make the file readonly, but
I guess if I don't do any explicit writing, it will effectively be a
readonly file anyway, right? (It's a text file I created filled with 0s
and 1s. Each line contains 20 digits, and there are 32 lines, so it's a
very rigid use of the file I have in mind.)
private static string[, ,] FillPanel(string[] switches,int startIndex)
{
string[,,] panel = new string[2,10,8];
for (int z = 0; z < 8; z++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 10; y++)
panel[x, y, z] = switches[startIndex++];
return panel;
}


Ah! Passing in a startIndex was what I was trying to figure out! This
helps a lot!

Now just one more question: Should I even put all the panels in one big
array? Or would it be okay to have four separate arrays? I was thinking
(as I posted in another thread, sorry!) that I could have four jagged
arrays, each representing one panel, and each containing 8 other arrays
for the 8 switches. Is it possible to have an array like string[][][,,]?
That would let me access a specific panel, switch, and position in the
switch. If that's not possible, then I'm thinking it might be better not
to use one array for all four panels, because it won't be as easy to
access each switch that way.
Nov 17 '05 #3
Ok, my new and improved version:

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

using (StreamReader readSwitches = new StreamReader(path))
{
string[] allSwitchValues =
readSwitches.ReadToEnd().Split('|');

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);
}
}

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

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

return panel;
}
I think it looks good, but who knows! :)
Nov 17 '05 #4
John Salerno wrote:
panel[i][x, y] = switches[startIndex++];


This line throws an exception:

Object reference not set to an instance of an object.
Nov 17 '05 #5
John Salerno <jo******@NOSPAMgmail.com> wrote:
John Salerno wrote:
panel[i][x, y] = switches[startIndex++];


This line throws an exception:

Object reference not set to an instance of an object.


Yes - you've created an array of multi-dimensional string arrays, but
you haven't populated it.

In the outermost loop, you need something like:

panel[i] = new string[2,10];

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
Jon Skeet [C# MVP] wrote:
Yes - you've created an array of multi-dimensional string arrays, but
you haven't populated it.

In the outermost loop, you need something like:

panel[i] = new string[2,10];


That makes sense. Last night I was thinking about this some more, and I
thought maybe the problem was this:

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);

Should each of those arrays first be created like this:

string[][,] panelOne = new string[8][,]
//panelOne[0] = new string[2, 10] //do i need this first?
panelOne[0] = FillPanel(etc...)

I guess I'm a little confused about how much needs to be initialized first.
Nov 17 '05 #7
Jon Skeet [C# MVP] wrote:
Yes - you've created an array of multi-dimensional string arrays, but
you haven't populated it.

In the outermost loop, you need something like:

panel[i] = new string[2,10];


Well, that worked, of course! Thank you! My arrays are finally filled!

Now I notice when I try to access the arrays, they are out of scope
because I'm no longer working within the using block. Is there a proper
way to write a using block so that I can still use those arrays later? I
changed it to this and it works:

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);
Nov 17 '05 #8
John Salerno <jo******@NOSPAMgmail.com> wrote:
In the outermost loop, you need something like:

panel[i] = new string[2,10];
Well, that worked, of course! Thank you! My arrays are finally filled!

Now I notice when I try to access the arrays, they are out of scope
because I'm no longer working within the using block. Is there a proper
way to write a using block so that I can still use those arrays later?


Declare them before the using block.
I changed it to this and it works:

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);


If an exception is thrown while you're reading, you won't be closing
the stream. Just declare the variables outside the using statement and
it'll be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9

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

Similar topics

2
by: sam | last post by:
Hello Coders I have a question can we have a php script or function to take screen shot for webpage? for example , let the php script take a shot for www.google.com and convert the content...
5
by: Tyler Style | last post by:
Hullo - looking for a little advice here. I have a form on a page in one domain submitting to a cgi in another domain. Weirdly, on some Windows XP systems, a form on the page fails to submit/post...
3
by: David W. Fenton | last post by:
A very old app of mine that's been in production use, and largely unchanged since about 1998 has started recently throwing error 3188 (can't update, locked by another session on this machine) when...
8
by: VooDoo | last post by:
Hi, I have created a ticket system where users can submit their problem. After several month of experience it appears that offering a zone to paste a screen shot will be ideal. Is there anyway to...
1
by: Jawad Rehman | last post by:
Hi.. I want to develp a window service in c# .the purpose of the service is to take a screen shot of a desktop after several specified time span. My problem is that how i take screen shot of...
3
by: rfuscjr via AccessMonster.com | last post by:
This is truly bizzare. I have a query that runs for hours in one Access db. When I import it into another Access db, it runs in minutes. I compacted and repaired the original, relinked tables...
3
by: Simon | last post by:
Dear reader, I have a Snap Shot file located in a folder. Now I like to view the Snap Shot file. First I created a string with the full pathname and Snap Shot file.
5
by: heday60 | last post by:
I've got this application that watches a directory for XML's and then parses them into a DB. It then moves them based on if the XML was succesful or a bad XML... foreach(FileInfo file in...
5
by: bdy120602 | last post by:
Is it possible, when a user or viewer of your Web page, prints or takes a screen shot of a Web page with mousover (roll-over) text in it, to have that text printed or captures as part of the screen...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.