2-D Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: 2-D Array

  1. #1
    Join Date
    Sep 2008
    Posts
    13

    2-D Array

    using VS9.0, C#

    I am having a tough time getting my data into an array. I am reading data from a serial port. I get a long string that looks like so:

    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,

    I want an array that looks like this:

    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n, (one record)
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,
    XXXX, XX/XX/XX, XX:XX, XXXXXXXX\r\n,

    There is no way to tell how many records will be coming in...the max amount is 300

    I am using the serialPort's datareceived event to grab the string...I have an idea how I think I can get what I want:

    1) take the long string and spilt it at each '\r\n' to make a 1-d array**
    2) count the number of elements in that array (to get number of records for 2-d array initializing)
    3) get a 2-d array [numOfRecords, 4]
    4) spilt each element of the 1-d array at each ',' and put into the new 2-d array

    **where I got stuck (don't know how many records...can't set size of array...now, i am lost).

    if anyone has a good idea how I can do this...or any suggestions, it would be much appreciated.

    Thanks!

    (PS. I would post my code...but it's a big load of crap that doesn't work...i'm ashamed)

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: 2-D Array

    changing your structure into 4 generic lists and coupling them with indices would be an offer.
    Please rate my post if it was helpful for you.
    C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: 2-D Array

    Quote Originally Posted by toraj58 View Post
    changing your structure into 4 generic lists and coupling them with indices would be an offer.
    I agree that using a dynamic class (such as List<T>) would be a good way to go, but would almost definately go in the opposite direction
    Code:
    class ARecord
    {
       public string Part1 {get; set;}
       public string Part2 {get; set;}
       public string Part3 {get; set;}
    }
    
    class MyData : List<ARecord> {}
    MUCH easier to work with...
    Code:
    var foo = from myData where p.Part1.Contains("Foo") select p;
    ps: I think the word you meant was either "option" or "alternative."..
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center