Thread: New user - Nuint test prob.....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    21

    New user - Nuint test prob.....

    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have a foreach loop inside your if? Shouldn't you just use the one storedTender that you know the type of?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    21
    Thanks for the reply,

    The foreach is there because there is more than one storedTender inside the repository.Tenders list.

    The repository.Tenders list is an extraction from a dictionary.

    Basically I started of with a foreach, Nunit errored so I added the if / else statement to try and get the foreach loop to distinguish between the different type of tenders.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by scott_ill View Post
    Thanks for the reply,

    The foreach is there because there is more than one storedTender inside the repository.Tenders list.

    The repository.Tenders list is an extraction from a dictionary.

    Basically I started of with a foreach, Nunit errored so I added the if / else statement to try and get the foreach loop to distinguish between the different type of tenders.
    I don't think you've quite got it. As it stands, if there are 10 tenders in your list, you are going to print 100 items. The outer for-loop will run 10 times, and each time one of the inner for loops will print 10 things. You should just print one thing inside your loop, not ten (or a hundred, or whatever).

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    21
    Quote Originally Posted by tabstop View Post
    I don't think you've quite got it. As it stands, if there are 10 tenders in your list, you are going to print 100 items. The outer for-loop will run 10 times, and each time one of the inner for loops will print 10 things. You should just print one thing inside your loop, not ten (or a hundred, or whatever).

    Yes I do understand that, thge problem is there are general params stored within the base object and specialised params at the higher level, which I can't access.

    I've stripped the whole test right down now and I'm still getting the same Nunit error. What would you suggest?

    Thanks.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by scott_ill View Post
    Yes I do understand that, thge problem is there are general params stored within the base object and specialised params at the higher level, which I can't access.

    I've stripped the whole test right down now and I'm still getting the same Nunit error. What would you suggest?

    Thanks.
    Cast storedTender to the appropriate type?

    (Edit to add: I don't know if that's actually necessary in C# to do polymorphism; I would guess not. If you post your current code, we can see what's going on.)
    Last edited by tabstop; 06-19-2009 at 11:12 AM.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Assuming Tender is the base class of the other 2 tender types, and your collection is a collection of the base type then you will need to cast down to the derived classes. You can do this "safely" with "is" or casting with "as" and checking the result for null.

    Although on a second look it looks like your collection is of UnpricedTender, which is why you can take a base class reference to it (since UnpricedTender "is a" Tender), but you cannot implicitally convert it to another derived type.

    If this is the case change your collection to a collection of the base type.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    21
    Thanks for the help guys.

    The following code got the job done:

    Code:
    if (storedTender.Status.Equals("UNPRICED"))
                    {
                        //cast type Tender to UnpricedTender
                        UnpricedTender UnpricedStoredTender = new UnpricedTender();
                        UnpricedStoredTender = (UnpricedTender)storedTender;
    
    .............................
    
    else if (storedTender.Status.Equals("LIVE"))
                    {
                        //cast type Tender to LiveTender
                        LiveTender LiveStoredTender = new LiveTender();
                        LiveStoredTender = (LiveTender)storedTender;  
    
    .................................

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My C++ test is COMING!!...
    By [Z-D] in forum C++ Programming
    Replies: 52
    Last Post: 12-01-2006, 08:02 PM
  2. Need help managing user input
    By JayDiddums10 in forum C Programming
    Replies: 2
    Last Post: 11-19-2006, 05:01 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  5. how to test if user input is an int
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2001, 05:58 PM