Returning a string from a List, but using an accessor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    Returning a string from a List, but using an accessor

    Hi,

    Got a bit of a fun one - but first the code...

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace IndexedClass
    {
         class Program
         {
             static void Main() // remove the string args[] - unless you're passing stuff in it's pointless!
             {
                 peoplejobs folks = new peoplejobs();
                 folks.Person = "Jones";
                 folks.Job = "waiter";
                 folks.Person = "Smith";
                 folks.Job = "fitter";
                 Console.WriteLine(folks.Person[0] + "is a " + folks.Job[0]);
                 Console.WriteLine(folks.Person[1] + "is a " + folks.Job[1]);
                 Console.ReadKey();
             }
         }
    
         public class peoplejobs
         {
             List <string> person = new List<string>();
             List <string> job = new List<string>();
    
             public string Person
             {
                set { person.Add(value); }
                 get { return person[value]; }
             }
    
             public string Job
             {
                set {job.Add(value);}
                 get { return job[value]; }
             }
         }
    }
    The get part won't work (the compiler complains that value isn't valid).

    Is there a way using a get that I can return either person[value] or job[value]?

    I've tried other methods to get this to work, but nothing seems to be happy.

    Thanks

    Paul
    Last edited by Paul Johnson; Dec 16 '11, 01:31 PM. Reason: missing a word
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    No, unfortunately you can't do this. If you just had one list you could use an index like so...

    Code:
    public class TestClass
    {
      private List<string> m_list = new List<string>();
    
      public this[int index]
      {
        get { return m_list[index]; }
        set { m_list[index] = value; }
      }
    
      public void Add(string item)
      {
        m_list.Add(item);
      }
    }
    Then you would use it like...

    Code:
    TestClass tc = new TestClass();
    tc.Add("str");
    Console.WriteLine(tc[0]);
    ... But with your case you want to have to. If you want more than one, it's probably better to just provide access to the entire list in read only form.

    Code:
    public class AnotherClass
    {
      private List<string> m_list = new List<string>();
    
      public List<string> List
      {
        get { return m_list; }
      }
    }
    Code:
    AnotherClass ac = new AnotherClass();
    ac.List.Add("str");
    Console.WriteLine(ac.List[0]);
    This will allow the programmer to access the entire list, but they won't be able to set it to null. They can still clear it though, but at the same time with what you had before items could be blanked out as well. Note that read-only access in this case applies to the reference itself, so the programmer is still free to do anything they want with the list, such as adding items to it.

    Comment

    Working...