Click here to Skip to main content
15,885,032 members
Home / Discussions / C#
   

C#

 
QuestionRe: Array of Strings.... Pin
Pete O'Hanlon16-May-12 4:00
mvePete O'Hanlon16-May-12 4:00 
GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 6:09
professionalglennPattonWork316-May-12 6:09 
GeneralRe: Array of Strings.... Pin
Pete O'Hanlon16-May-12 6:11
mvePete O'Hanlon16-May-12 6:11 
GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 6:18
professionalglennPattonWork316-May-12 6:18 
GeneralRe: Array of Strings.... Pin
PIEBALDconsult16-May-12 3:17
mvePIEBALDconsult16-May-12 3:17 
GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 3:26
professionalglennPattonWork316-May-12 3:26 
GeneralRe: Array of Strings.... Pin
Pete O'Hanlon16-May-12 3:44
mvePete O'Hanlon16-May-12 3:44 
AnswerRe: Array of Strings.... Pin
Luc Pattyn16-May-12 4:04
sitebuilderLuc Pattyn16-May-12 4:04 
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.

Smile | :)
Luc Pattyn [My Articles] Nil Volentibus Arduum

GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 4:18
professionalglennPattonWork316-May-12 4:18 
AnswerRe: Array of Strings.... Pin
Luc Pattyn16-May-12 4:31
sitebuilderLuc Pattyn16-May-12 4:31 
GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 4:34
professionalglennPattonWork316-May-12 4:34 
GeneralRe: Array of Strings.... Pin
glennPattonWork316-May-12 6:08
professionalglennPattonWork316-May-12 6:08 
Questionhelp me in c# Pin
ri198716-May-12 0:13
ri198716-May-12 0:13 
AnswerRe: help me in c# Pin
ri198716-May-12 0:14
ri198716-May-12 0:14 
GeneralRe: help me in c# Pin
Killzone DeathMan16-May-12 0:27
Killzone DeathMan16-May-12 0:27 
GeneralRe: help me in c# Pin
Pete O'Hanlon16-May-12 0:27
mvePete O'Hanlon16-May-12 0:27 
GeneralRe: help me in c# Pin
ri198716-May-12 0:57
ri198716-May-12 0:57 
GeneralRe: help me in c# Pin
Dave Kreskowiak16-May-12 1:38
mveDave Kreskowiak16-May-12 1:38 
GeneralRe: help me in c# Pin
Pete O'Hanlon16-May-12 1:43
mvePete O'Hanlon16-May-12 1:43 
GeneralRe: help me in c# Pin
Dave Kreskowiak16-May-12 7:59
mveDave Kreskowiak16-May-12 7:59 
GeneralRe: help me in c# Pin
Pete O'Hanlon16-May-12 1:41
mvePete O'Hanlon16-May-12 1:41 
Questionhelp me Pin
ri198715-May-12 23:35
ri198715-May-12 23:35 
AnswerRe: help me Pin
Pete O'Hanlon15-May-12 23:47
mvePete O'Hanlon15-May-12 23:47 
GeneralRe: help me Pin
ri198716-May-12 0:07
ri198716-May-12 0:07 
GeneralRe: help me Pin
ri198716-May-12 0:09
ri198716-May-12 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.