开发者

Comparing strings with objects in c#/XNA

I have an XML file which is split up using pipes "|". I have some code in a question class that splits up the XML files "Items" as so..

 List<string> questionComponents = newquestionString.Split('|').ToList<string>();

        questionString = questionComponents[0];
        apple = questionComponents[1];
        pear = questionComponents[2];
        orange = qu开发者_运维问答estionComponents[3];

        correctAnswer = Int32.Parse(questionComponents[4]);

I want to compare these components with objects which are instantiated in my Game1 class (three fruit - apple, pear, orange). So how do I do this?

A friend helped me get this far. I have no idea how to do this, and after searching google with no luck I've resulted in asking here to you lovely people.

Thanks in advance :D

EDIT: To clear things up...

I have three objects called apple, pear and orange and I want to associate these objects with the strings which are shown in the XML file for each component of the strings. The question string displays a question, [1] answer 1, [2] answer 2, [3] answer [3].

And then I need a way to compare the answer to the object that is eaten in the game..


Assuming you have some kind of Orange object, some kind of Pear object, and some kind of Apple object, in each class override the ToString method.

If you have some generic Fruit or Answer object, consider passing a string in the constructor and returning that string in the ToString method.

EDIT: Since you've now clarified, I would go with Jonathan's idea of having a Name or Answer property; then you can do:

if(object.Answer == questionComponent)
//do stuff

And ToString does not turn the object into a string. It simply returns a user-defined (if you choose to override) string for the object - for the Ints it is "42", and for bools it is "true" or "false". No conversion occurs.


Based on your edit, it sounds like all you're trying to do is be able to look up some concrete object based on a string that you get from a data file? Couldn't you use a Dictionary for this, as in:

Fruit apple = new Apple();
Fruit orange = new Orange();

Dictionary<string,Fruit> map = new Dictionary<string,Fruit>();
map["apple"] = apple;
map["orange"] = orange;

and then later you can get the user's answer/input:

string input = ...
Fruit result;
if(map.TryGetValue(input, out result)) {
  // `fruit` now holds the fruit object the user selected.
} else {
  // User input did not correspond to a known fruit.
}

But I'm still not convinced I'm understanding your question properly.


It looks like the range of possible strings is limited, so why not just use identifiers in the form of Enums, which you can parse from the string, and set that identifier into the answers and the fruits?

public enum AnswerType
{
  Apple,
  Pear,
  Banana
}

Store the answers in a dictionary mapping ids to answers (casting enum to int for the key), plus the correct answer's id, and now you can know which answer was picked each time, checking the eaten fruit's id, and if the answer was right.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜