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. How to use arrays as properties of a class?

How to use arrays as properties of a class?

Scheduled Pinned Locked Moved C#
tutorialquestiondata-structureshelp
6 Posts 5 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.
  • D Offline
    D Offline
    dnahelix
    wrote on last edited by
    #1

    I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?

    dnahelix

    L A N L 4 Replies Last reply
    0
    • D dnahelix

      I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?

      dnahelix

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      You need to give your class an "indexer."

      public class Patient
      {
      private string[] m_labValues;

      public Patient(int labValueCount) 
      {
          m\_labValues = new string\[labValueCount\];
      
          for(int i = 0; i < m\_labValues.Length; i++)
          {
              m\_labValues\[i\] = string.Empty;
          }
      }
      
      public string this\[int index\]
      {
          get
          {
              return m\_labValues\[index\];
          }    
          set 
          { 
              m\_labValues\[index\] = value; 
          }
      }
      

      }

      private Patient patient = new Patient(6);
      patient[0] = "B.P is 120,80";
      patient[1] = "Blood Sugar is 160";

      Error checking omitted for brevity's sake.

      1 Reply Last reply
      0
      • D dnahelix

        I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?

        dnahelix

        A Offline
        A Offline
        andre_swnpl
        wrote on last edited by
        #3

        You could simply do the following: public string[] LabValues { get { return m_labValues; } set { m_labValues = value; } } } This will work but is not considdered good coding. You should rather use a generic list or a public method to set the array. public void SetLabValues(string[] ar) { m_labValues=ar; }

        1 Reply Last reply
        0
        • D dnahelix

          I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?

          dnahelix

          N Offline
          N Offline
          Nouman Bhatti
          wrote on last edited by
          #4

          try to read on "Indexers" in c# leslie sanford gave an example of it also

          D 1 Reply Last reply
          0
          • N Nouman Bhatti

            try to read on "Indexers" in c# leslie sanford gave an example of it also

            D Offline
            D Offline
            dnahelix
            wrote on last edited by
            #5

            I thank you all for your kind help. Leslie Sanford's example looks wonderful. I shall implement this and see. Thanks again:)

            dnahelix

            1 Reply Last reply
            0
            • D dnahelix

              I am unable to figure out how to use the get-set style public properties to manipulate the private arrays of that class? for example public class Patient { private string[] m_labValues //this is what i am clueless about. //how do i get down to elements of this array? //i am sure that this implementation is wrong. public string LabValues { get { return m_labValues; } set { m_labValues = value; } } } from my main class i desire an ability to directly manipulate the elements...like: private Patient patient = new Patient(); patient.LabValues=new String[6]; patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160" .. .. .. I'm totally clueless about implementing this. I understand that if i declare the m_LabValues itself as public, i will be able to manipulate it directly with out using the get-set style Can some one please help me?

              dnahelix

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi,

              dnahelix wrote:

              public string LabValues { get { return m_labValues; } set { m_labValues = value; } }

              If you want the array m_labValues as a property, you must include the [] in its type: public string[] LabValues {...} That way your statements patient.LabValues[0]="B.P is 120,80"; patient.LabValues[1]="Blood Sugar is 160"; are OK If there is only one string array to expose (make publicly available) you could work with an indexor, as others already told you; but for more than one array, that becomes difficult since an indexor always uses the class name (in the ouside world) and this (within the class intself), so the only way to differentiate is by the type of the index variable, which does not have to be an int at all (it could be a string, or whatever you like), but would be an int if it really stands for an index in an array. :)

              Luc Pattyn [My Articles] [Forum Guidelines]

              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