Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Hello. My issue is that the if statement with the contains is not responding. It simply goes to the else statement. Is there a way to fix this code or a different way to do this? Thank you in advance.
using System.Linq;

*I do have this using option. Is this the reason that this way is of finding an item in the Array is not working?
CSS
#region class rooms
class Room
{
    public string Title;
    public string Description;
    public string map_location;
    public Room Close;
    public Room Open;
    public Room Warn;
    public Room Dwarn;
    public Room Swarn;
    public Room Awarn;
    public Items[] roomitems = new Items[10000];
    public Challenge RoomChallange;
    public Challenge RoomChallange2;
    public Challenge RoomChallange3;

    public Room(string title, string description, string maplocation)
    {
        Title = title;
        Description = description;
        map_location = maplocation;
    }
}

*I placed the array in here. I have seen other people with examples of Arrays have put the Array in the Main(string[] args). I tried putting it in the Main(string[] args) but that did nothing to help the situation. This next part is in the Main(string[] args)
Items Orcics_200 = new Items("Orcics_200", "A pile of two hundred Orcics.", 0, .2, 0, 200);
<pre>     
Room administrativeoffice21 = new Room("Administrative Office", "The shops are to Dwarn, the entrance to Orcinium is to the Warn, the inns inside the wall are to the Awarn, and the entrance back into Aldoria is to the Swarn. This is the administrative office. Hundreds of Orcs and adventures are lined behind you. You can feel hope, joy, and tension of these people. The administrative officers are franticly filling in the administrative papers of the new immigrants; as soon as your papers are filled up you may as well be invisible.", "Your location is (2,1)");
<pre>            
administrativeoffice21.roomitems[1] = Orcics_200;

*Whenever I fire up this code I get the else section. Why is this?
C#
case "take two hundred orcics":
    if (currentRoom.roomitems.Contains(Orcics_200))
    {
        Console.WriteLine("You have taken {0}.", Orcics_200.Name);
        playeritems.Add(Orcics_200);
        playerwieght = playerwieght + Orcics_200.Wieght;
        currentRoom.Description += " You have taken the 200 Orcics.";
    }
    else
    {
        Console.WriteLine("The Orcics are not here / you have already taken it.");
        goto fix;
    }
    break;
*Thank you in advance for all of your answers.
Posted
Comments
Ron Beyer 7-Jul-13 21:32pm    
Have you tried putting a breakpoint at the "if" statement and see what the roomitems array actually contains? I have a feeling that you aren't assigning currentRoom to administrativeOffice21 or something.
Sergey Alexandrovich Kryukov 7-Jul-13 22:20pm    
Let me assure you: Contains does work correctly. If it returns false, you can be sure that the element in question is not contained. Check up your logic, execute your code under the debugger...
—SA

1 solution

try following code:

C#
case "take two hundred orcics":
                            //ToUpper() added
    if (currentRoom.roomitems.ToUpper().Contains(Orcics_200.ToUpper()))
    {
        //i add .ToUpper() or you can use .ToLower() 
        //Condition getting false cause of LetterCase.
        Console.WriteLine("You have taken {0}.", Orcics_200.Name);
        playeritems.Add(Orcics_200);
        playerwieght = playerwieght + Orcics_200.Wieght;
        currentRoom.Description += " You have taken the 200 Orcics.";
    }
    else
    {
        Console.WriteLine("The Orcics are not here / you have already taken it.");
        goto fix;
    }
    break;


if any concern let me know.

thanks
 
Share this answer
 
Comments
[no name] 28-Jul-13 4:13am    
Good Answer
MuhammadUSman1 28-Jul-13 23:56pm    
Thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900