Priya Singh

Priya Singh

  • NA
  • 1
  • 995

I want to create 3 methods, 2 methods to initialize arrays .

Mar 9 2015 2:15 AM
I want to create 3 methods, 2 methods to initialize arrays and 3rd method to loop through them. then I want to display the arrays by invoking the 3rd method in main(). Please guide using the below example created by me. It might have few bugs. thanks
 
 
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.display(Carnivore, ZooMammals);
}
public void Mammals(ZooMammals[] category)
{
category = new ZooMammals[4];
category[1] = new ZooMammals() {Category="Carnivorous", Attribute = new Attributes[2] };
category[2] = new ZooMammals() { Category = "Carnivorous", Attribute = new Attributes[1] };
category[3] = new ZooMammals() { Category = "Carnivorous", Attribute = new Attributes[3] };
}
public static void Carnivore(Attributes[] Carn)
{
Carn = new Attributes[3];
Carn[0] = new Attributes() { Sex ="M", colour = "yellow", Name="Lion" };
Carn[1] = new Attributes() { Sex ="F", colour = "yellow", Name="Cat" };
Carn[2] = new Attributes() { Sex ="M", colour = "yellow", Name="Tiger"};
}
public static void display(Attributes[] Carn,ZooMammals[] category)
{
foreach (var a in category)
{
if (a == null) continue;
Console.WriteLine("{0},{1},{2}", a.Category, a.Attribute);
}
foreach (var x in category.Carn)
{
if (x == null) continue;
Console.WriteLine("{0},{1},{2}", x.Sex, a.colour, x.Name);
Console.WriteLine(category[1].Carn[0]);
Console.WriteLine(category[1].Carn[1]);
Console.WriteLine(category[1].Carn[2]);
Console.WriteLine(category[2].Carn[0]);
Console.WriteLine(category[2].Carn[1]);
}
Console.ReadLine();
}
}
public class Attributes
{
public string Sex;
public string colour;
public string Name;
}
public class ZooMammals
{
public string Category;
public Attributes[] Attribute;
}
}

Answers (1)