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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Struct Arrays

Struct Arrays

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
3 Posts 2 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.
  • S Offline
    S Offline
    Stephen_Mc
    wrote on last edited by
    #1

    Hi, I have created a structure as follows public struct symbol { char szKeyword; int dflt; bool fPassDflt; int idx; } symbol SYM; I would like to create an array of the structure and write the variables in. I have found a sample in the help for a normal array as follows: int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; What I want to do is pass the values of the structure but I can't get this to work. Any suggestions? Thanks Stephen

    G 1 Reply Last reply
    0
    • S Stephen_Mc

      Hi, I have created a structure as follows public struct symbol { char szKeyword; int dflt; bool fPassDflt; int idx; } symbol SYM; I would like to create an array of the structure and write the variables in. I have found a sample in the help for a normal array as follows: int[,] myArray = new int[,] {{1,2}, {3,4}, {5,6}, {7,8}}; What I want to do is pass the values of the structure but I can't get this to work. Any suggestions? Thanks Stephen

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Make a constructor in the struct. I have given the member variables some more descriptive names, but as I couldn't figure your variable names out, I may be way off what you intended. ;) I also made the member variables private and made read-only properties for them, so that the structure is immutable.

      public struct Symbol {

      private char keyword;
      private int defibrilator;
      private bool passDefibrilator;
      private int index;

      public symbol( char keyword, int defibrilator, bool passDefibrilator, int index) {
      this.keyword = keyword;
      this.defibrilator = defibrilator;
      this.passDefibrilator = passDefibrilator;
      this.index = index;
      }

      public char Keyword { get { return this.keyword; } }
      public int Defibrilator { get { return this.defibrilator; } }
      public bool PassDefibrilator { get { return this.passDefibrilator; } }
      public int Index { get { return this.index; } }

      }

      As you have a constructor, you can easily create an array of structures:

      Symbol[] symbols = new Symbol[] {
      new Symbol('a', 42, true, 1),
      new Symbol('b', 8, false, 5),
      new Symbol('c', 199866, false, 377378423),
      };

      --- b { font-weight: normal; }

      S 1 Reply Last reply
      0
      • G Guffa

        Make a constructor in the struct. I have given the member variables some more descriptive names, but as I couldn't figure your variable names out, I may be way off what you intended. ;) I also made the member variables private and made read-only properties for them, so that the structure is immutable.

        public struct Symbol {

        private char keyword;
        private int defibrilator;
        private bool passDefibrilator;
        private int index;

        public symbol( char keyword, int defibrilator, bool passDefibrilator, int index) {
        this.keyword = keyword;
        this.defibrilator = defibrilator;
        this.passDefibrilator = passDefibrilator;
        this.index = index;
        }

        public char Keyword { get { return this.keyword; } }
        public int Defibrilator { get { return this.defibrilator; } }
        public bool PassDefibrilator { get { return this.passDefibrilator; } }
        public int Index { get { return this.index; } }

        }

        As you have a constructor, you can easily create an array of structures:

        Symbol[] symbols = new Symbol[] {
        new Symbol('a', 42, true, 1),
        new Symbol('b', 8, false, 5),
        new Symbol('c', 199866, false, 377378423),
        };

        --- b { font-weight: normal; }

        S Offline
        S Offline
        Stephen_Mc
        wrote on last edited by
        #3

        Thanks so much.

        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