hello, i recently asked for help on my practice code and tried to post a response on it but i dont know why its wont let me so i am posting a new question. Somebody helped me with the constructor problem i was having but for some reason we cannot figure out why my set and get commands arent working.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace the_helped
{
class Animal
{
private string name;
private string race;
private double weight;
private double age;
public double _age
{
get
{
return age;
}
set
{
if (value > 0 && value <= 25) age = value;
else
{
age = 1;//for example here i want '1' to be the default number but when i run //the program, the number that shows is '0' and another thing that
Console.WriteLine("Invalid Age!.");//and another thing that is happening is
//is that when the condition in 'if' is not
//true 'invalid age!' does not show with the //default number '1'.
//ex: "please enter dogs age:" 500 (user input)
//Invalid age! Dog's age: 1. the same thing
//happens with weight. i just dont understand why
//its not working.
}
}
}
public double _weight
{
get
{
return weight;
}
set
{
if (value > 0 && value <= 60) weight = value;
else
{
weight = 1;
Console.WriteLine("Invalid Weight!");
}
}
}
public Animal()
{
Console.Write("These are the constructor details : ");
}
public void getdetails()
{
Console.WriteLine("\nName: No Name");
Console.WriteLine("Race: No Race");
Console.WriteLine("Age: " + age);
Console.WriteLine("Weight: " + weight);
}
public static void Main(String[] args)
{
Animal dog = new Animal();
dog.getdetails();
Console.Write("Please enter your dog's name: ");
dog.name = Console.ReadLine();
Console.Write("Please enter your dog's age: ");
dog.age = double.Parse(Console.ReadLine());
Console.Write("Please enter your dog's weight: ");
dog.weight = double.Parse(Console.ReadLine());
Console.Write("Please enter your dog's race: ");
dog.race = Console.ReadLine();
Console.WriteLine(dog.name);
Console.WriteLine(dog.race);
Console.WriteLine(dog.weight);
Console.WriteLine(dog.age);
Console.ReadLine();
}
}
}
Loading
VulpesPosted Oct 3, 2013, 7:37 PM
Try this code for the weight property:
A APosted Oct 3, 2013, 7:14 PM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace the_helped
{
class Animal
{
private string name;
private string race;
private double _weight;
private double _age;
public double age
{
get
{
return _age;
}
set
{
if (_age > 0 && _age <= 60)
{
_age = value;
}
else
{
do
{
Console.WriteLine("Invalid Age!\nDog's age: " + _age);
Console.Write("Please enter dog's age: ");
} while (_age < 0 && age > 25);
}
}
}
public double weight
{
get
{
return _weight;
}
set
{
if (_weight > 0 && _weight <= 60)
{
_weight = value;
}
else
{
do//for some reason my do while is not executing, i am a beginner but
//this seems to be correctly written to me. well obviously
//somethings wrong if it does not do what i want it to do
{
Console.WriteLine("Invalid Weight!\nDog's weight: " + _weight);
Console.Write("Please enter dog's weight: ");
//here i also tried putting '_weight = double.parse.(console.ReadLine());' but
//nothing changed im trying to get information on set and get over the internet
//because i do not have the book yet (i know i need to purchase it but they are too
//expensive for me now) but specific information is a little tough to come by.
} while (_weight < 0 && _weight >= 60);
}
}
}
public Animal()
{
Console.Write("\nThese are the constructor details : ");
}
public void getdetails()
{
_age = 1;
_weight = 1;
Console.WriteLine("\n\nName: No Name");
Console.WriteLine("Race: No Race");
Console.WriteLine("Weight: " + _weight);
Console.WriteLine("Age: " + _age);
Console.WriteLine();
}
public void info()
{
Console.WriteLine("\nDog's name: " + name);
Console.WriteLine("Dog's race: " + race);
Console.WriteLine("Dog's age: " + age);
Console.WriteLine("Dog's weight: " + weight);
Console.ReadLine();
}
public static void Main(String[] args)
{
Animal dog = new Animal();
dog.getdetails();
Console.Write("Please enter your dog's name: ");
dog.name = Console.ReadLine();
Console.Write("Please enter your dog's race: ");
dog.race = Console.ReadLine();
Console.Write("Please enter your dog's age: ");
dog.age = double.Parse(Console.ReadLine());
Console.Write("Please enter your dog's weight: ");
dog.weight = double.Parse(Console.ReadLine());
dog.info();
}
}
}
VulpesPosted Sep 30, 2013, 3:47 PM
If you make the highlighted changes in the Main() method then all should be well:
Incidentally, it's much more usual to precede the field names with underscores rather than the property names though this is not essential.