Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9224523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:20:35+00:00 2026-06-18T04:20:35+00:00

Say I have a class like this: class public Person { public string firstName;

  • 0

Say I have a class like this:

class public Person
{
    public string firstName;
    public string lastName;
    public string address;
    public string city;
    public string state;
    public string zip;

    public Person(string firstName, string lastName)
    {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

And let’s further say I create a List of type Person like this:

List<Person> pList = new List<Person>;
pList.Add(new Person("Joe", "Smith");

Now, I want to set the address, city, state, and zip for Joe Smith, but I have already added the object to the list. So, how do I set these member variables, after the object has been added to the list?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:20:37+00:00Added an answer on June 18, 2026 at 4:20 am

    You can get the first item of the list like so:

    Person p = pList[0]; or Person p = pList.First();

    Then you can modify it as you wish:

    p.firstName = "Jesse";

    Also, I would recommend using automatic properties:

    class public Person
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
    
        public Person(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
    

    You’ll get the same result, but the day that you’ll want to verify the input or change the way that you set items, it will be much simpler:

    class public Person
    {
        private const int ZIP_CODE_LENGTH = 6;
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        private string zip_ = null;
        public string zip 
        { 
            get { return zip_; } 
            set
            {
                if (value.Length != ZIP_CODE_LENGTH ) throw new Exception("Invalid zip code.");
                zip_ = value;
            }
        }
    
        public Person(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
    

    Quite possibly not the best decision to just crash when you set a property here, but you get the general idea of being able to quickly change how an object is set, without having to call a SetZipCode(...); function everywhere. Here is all the magic of encapsulation an OOP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say we have something like this public class Person { public string Name
Let's say I have a class like this: public class Person { private String
Let's say I have a class like this: class Person { public string name;
So let's say you have something like this: Class Person: height = 6' 0
Lets say I have a set of model classes like this: public class Person
Let's say I have a class like this : public class MyClass { public
Let's say I have the following two classes: public class Person { public string
Let's say I have a class Person: public class Person { public string Name
Lets say I have class definition like this (fairly simple): class Person { public
lets say I have my owndefined datatype like this: public class InformationType { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.