Hi all,
I just started C# a couple of days ago and I've not done any programming for about 2 years (and that was java).
Was wondering if you could give me a hand with some code thats falling over in Nunit.....
Basically I've got an object called "Tender" and 2 specialisations of it called "UnpricedTender" and "LiveTender", an instance of each of these is stored in a library. I'm trying to loop through the instances and extract all parameters, but for some reason it's trying top cast the instance of "LiveTender" to "UnpricedTender"
Code:
//Check contents of params for each tender in the dictionary
foreach (Tender storedTender in repository.Tenders)
{
//General params
Console.Out.WriteLine("Tender ID = " + storedTender.TenderId);
Console.Out.WriteLine("Status = " + storedTender.Status);
Console.Out.WriteLine("Client = " + storedTender.Client);
Console.Out.WriteLine("Received date = " + storedTender.Received);
Console.Out.WriteLine("Project = " + storedTender.Project);
Console.Out.WriteLine("Contact Name= " + storedTender.ContactName);
Console.Out.WriteLine("Contact Number = " + storedTender.ContactNumber);
if (storedTender.Status.Equals("UNPRICED") == true)
{
foreach (UnpricedTender storedUnpricedTender in repository.Tenders)
{
//UnpricedTender specific params
Console.Out.WriteLine("Reason Unpriced = " + storedUnpricedTender.ReasonUnpriced);
}
}
else if (storedTender.Status.Equals("LIVE") == true)
{
foreach (LiveTender storedLiveTender in repository.Tenders)
{
//LiveTender specific params
Console.Out.WriteLine("Main Contract Status = " + storedLiveTender.MainContractStatus);
Console.Out.WriteLine("Contract Value = £" + storedLiveTender.ContractValue);
Console.Out.WriteLine("Return Date = " + storedLiveTender.ReturnDate);
}
}
else return;
}
NB: I only put the if loop in to try and catch this problem.
Nunit error: tenderLib.TenderTests.AddTenderToRepositoryTest:
System.InvalidCastException : Unable to cast object of type 'tenderLib.UnpricedTender' to type 'tenderLib.LiveTender'.
hope this makes sense