Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to list each pet in the pet class on my form for my C# .NET project.

ID: 3909836 • Letter: I

Question

I am trying to list each pet in the pet class on my form for my C# .NET project. I am getting an error that says pet is a type which is not valid in the given contex.

This is the peice of code causing the error:

foreach (var item in Pet)
{
Console.WriteLine("list item " + item.arrivalDate );
petList.Text = "list item " + item.arrivalDate;

}

Pet is the super class -> cat and dog inherit from pet -> chiwawa and husky inherit from dog/ siamese and tabby inherit from cat

This line of code shows that I have 4 in the pet class: petCount.Text = "Pets Available : " + Pet.petCount;

Not sure how else besides a for loop I would be able to show the arrival date of each pet?

Here is my form:

______________________________________________________

namespace FinalProject
{
public partial class Form1 : Form
{
public Form1()
{
  
InitializeComponent();
  

}

private void Form1_Load(object sender, EventArgs e)
{
Tabby sunshine = new Tabby(22222222222222222, new DateTime(2016, 2, 24), false);

Chiwawa tony = new Chiwawa(33333333333333333, new DateTime(2016, 2, 24), false);

Siamese felix = new Siamese(44444444444444444, new DateTime(2016, 3, 11), false);

Husky fluffs = new Husky(55555555555555555, new DateTime(2016, 2, 24), false);
  
foreach (var item in Pet)
{
Console.WriteLine("list item " + item.arrivalDate );
petList.Text = "list item " + item.arrivalDate;

}
  
petCount.Text = "Pets Available : " + Pet.petCount;
  
}
}
}

_______________________________________________

Pet.cs

_______________________________________________

namespace FinalProject
{
public abstract class Pet
{

#region Fields
protected long chip;
protected DateTime ArrivalDate;

protected bool AdoptedStatus;
public static int petCount = 0;


#endregion End of Fields

#region Constructors
public Pet()
{
chip = 0;
AdoptedStatus = false;
petCount++;
}
public Pet(long chip, DateTime arrivalDate, Boolean adoptedStatus)
{
this.chip = chip;
ArrivalDate = arrivalDate;
AdoptedStatus = adoptedStatus;
petCount++;
}
#endregion End of Constructors

#region Properties

public int PetCount
{
get
{
return petCount;
}
}
public long Chip
{
get
{
return chip;
}
set
{
if (value > 0)
chip = value;
else
chip = 0;
}
}

public DateTime arrivalDate { get; set; }

public Boolean adoptedStatus { get; set; }

#endregion End Properties

#region Methods

public bool UpdateStatus() => adoptedStatus = true;
public int UpdateInventory() => petCount = petCount - 1;
public abstract void Noise();

// public override string ToString()
// {
// return $"{Model} MRSP:${Mrsp} Vin:{Vin} Delivered:{DeliveryDate} Sold: {soldStatus} ";
// }
#endregion End of Methods
}
}

____________________________________________________

Dog.cs

namespace FinalProject
{
public class Dog : Pet
{
public Dog()
{
Console.WriteLine(" Dog:");
}

public Dog(long chip, DateTime arrivalDate, Boolean adoptedStatus)
: base(chip, arrivalDate, adoptedStatus)
{
Console.WriteLine("Dog:");
}
public override void Noise()
{
//Console.WriteLine("Dog");
}
}
}

_________________________________________

Husky.cs

namespace FinalProject
{
public class Husky : Dog
{
public Husky()
{
Console.WriteLine(" I am a Husky");
}

public Husky(long chip, DateTime arrivalDate, Boolean adoptedStatus)
: base(chip, arrivalDate, adoptedStatus)
{
Console.WriteLine("I am a Husky Chip:{Chip} Arrived:{ArrivalDate} Adopted: {AdoptedStatus} ");
}
public override void Noise()
{

}
}
}

Explanation / Answer

Foreach statement executes a single statement or multiple statement for evey element in an instance of type that implements collection.

Here you are using:

foreach (var item in Pet)

this is a Pet class not collecction

You hav to use something like this

foreach (var item in PetList)

where PetList has to be a instance of the list class or collection.

If the above soution is helpful to you in any way please rate it Or if you have any concerns comment me, I will help you through

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote