3

I have the following code:

public class settings
{
    public appConfig conf = new appConfig();

    public void getUserID(int listIndex, int newID)
    {
        conf.users[listIndex].ID = newID;
    }

    private class appConfig {
        public List<user> users;

        public appConfig()
        {
            users = new List<user>();
        }
    }
}

public class user {
    public int ID;
}

So with that code I use

var set = new settings();
set.setUserID(0, 20);

How can I change the code so I can access it like this:

var set = new settings();
set.userPropertys(listIndex).ID = newID;

How should I declare the userPropertys ? Is it a method or a new class or what?

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Archangels
  • 87
  • 9
  • 4
    Please provide a [mcve] - the code you've provided at the moment wouldn't compile. I'd also strongly recommend that you follow .NET naming conventions, even when just providing small sample code. (I'd also suggest not exposing public fields...) – Jon Skeet Mar 09 '17 at 07:38
  • is `userPropertys` supposed to be a method that returns a listitem at the position `listIndex` ? – Mong Zhu Mar 09 '17 at 07:41

2 Answers2

2

If I understand correctly, instead of having to call the setUserID() method, you want a way to retrieve the user object and modify its property directly. There are at least two obvious ways to do this (misspelled names in code retained for clarity):

public class settings {
    public config conf = new config();
    public void setUserID(int listIndex, int newID) {
        conf.users[listIndex].ID = newID;
    }

    public user userPropertys(int index) { return conf.users[index]; }

    private class config {
        public List<user> users;
    }
}

Used like this:

set.userPropertys(listIndex).ID = newID;

I.e. exactly as you show it in your question.

or…

public class settings {
    public config conf = new config();
    public void setUserID(int listIndex, int newID) {
        conf.users[listIndex].ID = newID;
    }

    public List<user> userPropertys { return conf.users; }

    private class config {
        public List<user> users;
    }
}

Used like this:

set.userPropertys[listIndex].ID = newID;

I.e. used with the square-bracket indexer syntax.

Note that the only "repair" I did to your original code was add the config type to the conf field declaration. You don't show any actual initialization of the config.users field, which of course you'd need if you expect to access the List<user> object. And I agree with Jon's recommendations regarding naming conventions and exposing (or rather, not exposing) fields as public members.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Now what I don't see is how to access those propertys if I don't put them like public ? – Archangels Mar 09 '17 at 08:01
  • If I had a nickel for every time I was too close to a problem to see the obvious, well... you get the idea. :) – Peter Duniho Mar 09 '17 at 08:01
  • _"I don't see is how to access those propertys if I don't put them like public"_ -- not sure what you mean. Accessibility modifiers like `public`, `protected`, etc. are there to control access to members. If you want outside code to have access, using `public` is fine. Why don't you want to use `public`? – Peter Duniho Mar 09 '17 at 08:02
  • I think I don't understand this: _And I agree with Jon's recommendations regarding naming conventions and exposing (or rather, not exposing) fields as public members._ where is public wrong set ? – Archangels Mar 09 '17 at 08:06
  • It's not so much that _`public`_ itself is wrong, but that _fields_ should not be exposed as `public` members, generally. The main exception being a `readonly` field, but even then only occasionally. See e.g. http://stackoverflow.com/questions/1277572/should-i-use-public-properties-and-private-fields-or-public-fields-for-data, http://stackoverflow.com/questions/6989973/when-does-it-make-sense-to-use-a-public-field, and http://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties – Peter Duniho Mar 09 '17 at 08:28
  • oh now I understand what you mean, I now set them as get; set; Thank You – Archangels Mar 09 '17 at 08:44
2

Yes you can do like this if userPropertys is a method that returns an object of type user. if so the signature will be like this:

public user userPropertys(int listIndex)
{
   return conf.users[listIndex];
}

And the usage will be like the following:

int listIndex = 0;
int newID = 12;
var set = new settings();
set.userPropertys(listIndex).ID = newID ;

Could you please verify this suggestion with the help of this working Example

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88