Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sort thru arrays for elements

Sort thru arrays for elements

Scheduled Pinned Locked Moved C#
questiondata-structures
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    onetreeup
    wrote on last edited by
    #1

    I have an array with 4 elements in each record of 8 records, the first element is a string, the other 3 are int. How do I pull the 3 elements, i.e., 98, 97, 98 from the record, then add them together? Thanks

    A 1 2 Replies Last reply
    0
    • O onetreeup

      I have an array with 4 elements in each record of 8 records, the first element is a string, the other 3 are int. How do I pull the 3 elements, i.e., 98, 97, 98 from the record, then add them together? Thanks

      A Offline
      A Offline
      ABitSmart
      wrote on last edited by
      #2

      You could use a LINQ query to that for you. Without knowing your actual data structure it would be difficult to provide a sample query.

      1 Reply Last reply
      0
      • O onetreeup

        I have an array with 4 elements in each record of 8 records, the first element is a string, the other 3 are int. How do I pull the 3 elements, i.e., 98, 97, 98 from the record, then add them together? Thanks

        1 Offline
        1 Offline
        12Code
        wrote on last edited by
        #3

        create a class

        public class array_of_4element()
        {
            public array_of_4element(string s, params int[] i)
            {
               str = s;
               num.CopyTo(i,0);
            }
            string str;
            int[] num = new int[3];
        
            public int sum()
            {
               return num[0]+num[1]+num[2];
            }
        }
        

        so your

        onetreeup wrote:

        an array with 4 elements in each record of 8 records

        would be done in

        List<array_of_4element> 8_records = new List<array_of_4element>(8);
        
        // add first record
        array_of_4element arr = new array_of_4element("text1", 98,97,98);
        8_records.Add(arr);
        
        // get first record, sum of num values
        8_records[0].sum();
        

        8.Kelvin()
        {
        while (!(the machine can program itself))
        Wont_stop_coding = true;
        }

        modified on Monday, March 23, 2009 12:24 AM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups