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. Jag Arrays

Jag Arrays

Scheduled Pinned Locked Moved C#
data-structuresquestion
5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, is it possible to have a jaggard array of different datatypes on each row ?

    R J 2 Replies Last reply
    0
    • L Lost User

      Hi, is it possible to have a jaggard array of different datatypes on each row ?

      R Offline
      R Offline
      rchiav
      wrote on last edited by
      #2

      I don't think so.. because you're declaring an array that holds arrays of a certian type. I think you're going to want an object or a struct. I'm guessing a stuct is going to be more suited to what you need. Example..

      using System;

      class MyApp
      {
      static void Main() {
      myStruct testVar = new myStruct();
      testVar.intArray = new int[] {1,2,3};
      testVar.strArray = new String[] {"One", "Two", "Three"};

      	Console.WriteLine("{0} - {1}", testVar.intArray\[2\], testVar.strArray\[2\]);
      }
      
      struct myStruct 
      {
      	public int\[\] intArray;
      	public string\[\] strArray;
      };
      

      }

      structs are going to be faster than objects. But if you want to iterate through members, you should create an object that can be used with a "foreach" loop.

      1 Reply Last reply
      0
      • L Lost User

        Hi, is it possible to have a jaggard array of different datatypes on each row ?

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        It is possible, there is on cavet though. If one of your datatypes is a ValueType (ie, you can't set it to null) an exception will be thrown when you do a foreach on the array.

        object [] test = new object[2];
        test[0] = new string[4];
        test[1] = new Type[2];

        int j;
        for(j = 0; j < 4; j++)
        {
        ((string[]) test[0])[j] = "moo" + j.ToString();
        }

        ((Type[]) test[1])[0] = typeof(object);
        ((Type[]) test[1])[1] = typeof(System.Guid);

        foreach(object [] t in test)
        {
        foreach( object i in t )
        {
        Console.Write("{0}, ", i);
        }
        Console.WriteLine("");
        }

        Console.ReadLine();

        Sorry for the horrible formatting, the pre tag seems to be taking out all blank lines :( James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

        R L 2 Replies Last reply
        0
        • J James T Johnson

          It is possible, there is on cavet though. If one of your datatypes is a ValueType (ie, you can't set it to null) an exception will be thrown when you do a foreach on the array.

          object [] test = new object[2];
          test[0] = new string[4];
          test[1] = new Type[2];

          int j;
          for(j = 0; j < 4; j++)
          {
          ((string[]) test[0])[j] = "moo" + j.ToString();
          }

          ((Type[]) test[1])[0] = typeof(object);
          ((Type[]) test[1])[1] = typeof(System.Guid);

          foreach(object [] t in test)
          {
          foreach( object i in t )
          {
          Console.Write("{0}, ", i);
          }
          Console.WriteLine("");
          }

          Console.ReadLine();

          Sorry for the horrible formatting, the pre tag seems to be taking out all blank lines :( James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

          R Offline
          R Offline
          rchiav
          wrote on last edited by
          #4

          Didn't even think about an object type.. thanks for pointing that out.

          1 Reply Last reply
          0
          • J James T Johnson

            It is possible, there is on cavet though. If one of your datatypes is a ValueType (ie, you can't set it to null) an exception will be thrown when you do a foreach on the array.

            object [] test = new object[2];
            test[0] = new string[4];
            test[1] = new Type[2];

            int j;
            for(j = 0; j < 4; j++)
            {
            ((string[]) test[0])[j] = "moo" + j.ToString();
            }

            ((Type[]) test[1])[0] = typeof(object);
            ((Type[]) test[1])[1] = typeof(System.Guid);

            foreach(object [] t in test)
            {
            foreach( object i in t )
            {
            Console.Write("{0}, ", i);
            }
            Console.WriteLine("");
            }

            Console.ReadLine();

            Sorry for the horrible formatting, the pre tag seems to be taking out all blank lines :( James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks a bunch guys :)

            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