Reputation: 77
This code is supposed to output lines which say
trex (2 legged) dinosaur (extinct)
For a list of animals taken from a file. If the animal isn't extinct, it is skipped by the if statement.
Everything works fine, except for the last bit, which should say (extinct).
Instead it said "true" or "false" depending on how I set up the isExtinct property.
Now I've put an if statement into the isExtinct property to output "extinct" but I get an error post compilation
Stacktrace:
Native stacktrace:
mono() [0x8105b4a]
mono() [0x8153274]
mono() [0x806a20b]
[0x777b940c]
mono() [0x8153224]
mono() [0x806a20b]
[0x777b940c]
[0x7744c667]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
[0x7744c66c]
Debug info from gdb:
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Aborted (core dumped)
Code below
class MainClass
{
public static void Main (string[] args)
{
StreamReader input = new StreamReader ("animals.txt");
while (!input.EndOfStream) {
string line = input.ReadLine ();
string[] data = line.Split (',');
if (data [3] == "true") {
Animal myAnimal = new Animal (data[0], Convert.ToInt32(data[1]), data[2], Convert.ToBoolean(data[3]));
Console.WriteLine (myAnimal);
}
}
}
}
}
public class Animal
{
string species;
int legs;
string genus;
bool extinct;
public Animal (string species, int legs, string genus, bool extinct)
{
this.species = species;
this.legs = legs;
this.genus = genus;
this.extinct = extinct;
}
public string Species{ get; set;}
public int Legs{ get; set;}
public string Genus{ get; set;}
public string isExtinct{
get
{
return isExtinct;
}
set
{
if (extinct == true) {
isExtinct = "extinct";
} else {
isExtinct = "";
}
}
}
public override string ToString(){
return String.Format("{0} ({1} legged) {2} ({3})", species, legs, genus, isExtinct);
}
}
}
Upvotes: 0
Views: 60
Reputation: 48425
Quick Fix
Well that isExtinct
property looks like a problem... try this one:
public string isExtinct
{
get { return extinct ? "extinct" : "" }
}
That should fix it, but I would say you probably want to consider a cleanup...
Clean Up
For example, I would argue not having the getter at all. Just do the logic in your ToString
method (as that is likely the only place that will need to use it anyway), then you can make the brackets conditional too. Something like:
public override string ToString()
{
string result = String.Format("{0} ({1} legged) {2}", species, legs, genus);
if(extinct)
result += " (extinct)";
return result;
}
Suggested Code
In addition, your properties Species
, Legs
, and Genus
don't have any use, they are not tied to your fields anyway. I would suggest removing the fields and stick with just the 4 properties:
public string Species{ get; set;}
public int Legs{ get; set;}
public string Genus{ get; set;}
public string IsExtinct { get; set;}
public Animal (string species, int legs, string genus, bool isExtinct)
{
Species = species;
Legs = legs;
Genus = genus;
IsExtinct = isExtinct;
}
public override string ToString()
{
string result = String.Format("{0} ({1} legged) {2}", Species, Legs, Genus);
if(IsExtinct )
result += " (extinct)";
return result;
}
Upvotes: 2
Reputation: 1138
Your property logic was wrong - you don't want two separate writable properties and your syntax incorrect.
Try this :
public string isExtinct {
get
{
if (extinct == true) {
return "extinct";
} else {
return "";
}
}
}
Upvotes: 1