0

I am desperately trying to adapt an API from which I want to fetch a specific value out of an array and I can't seem capable of doing so.

Specifically, I am trying to fetch a custom field name and value out of the API where the name is Controller and the value is whatever the user gave it a value of (its a string value).

I order to get to this, I need to use what is called an IGame interface, which has several properties and methods within it. All of the properties are used with game proceeded with the name of the property. For example, game.title or game.Platform.

So, doing this.Controller.Text = game.Title; outputs the game title to the UI. So far, so good.

My problem comes when using the methods, whering the custom fields lie. I have to use GetAllCustomFields which has the syntax of ICustomField[] GetAllCustomFields(). The return value is ICustomField[] whose syntax in turn is public interface ICustomField with the properties of GameId, Name, Value.

Unfortunately, the API gives me no further information on actual usage so I'm left to try to figure it out but to no avail.

Here what i have so far:

    public void OnSelectionChanged(FilterType filterType, string filterValue, IPlatform platform, IPlatformCategory category, IPlaylist playlist, IGame game)
    {
        if (game == null)
        {
            this.Controller.Text = "HellO!";
        }
        else
        {
            foreach (string field in game.GetAllCustomFields(ICustomField[Name]))
            {
                this.Controller.Text = "The " + Name + " is " + Value;
            }
        }
    }

XAML

<Grid>
    <TextBox x:Name="Controller" />
</Grid>

Can someone help me restructure the foreach so it actually works?

3
  • 1
    It sounds like you want foreach (ICustomField field in game.GetAllCustomFields()) in your for loop, and then this.Controller.Text = "The " + field.Name + " is " + field.Value; Commented Nov 11, 2017 at 21:07
  • I knew I was close! thank you! Commented Nov 11, 2017 at 21:17
  • One thing though, there can be several different custom fields so how do I ensure I'm only displaying the field with the name Controller? If users have mutiple different field names, I dont want to output everything. Commented Nov 11, 2017 at 21:23

1 Answer 1

1
var controllerFields = game.GetAllCustomFields().Where(f => f.Name == "Controller");

foreach (var field in controllerFields)
{
    this.Controller.Text = "The " + field.Name + " is " + field.Value;
}

Or if it is just one Controller-field:

Controller.Text = game.GetAllCustomFields()
                      .SingleOrDefault(f => f.Name == "Controller")
                      ?.Value ?? "No controller here";

?.Value will return the value of the field if it was found. If not, that question mark will prevent to access .Value because it would lead to a NullReferenceException. By adding ?? "No controller here", you give a fallback which is returned if no Controller field was found. That opererator (?.) is called the null coalescing operator, if you don't know it yet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.