I am trying to teach myself C# and I have run into a problem with one of the chapter assignments. The problem is as follows:

Create a class named City. Objects will be instantiated from this class. Include fields that hold the City's name, its state, and its population. Include a constructor that accepts arguments to assign to all three fields. Also include methods to get and set each field. Save the class as City.cs.

Create a class named MovingHistory. Instantiate two City objects---myCity and myFriendsCity. When you declare each object, provide constructor parameters that set appropriate City values for the birthplaces of you and your friend. Display the City objects. Then use the set methods to change each City object and reflect a City to which you relocated later in life. Display the City objects again. Save the class as MovingHistory.cs.

After 3 days, this is what I have come up with. Can someone help me?

using System;
using CityNamespace;
public class City
{
    private string name;
    private int population;
    private string state;

    public City(string city, int pop, string st)
{
    public string GetName()
    {
        return city;
    }
    public int GetPopulation()
    {
        return population;
    }
    public string GetState()
    {
        return State;
    }
    public void SetName(string city)
    {
        stringCity = city;
    }
    public void SetPopulation(int pop)
    {
        popNumber = population;
    }
    public void SetState(string st)
    {
        stringState = state;
    }
}
using System;
using CityNamespace;
public class MovingHistory
{
    public static void Main()
    {   
        CitymyCity = new City("Stafford",92000,"VA");
        CtiymyFriendsCity = new City("Athens",100000,"GA");
        Console.WriteLine("I was born in {0},{1}.  Population:  {2}",
            myCity.GetName(),mycity.GetState(),
            myCity.GetPopulation().ToString("n0"));
        Console.WriteLine("My friend was born in {0},{1}.  Population:  {2}",
            myFriendsCity.GetName(),myfriendsCity.GetState(),
            myFriendsCity.GetPopulation().ToString("n0"));
            myCity.SetName("Stafford");
            myCity.SetPopulation(92000");
            myCity.SetState("VA")'
            myFriendsCity.SetName("Athens");
            myFriendsCity.SetPopulation(100000);
            myFriendsCity.SetState("GA");
        Console.WriteLine("Now I live in {0},{1}.  Population:  {2}",
            myCity.GetName(),myCity.GetState(),
            myCity.GetPopulation().ToString("n0"));
        Console.WriteLine("Now my friend lives in {0},{1}.  Population:  {2}",
            myFriendsCity.GetName(),myFriendsCity.GetState(),
            myFriendsCity.GetPopulation().ToString("n0"));
    }
}

Edited 3 Years Ago by pyTony: fixed formating

Keeping it simple:

using System;

class City {
  private string name, state;
  private long population;

  public City(string name, string state, long population)
  {
    this.name = name;
    this.state = state;
    this.population = population;
  }

  public void relocate(string name, string state, long population)
  {
    this.name = name;
    this.state = state;
    this.population = population;
  }

  public string getString()
  {
    return name + ", " + state + " Population: " + population;
  }
}

class main {
  public static void Main()
  {
    City myCity = new City("Stafford", "VA", 92000);

    Console.WriteLine("I was born in " + myCity.getString());
    myCity.relocate("Athens", "GA", 100000);
    Console.WriteLine("Now I live in " + myCity.getString());

    Console.ReadKey(); // Pause
  }
}

Hello NaRue,

When I attempted to compile the code, I received the following error message:

MonvingHistory.cs(36,5): error CS0117" 'System.Console' does not contain a definition for 'ReadKey'

I hope that is enough information and thank you for taking time to help the "clueless"!

>'System.Console' does not contain a definition for 'ReadKey'
Get a newer compiler.

By the way, this is the C++ forum, not the C# forum.

This article has been dead for over six months. Start a new discussion instead.