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. Get/set method for an array?

Get/set method for an array?

Scheduled Pinned Locked Moved C#
questiondata-structures
4 Posts 3 Posters 1 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.
  • F Offline
    F Offline
    frogb0x
    wrote on last edited by
    #1

    Hey all, How can I create a get/set method that returns the Nth item of an array?

    A K 2 Replies Last reply
    0
    • F frogb0x

      Hey all, How can I create a get/set method that returns the Nth item of an array?

      A Offline
      A Offline
      Alex Korchemniy
      wrote on last edited by
      #2

      What exactly are you trying to do?

      F 1 Reply Last reply
      0
      • A Alex Korchemniy

        What exactly are you trying to do?

        F Offline
        F Offline
        frogb0x
        wrote on last edited by
        #3

        Well, I have a class Foo(). Inside Foo is a private array of another class, Bar(). public Foo() { private Bar[] myBars = new Bar[10]; } When I instantiate Foo, I need to be able to assign a value to the array of Bars: myFoo.myBars[n].someValue = 10; So how can I do this?

        1 Reply Last reply
        0
        • F frogb0x

          Hey all, How can I create a get/set method that returns the Nth item of an array?

          K Offline
          K Offline
          Kastro
          wrote on last edited by
          #4

          Such a property is called an indexer. You can read a tutorial on them in the MSDN documentation (under "indexers->tutorial (C#)")The following is an example...

          public class MyArray {

          ...
          
          public Object this\[int index\] {
              get {
                  return \_Items\[index\];
              }
              set {
                  \_Items\[index\] = value;
              }
          }
          
          private Object\[\] \_Items;
          

          }

          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