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"));
}
}

Recommended Answers

All 4 Replies

Okay, what's your exact problem? I've already been kind enough not only to refrain from flaming you for posting in the wrong forum, but also to give you a viable solution to your last problem. I don't have a great deal of patience, so I highly recommend moving your thread to the .NET C# forum and asking a smart question. When I lose my patience, it's not pretty.

This I have seen. I only wanted more of a hint to what you so so kindly gave me earlier. Like I said, I tried to compile the code that you suggested and I received an error. You suggested that I get a new compiler, but the compliler that I am using came with the software for the C# class. Anyways, I really do appreciate you not yelling at me, but I am truly lost at this point. Sorry if I've upset you!

>but the compliler that I am using came with the software for the C# class.
Then remove the call to ReadKey and everything will work. That was the only 2.0 language feature that I used. There's a lot to be said for experimentation, you know. Check my blogs for a thorough rant on the topic.

Thank you so much Narue! You are my HERO!

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.