473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Returning a string from a List, but using an accessor

Paul Johnson
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace IndexedClass
  6. {
  7.      class Program
  8.      {
  9.          static void Main() // remove the string args[] - unless you're passing stuff in it's pointless!
  10.          {
  11.              peoplejobs folks = new peoplejobs();
  12.              folks.Person = "Jones";
  13.              folks.Job = "waiter";
  14.              folks.Person = "Smith";
  15.              folks.Job = "fitter";
  16.              Console.WriteLine(folks.Person[0] + "is a " + folks.Job[0]);
  17.              Console.WriteLine(folks.Person[1] + "is a " + folks.Job[1]);
  18.              Console.ReadKey();
  19.          }
  20.      }
  21.  
  22.      public class peoplejobs
  23.      {
  24.          List <string> person = new List<string>();
  25.          List <string> job = new List<string>();
  26.  
  27.          public string Person
  28.          {
  29.             set { person.Add(value); }
  30.              get { return person[value]; }
  31.          }
  32.  
  33.          public string Job
  34.          {
  35.             set {job.Add(value);}
  36.              get { return job[value]; }
  37.          }
  38.      }
  39. }
  40.  
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
Dec 16 '11 #1

✓ answered by GaryTexmo

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

Expand|Select|Wrap|Line Numbers
  1. public class TestClass
  2. {
  3.   private List<string> m_list = new List<string>();
  4.  
  5.   public this[int index]
  6.   {
  7.     get { return m_list[index]; }
  8.     set { m_list[index] = value; }
  9.   }
  10.  
  11.   public void Add(string item)
  12.   {
  13.     m_list.Add(item);
  14.   }
  15. }
Then you would use it like...

Expand|Select|Wrap|Line Numbers
  1. TestClass tc = new TestClass();
  2. tc.Add("str");
  3. 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.

Expand|Select|Wrap|Line Numbers
  1. public class AnotherClass
  2. {
  3.   private List<string> m_list = new List<string>();
  4.  
  5.   public List<string> List
  6.   {
  7.     get { return m_list; }
  8.   }
  9. }
Expand|Select|Wrap|Line Numbers
  1. AnotherClass ac = new AnotherClass();
  2. ac.List.Add("str");
  3. 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.

1 2387
GaryTexmo
1,501 Expert 1GB
No, unfortunately you can't do this. If you just had one list you could use an index like so...

Expand|Select|Wrap|Line Numbers
  1. public class TestClass
  2. {
  3.   private List<string> m_list = new List<string>();
  4.  
  5.   public this[int index]
  6.   {
  7.     get { return m_list[index]; }
  8.     set { m_list[index] = value; }
  9.   }
  10.  
  11.   public void Add(string item)
  12.   {
  13.     m_list.Add(item);
  14.   }
  15. }
Then you would use it like...

Expand|Select|Wrap|Line Numbers
  1. TestClass tc = new TestClass();
  2. tc.Add("str");
  3. 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.

Expand|Select|Wrap|Line Numbers
  1. public class AnotherClass
  2. {
  3.   private List<string> m_list = new List<string>();
  4.  
  5.   public List<string> List
  6.   {
  7.     get { return m_list; }
  8.   }
  9. }
Expand|Select|Wrap|Line Numbers
  1. AnotherClass ac = new AnotherClass();
  2. ac.List.Add("str");
  3. 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.
Dec 16 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: shama.bell | last post by:
Hello, I am getting the following error when returning a list: return tbl "IndexError: each subindex must be either a slice, an integer, Ellipsis, or NewAxis" What does it mean?
6
by: ASPfool | last post by:
Hello everyone, Please help me before I throw my computer out of the window: I'm working on a web application in classic ASP (vbscript), which is making calls to some webservices. The calls...
13
by: krbyxtrm | last post by:
hi, i have problem implemting a string parser that parser comman delimited string: "str1,str2,str3" INTO: 1. str1 2. str2 3. str3 *also strings are of any string (no specific string/keyword)
2
by: A.M | last post by:
Hi, Is there any online resource that gives examples about advanced python string, list and map operations? Today I saw this and I found that I have to work more on mentioned topics:
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
5
by: admin | last post by:
I have a class that has a function that should query a database and return a list of usernames as well as their id. What type should I use as a return type, that can hold data such as: user1, 1...
12
by: python101 | last post by:
I have a string list items = Assume that I don't know the order (index) of these items. I would like to remove the second 'B' out of the list without sorting or changing the the order of...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.