ok so i am trying to do a little phone book. i have a class that has information on the individual, the main would read from a file of individuals and ask if the user wants to add or remove , and the last one is array of individuals..here is the code for info on the individuals but i want to be able to call a function from there to the array of individuals..how would i do that?

class PersonInfo
    {
        private string first_name = null;
        private string last_name = null;
        private string address = null;
        private string city = null;
        private string state = null;
        private int zipcode = 0;
        private string phone_num = null;

        public PersonInfo()
        {
           
        }

        public string getFirst_Name()
        {
            return first_name;
        }

        public void setFirst_Name(string first_name)
        {
            this.first_name = first_name;
        }

        public string getLast_Name()
        {
            return last_name;
        }

        public void setLast_Name(string last_name)
        {
            this.last_name = last_name;
        }

        public string getAddress()
        {
            return address;
        }

        public void setAddress(string address)
        {
            this.address = address;
        }

        public string getCity()
        {
            return city;
        }

        public void setCity(string city)
        {
            this.city = city;
        }

        public string getState()
        {
            return state;
        }

        public void setState(string state)
        {
            this.state = state;
        }
        public int getZipcode()
        {
            return zipcode;
        }

        public void setZipcode(int zipcode)
        {
            this.zipcode = zipcode;
        }
        public string getPhone_Num()
        {
            return phone_num;
        }

        public void setPhone_Num(string phone_num)
        {
            this.phone_num = phone_num;
        }
    }

this is the code i have for the array of individuals

class Person
    {
        ArrayList page = new ArrayList(); // creates a arraylist
        page.Add();

Dani AI

Generated

The simplest, clearest fix is to separate responsibilities: keep PersonInfo as a plain data model and put collection operations (add/remove/find/load/save) in a separate manager class. That avoids coupling an individual object to the storage that holds many objects. As suggested, use properties for the model. As pointed out, prefer a generic collection for type safety. asked for a conceptual example — below is a minimal, practical pattern to follow.

public class PersonInfo
{
    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 int Zipcode      { get; set; }
    public string Phone     { get; set; }

    public override string ToString() => $"{FirstName} {LastName} ({Phone})";
}
public class PhoneBook
{
    private readonly List<PersonInfo> entries = new List<PersonInfo>();

    public void Add(PersonInfo p) => entries.Add(p);

    public bool RemoveByName(string first, string last)
    {
        var match = entries.FirstOrDefault(e =>
            string.Equals(e.FirstName, first, StringComparison.OrdinalIgnoreCase) &&
            string.Equals(e.LastName, last, StringComparison.OrdinalIgnoreCase));
        if (match == null) return false;
        entries.Remove(match);
        return true;
    }

    public PersonInfo Find(string first, string last) =>
        entries.FirstOrDefault(e => /* same comparison as above */);
}

Usage from Main becomes straightforward: load the file into PersonInfo objects, call phoneBook.Add(...), call phoneBook.RemoveByName(...), etc. If the goal truly is for a PersonInfo instance to cause collection changes, pass a reference to the manager into the method (or raise an event), but prefer keeping model classes free of collection logic. Additional improvements: implement parsing/validation when loading from file, override Equals/GetHashCode or implement IComparable for sorting, and favor List<T> over ArrayList for type safety.

Recommended Answers

All 4 Replies

i want to be able to call a function from there to the array of individuals..how would i do that?

I'm not quite sure what you mean by that. Can you give a brief example? (just conceptually)

> i want to be able to call a function from there to the array of individuals..how would i do that?

Use generic.

using System;
using System.Collections.Generic;
class Program
    {
    static void Main(string[] args)
        {
            List<PersonInfo> a = new List<PersonInfo>();
            a.Add(new PersonInfo());
            a.Add(new PersonInfo());

            a[0].setFirst_Name("Mr.A");
            a[1].setCity("City1");
        }
     }

hey thanks ! that really helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.