Array of Strings....
-
Hi All, I am writing a program to download some data off a piece of hardware. My thinking was have an array fill it. The data is in the form of strings rather than ints (which have done this with in the past) my question is strings are bigger than ints so will this cause issues with overflow and what is the correct way of handling groups of strings?? Glenn
First of all, why an array? Why not a
List
of strings? Second, are the strings unique or are they just a well defined list that you could get some combination of? If it's a well known list, just assign an enumeration to the strings and return a list of those enumerations. Alternatively, you could write a slightly more complex algorithm and use aDictionary
to keep the string, and a list of indexes where that string occur - and then decode this at the client end.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
First of all, why an array? Why not a
List
of strings? Second, are the strings unique or are they just a well defined list that you could get some combination of? If it's a well known list, just assign an enumeration to the strings and return a list of those enumerations. Alternatively, you could write a slightly more complex algorithm and use aDictionary
to keep the string, and a list of indexes where that string occur - and then decode this at the client end.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
I have only really looked at List from a distance can you recommend a good tutorial? I just need to get the strings and possibly check them and save them. Glenn
-
I have only really looked at List from a distance can you recommend a good tutorial? I just need to get the strings and possibly check them and save them. Glenn
Here you go. Defining a list of strings:
List<string> myStrings = new List<string>();
Doing the same and setting the initial size to 100 elements
List<string> myStrings = new List<string>(100);
Adding to the string
myStrings.Add("A string");
That's pretty much it as far as you need to get started.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Here you go. Defining a list of strings:
List<string> myStrings = new List<string>();
Doing the same and setting the initial size to 100 elements
List<string> myStrings = new List<string>(100);
Adding to the string
myStrings.Add("A string");
That's pretty much it as far as you need to get started.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Really that simple? (something somewhere is going to go bang!) Thanks Glenn!
-
Really that simple? (something somewhere is going to go bang!) Thanks Glenn!
Really that simple. Of course, there's a lot more you can do with the list, but that's enough to get you going.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Really that simple. Of course, there's a lot more you can do with the list, but that's enough to get you going.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Cool, I have also found "http://www.codeproject.com/Articles/38447/Generic-List-C">Generic List (C#) Looks like that is my afternoon sorted! Glenn
-
Hi All, I am writing a program to download some data off a piece of hardware. My thinking was have an array fill it. The data is in the form of strings rather than ints (which have done this with in the past) my question is strings are bigger than ints so will this cause issues with overflow and what is the correct way of handling groups of strings?? Glenn
Yes a List, or a Queue if you want to have one thread adding values and another processing them. :thumbsup:
glennPattonWork wrote:
strings rather than ints
If they are ints that have been stringified, then why not Parse them and then add them to the collection?
-
Yes a List, or a Queue if you want to have one thread adding values and another processing them. :thumbsup:
glennPattonWork wrote:
strings rather than ints
If they are ints that have been stringified, then why not Parse them and then add them to the collection?
Good, These are strings, I meant I had used an integer array to store the data as it came in. I was thinking that as Strings are bigger in size I didn't want to slow everything down by trying to handle a load of them as an array, (seemed to me like having one huge box containing smaller boxes rather unwieldy!) Glenn
-
Good, These are strings, I meant I had used an integer array to store the data as it came in. I was thinking that as Strings are bigger in size I didn't want to slow everything down by trying to handle a load of them as an array, (seemed to me like having one huge box containing smaller boxes rather unwieldy!) Glenn
The alternative, as noted in this part of the thread, is to use a
Queue
so you could remove items when you are finished processing them - this helps to keep things down to a more managable size.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Really that simple. Of course, there's a lot more you can do with the list, but that's enough to get you going.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
It can insert multiple values or characters in your program. Array of string is the best way to insert multiple character to take at a time with the help of strings. Most of string start from zero. Three kind of strings are there : one pair string,two pair string and multiple pair string.
-
It can insert multiple values or characters in your program. Array of string is the best way to insert multiple character to take at a time with the help of strings. Most of string start from zero. Three kind of strings are there : one pair string,two pair string and multiple pair string.
Are you really sure you meant to reply to me? The OP doesn't get notified if you post an answer to my posts.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hi All, I am writing a program to download some data off a piece of hardware. My thinking was have an array fill it. The data is in the form of strings rather than ints (which have done this with in the past) my question is strings are bigger than ints so will this cause issues with overflow and what is the correct way of handling groups of strings?? Glenn
While text (strings) may be a good way to transfer information between systems, it is not the recommended way to store data in memory. So my approach would be slightly different, I'd have: - a textual representation of the data, as an example X123Y456E would be a point with X and Y coordinates (E stands for a terminator, think newline character); - a class that holds an instance of data, in the example:
public class MyPoint {
public int X;
public int Y;
}BTW: I prefer a class over a struct to avoid all kinds of problems including boxing/unboxing. - some container that holds all the data, this could be
List<MyPoint> myCollectionOfPoints
in the example. - a method to receive textual data, convert it on the spot, and store it; it might look like:public void ReceiveMyPoint() {
string line=mySerialPort.ReadLine(); // this is why newline is a good terminator!
... here comes input validation and parsing, resulting in actual values:
int x=...;
int y=...;
myCollectionOfPoints.Add(new MyPoint(x,y));
}- a thread to consume all incoming data, basically a loop calling ReceiveMyPoint() all the time. I don't use the DataReceived event here, as that does not synchronize with incoming text lines, it fires randomly when "some data" is available, which may well be a partial line of text. The net result is your interface is textual, the data is in meaningful types (two ints in the example), conversion is not a separate step, and no storage space gets wasted. Note: you must program defensively when receiving data from another system, as it will go wrong sooner or later, with a bad conenction, some missing characters, a noisy line, whatever. Therefore validation is important; even the simplest checksum or CRC added to each message would prove valuable. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
While text (strings) may be a good way to transfer information between systems, it is not the recommended way to store data in memory. So my approach would be slightly different, I'd have: - a textual representation of the data, as an example X123Y456E would be a point with X and Y coordinates (E stands for a terminator, think newline character); - a class that holds an instance of data, in the example:
public class MyPoint {
public int X;
public int Y;
}BTW: I prefer a class over a struct to avoid all kinds of problems including boxing/unboxing. - some container that holds all the data, this could be
List<MyPoint> myCollectionOfPoints
in the example. - a method to receive textual data, convert it on the spot, and store it; it might look like:public void ReceiveMyPoint() {
string line=mySerialPort.ReadLine(); // this is why newline is a good terminator!
... here comes input validation and parsing, resulting in actual values:
int x=...;
int y=...;
myCollectionOfPoints.Add(new MyPoint(x,y));
}- a thread to consume all incoming data, basically a loop calling ReceiveMyPoint() all the time. I don't use the DataReceived event here, as that does not synchronize with incoming text lines, it fires randomly when "some data" is available, which may well be a partial line of text. The net result is your interface is textual, the data is in meaningful types (two ints in the example), conversion is not a separate step, and no storage space gets wasted. Note: you must program defensively when receiving data from another system, as it will go wrong sooner or later, with a bad conenction, some missing characters, a noisy line, whatever. Therefore validation is important; even the simplest checksum or CRC added to each message would prove valuable. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Great thanks for that, can you recommend a reference for this type of data handling (or really any data handling would be useful) It's just I am tasked with writing an application to interface to a board that does exist (yet!) "it will do it like the X board, you already written the software for that". I'm looking at this as a chance to increase my Windows knowledge! Glenn
-
Great thanks for that, can you recommend a reference for this type of data handling (or really any data handling would be useful) It's just I am tasked with writing an application to interface to a board that does exist (yet!) "it will do it like the X board, you already written the software for that". I'm looking at this as a chance to increase my Windows knowledge! Glenn
Sorry, no reference, just my own experience with peripherals and embedded systems. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Sorry, no reference, just my own experience with peripherals and embedded systems. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Ah well google & MSDN, I did look at something on the code project that confused me! (link earlier) Glenn
-
While text (strings) may be a good way to transfer information between systems, it is not the recommended way to store data in memory. So my approach would be slightly different, I'd have: - a textual representation of the data, as an example X123Y456E would be a point with X and Y coordinates (E stands for a terminator, think newline character); - a class that holds an instance of data, in the example:
public class MyPoint {
public int X;
public int Y;
}BTW: I prefer a class over a struct to avoid all kinds of problems including boxing/unboxing. - some container that holds all the data, this could be
List<MyPoint> myCollectionOfPoints
in the example. - a method to receive textual data, convert it on the spot, and store it; it might look like:public void ReceiveMyPoint() {
string line=mySerialPort.ReadLine(); // this is why newline is a good terminator!
... here comes input validation and parsing, resulting in actual values:
int x=...;
int y=...;
myCollectionOfPoints.Add(new MyPoint(x,y));
}- a thread to consume all incoming data, basically a loop calling ReceiveMyPoint() all the time. I don't use the DataReceived event here, as that does not synchronize with incoming text lines, it fires randomly when "some data" is available, which may well be a partial line of text. The net result is your interface is textual, the data is in meaningful types (two ints in the example), conversion is not a separate step, and no storage space gets wasted. Note: you must program defensively when receiving data from another system, as it will go wrong sooner or later, with a bad conenction, some missing characters, a noisy line, whatever. Therefore validation is important; even the simplest checksum or CRC added to each message would prove valuable. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
Thanks for the Help! I now have it getting the data tomorrow I can work on it further,but now have the bones. Glenn
-
First of all, why an array? Why not a
List
of strings? Second, are the strings unique or are they just a well defined list that you could get some combination of? If it's a well known list, just assign an enumeration to the strings and return a list of those enumerations. Alternatively, you could write a slightly more complex algorithm and use aDictionary
to keep the string, and a list of indexes where that string occur - and then decode this at the client end.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Thanks for the Help! I now have it getting the data tomorrow I can work on it further,but now have the bones. Glenn
-
Thanks for the Help! I now have it getting the data tomorrow I can work on it further,but now have the bones. Glenn
You're welcome. I look forward to finding out how you get on.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
You're welcome. I look forward to finding out how you get on.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
You are not the only one, it would easier if I had more direction than oh it work like X when X is not fully tested and the board you are writing the code for does not exist yet, but to keep the powers that be happy (Gannt charts running) "The Windows Interface Code Must Be Started:". Will let you know! Glenn (Dilbert anyone??)