Reputation: 17
I'm trying to pass values stored in a class to a program, but I am not sure how! I felt I was so close to completing this program but when running it it only displays what it is written in the actual program and it doesn't display options to store values on the class variable. If anyone could help me guide on what am I doing wrong it would be greatly appreciated.
Here is the class I have written.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Pet
{
public string Name;
public string Type;
public double Age;
public string setName()
{
Console.WriteLine("Please write your pet's name");
Name = Console.ReadLine();
return Name;
}
public string setType()
{
Console.WriteLine("What type of animal is your pet?");
Type = Console.ReadLine();
return Type;
}
public double getAge()
{
Console.WriteLine("Input your pet's age");
while(!double.TryParse(Console.ReadLine(), out Age))
Console.WriteLine("Please enter a valid number");
return Age;
}
}
}
And here is the actual program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Pet mypet = new Pet();
mypet.Name = "";
mypet.Type = "";
mypet.Age = 0;
Console.WriteLine("The name of your pet is:" + mypet.Name);
Console.WriteLine("The type of animal your pet is:" + mypet.Type);
Console.WriteLine("The age of your pet is:" + mypet.Age);
}
}
}
Upvotes: 0
Views: 174
Reputation: 11252
Your class definition should probably look like this:
class Pet
{
public string Name { get; set; }
public string Type { get; set; }
public DateTime DateOfBirth { get; set; }
}
You want to use auto-implemented properties instead of public fields. You also do not want to put input/output logic into your data class! That's bad object oriented design.
You should also not store age as the number of years. Age should be represented by date of birth so that you can always calculate the age. That way if you store your pet's information in a file and load it one year later your program will know your pet is one year older.
var myPet = new Pet();
Console.WriteLine("Please write your pet's name");
myPet.Name = Console.ReadLine();
Console.WriteLine("The type of animal your pet is:");
myPet.Type = Console.ReadLine();
DateTime dateOfBirth;
string line;
do
{
Console.WriteLine("The date of birth of your pet:");
line = Console.ReadLine();
} while(DateTime.TryParse(line, out dateOfBirth);
myPet.DateOfBirth = dateOfBirth;
Console.WriteLine("The name of your pet is:", myPet.Name);
Console.WriteLine("The type of animal your pet is: ", myPet.Type);
Console.WriteLine("The age of your pet is:", GetAge(myPet.DateOfBirth));
You could implement the GetAge function as follows:
public static int GetAge(DateTime birthDate)
{
DateTime n = DateTime.Now; // To avoid a race condition around midnight
int age = n.Year - birthDate.Year;
if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
age--;
return age;
}
Upvotes: 1
Reputation: 4960
You haven't implemented properties correctly. See http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
Upvotes: 0
Reputation: 139
You don't have calls to mypet.setName(), mypet.setType() nor mypet.getAge() before your console writes.
Upvotes: 0
Reputation: 11158
I think that, following the way your code is organised, your program should look like this:
class Program
{
static void Main(string[] args)
{
Pet mypet = new Pet();
mypet.setName()
mypet.setType();
mypet.getAge();
Console.WriteLine("The name of your pet is:" + mypet.Name);
Console.WriteLine("The type of animal your pet is:" + mypet.Type);
Console.WriteLine("The age of your pet is:" + mypet.Age);
}
}
By the way, the it could be more consistent naming your methods the following way:
PromptName();
PromptType();
PromptAge();
As that is what they actually do. Notice the UpperCamelCase notation (http://en.wikipedia.org/wiki/CamelCase), which is the standard when writing C# code.
Upvotes: 4
Reputation: 19447
You are not requesting for input, simply displaying the output.
Change your code to something like
static void Main(string[] args)
{
Pet mypet = new Pet();
mypet.setName();
mypet.setType();
mypet.getAge();
Console.WriteLine("The name of your pet is:" + mypet.Name);
Console.WriteLine("The type of animal your pet is:" + mypet.Type);
Console.WriteLine("The age of your pet is:" + mypet.Age);
}
Upvotes: 1