0

this is my first time working with UWP and Xaml, and I am trying to create a memory matching game.

Here is what I have so far:

private void Start_Click(object sender, RoutedEventArgs e)
    {
        btnStart.Opacity = 0;
        btnStart.IsEnabled = false;

        CardAssign();
    }

    public void CardAssign()
    {
        List<string> cards = new List<string>
        {
            "ms-appx:///Assets/CardA.png", "ms-appx:///Assets/CardA.png",
            "ms-appx:///Assets/CardB.png", "ms-appx:///Assets/CardB.png",
            "ms-appx:///Assets/CardC.png", "ms-appx:///Assets/CardC.png",
            "ms-appx:///Assets/CardD.png", "ms-appx:///Assets/CardD.png",
            "ms-appx:///Assets/CardE.png", "ms-appx:///Assets/CardE.png",
            "ms-appx:///Assets/CardF.png", "ms-appx:///Assets/CardF.png",
            "ms-appx:///Assets/CardG.png", "ms-appx:///Assets/CardG.png",
            "ms-appx:///Assets/CardH.png", "ms-appx:///Assets/CardH.png"
        };

        var shuffledcards = cards.OrderBy(a => Guid.NewGuid()).ToList();

        Card1Img.Source = new BitmapImage(new Uri(shuffledcards[0]));
        Card2Img.Source = new BitmapImage(new Uri(shuffledcards[1]));
        Card3Img.Source = new BitmapImage(new Uri(shuffledcards[2]));
        Card4Img.Source = new BitmapImage(new Uri(shuffledcards[3]));
        Card5Img.Source = new BitmapImage(new Uri(shuffledcards[4]));
        Card6Img.Source = new BitmapImage(new Uri(shuffledcards[5]));
        Card7Img.Source = new BitmapImage(new Uri(shuffledcards[6]));
        Card8Img.Source = new BitmapImage(new Uri(shuffledcards[7]));
        Card9Img.Source = new BitmapImage(new Uri(shuffledcards[8]));
        Card10Img.Source = new BitmapImage(new Uri(shuffledcards[9]));
        Card11Img.Source = new BitmapImage(new Uri(shuffledcards[10]));
        Card12Img.Source = new BitmapImage(new Uri(shuffledcards[11]));
        Card13Img.Source = new BitmapImage(new Uri(shuffledcards[12]));
        Card14Img.Source = new BitmapImage(new Uri(shuffledcards[13]));
        Card15Img.Source = new BitmapImage(new Uri(shuffledcards[14]));
        Card16Img.Source = new BitmapImage(new Uri(shuffledcards[15]));

        Countdown();
    }

    private async void Countdown()
    {
        await Task.Delay(7000);

        Hide();
    }

    private void Hide()
    {
        Card1.IsEnabled = true;
        Card2.IsEnabled = true;
        Card3.IsEnabled = true;
        Card4.IsEnabled = true;
        Card5.IsEnabled = true;
        Card6.IsEnabled = true;
        Card7.IsEnabled = true;
        Card8.IsEnabled = true;
        Card9.IsEnabled = true;
        Card10.IsEnabled = true;
        Card11.IsEnabled = true;
        Card12.IsEnabled = true;
        Card13.IsEnabled = true;
        Card14.IsEnabled = true;
        Card15.IsEnabled = true;
        Card16.IsEnabled = true;

        Card1Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card2Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card3Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card4Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card5Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card6Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card7Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card8Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card9Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card10Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card11Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card12Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card13Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card14Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card15Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card16Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
    }

    private void Card1_Click(object sender, RoutedEventArgs e)
    {
        Card1Img.Source = new BitmapImage(new Uri(shuffledcards[0]));
    }
}

The only issue is that whenever I try to display the shuffled card image again, it says that it doesn't exist in the current context. I have not yet been able to figure out how to fix this! Any help would be appreciated.

EDIT: The error I'm getting is as follows: Error CS0103 The name 'shuffledcards' does not exist in the current context

Light
  • 3
  • 2
  • 1
    Don't describe the error, instead paste it exactly as it appears. It sounds like a runtime error also called an Exception. Paste *all* the exception details. You can get them by calling `.ToString()` on the Exception instance. [edit] your question and add all the details. – Igor Apr 17 '20 at 18:10
  • It's a compilation error because `shuffledcards` is declared in `CardAssign` and OP is trying to use it in `Card1_Click`. The deleted answer was on the right track (as far as the first paragraph went, anyway) but referred to the wrong variable. – madreflection Apr 17 '20 at 18:13
  • I added the exact error! – Light Apr 17 '20 at 18:17
  • Does this answer your question? [The name "......." does not exist in the current context. error](https://stackoverflow.com/questions/24464536/the-name-does-not-exist-in-the-current-context-error) – madreflection Apr 17 '20 at 18:18
  • I looked at that already and I still couldn't figure out, my brain is inept enough at this stuff yet haha – Light Apr 17 '20 at 18:21
  • I've unhidden and updated my answer, answered a bit too quickly at first based on the title of this question – ColinM Apr 17 '20 at 18:24

1 Answers1

1

It's not a "public void list", it's a variable that is defined within the scope of the method. To access it elsewhere simply set it as a field on the type.

I have moved the initialization of shuffledCards to the constructor because it is simply an ordered variant of a List<string> which contains fixed values.

Also, I have changed the return type of Countdown from void to Task. Any asynchronous methods should return a Task and not void (unless it's an event handler)

public class MyType
{
    // Private field, accessible within the scope of this type
    private List<string> shuffledCards;

    // Constructor which is invoked when a new instance of this type is initialized
    public MyType()
    {
        List<string> cards = new List<string>
        {
            "ms-appx:///Assets/CardA.png", "ms-appx:///Assets/CardA.png",
            "ms-appx:///Assets/CardB.png", "ms-appx:///Assets/CardB.png",
            "ms-appx:///Assets/CardC.png", "ms-appx:///Assets/CardC.png",
            "ms-appx:///Assets/CardD.png", "ms-appx:///Assets/CardD.png",
            "ms-appx:///Assets/CardE.png", "ms-appx:///Assets/CardE.png",
            "ms-appx:///Assets/CardF.png", "ms-appx:///Assets/CardF.png",
            "ms-appx:///Assets/CardG.png", "ms-appx:///Assets/CardG.png",
            "ms-appx:///Assets/CardH.png", "ms-appx:///Assets/CardH.png"
        };

        // Order cards and assign the result to shuffledCards
        shuffledCards = cards.OrderBy(a => Guid.NewGuid()).ToList();
    }

    private void Start_Click(object sender, RoutedEventArgs e)
    {
        btnStart.Opacity = 0;
        btnStart.IsEnabled = false;

        CardAssign();
    }

    public void CardAssign()
    {
        Card1Img.Source = new BitmapImage(new Uri(shuffledCards[0]));
        Card2Img.Source = new BitmapImage(new Uri(shuffledCards[1]));
        Card3Img.Source = new BitmapImage(new Uri(shuffledCards[2]));
        Card4Img.Source = new BitmapImage(new Uri(shuffledCards[3]));
        Card5Img.Source = new BitmapImage(new Uri(shuffledCards[4]));
        Card6Img.Source = new BitmapImage(new Uri(shuffledCards[5]));
        Card7Img.Source = new BitmapImage(new Uri(shuffledCards[6]));
        Card8Img.Source = new BitmapImage(new Uri(shuffledCards[7]));
        Card9Img.Source = new BitmapImage(new Uri(shuffledCards[8]));
        Card10Img.Source = new BitmapImage(new Uri(shuffledCards[9]));
        Card11Img.Source = new BitmapImage(new Uri(shuffledCards[10]));
        Card12Img.Source = new BitmapImage(new Uri(shuffledCards[11]));
        Card13Img.Source = new BitmapImage(new Uri(shuffledCards[12]));
        Card14Img.Source = new BitmapImage(new Uri(shuffledCards[13]));
        Card15Img.Source = new BitmapImage(new Uri(shuffledCards[14]));
        Card16Img.Source = new BitmapImage(new Uri(shuffledCards[15]));

        Countdown();
    }

    private async Task Countdown()
    {
        await Task.Delay(7000);

        Hide();
    }

    private void Hide()
    {
        Card1.IsEnabled = true;
        Card2.IsEnabled = true;
        Card3.IsEnabled = true;
        Card4.IsEnabled = true;
        Card5.IsEnabled = true;
        Card6.IsEnabled = true;
        Card7.IsEnabled = true;
        Card8.IsEnabled = true;
        Card9.IsEnabled = true;
        Card10.IsEnabled = true;
        Card11.IsEnabled = true;
        Card12.IsEnabled = true;
        Card13.IsEnabled = true;
        Card14.IsEnabled = true;
        Card15.IsEnabled = true;
        Card16.IsEnabled = true;

        Card1Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card2Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card3Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card4Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card5Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card6Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card7Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card8Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card9Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card10Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card11Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card12Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card13Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card14Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card15Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
        Card16Img.Source = new BitmapImage(new Uri("ms-appx:///Assets/Card Backs.png"));
    }

    private void Card1_Click(object sender, RoutedEventArgs e)
    {
        Card1Img.Source = new BitmapImage(new Uri(shuffledCards[0]));
    }
}
ColinM
  • 2,340
  • 12
  • 26
  • I tried out your edits and I still get the 'shuffledcards' does not exist in the current context... I also get a new error with the public MyType() saying I need a return type – Light Apr 17 '20 at 18:29
  • @Light C# is case sensitive, `shuffledCards` is the correct variable name. `MyType` also needs to match the name of your class, I have used `MyType` as your code example doesn't contain the full class code. – ColinM Apr 17 '20 at 18:30
  • The `c` was lowercase in the original post. – madreflection Apr 17 '20 at 18:31
  • My bad, you're totally right. I do still get the return type error though – Light Apr 17 '20 at 18:33
  • 1
    Please read the final sentence in my previous message and see [the following link](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constructors) – ColinM Apr 17 '20 at 18:33
  • I see now!! Thank you very much for all your help my friends:) – Light Apr 17 '20 at 18:35
  • Alright sorry to bother you both again, but now I get an exception on the first light of the CardAssign() method saying System.NullReferenceException Message=Object reference not set to an instance of an object. – Light Apr 17 '20 at 18:37
  • Have you changed `public MyType` to match the name of your class, such as `public Form1` or `public MainForm`? – ColinM Apr 17 '20 at 18:41
  • 1
    You've transitioned from compilation error to runtime error. That's progress. But without seeing what you have now (which might vary a bit from what was suggested), it would be difficult to assist you at this point. This would warrant a new question. *However*, I suggest reading this: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it). Questions about that exception usually gets closed with a reference to that answer, and very quickly. – madreflection Apr 17 '20 at 18:41
  • I went ahead and edited the code to show you guys where I am at now! I appreciate all the help, thanks so much :) any ideas? If not I'll go back to the drawing board haha – Light Apr 17 '20 at 18:45
  • Editing the answer like that is not the accepted process. The original problem was solved. This is a different problem. It warrants a new question. – madreflection Apr 17 '20 at 18:47
  • Ohh sorry, I'm new to the site. Well, thank you anyways! :) – Light Apr 17 '20 at 18:48
  • The reason why it's not the accepted process is because questions would be never ending which in turn makes it impossible for other developers to search for problems online and come across solutions. – ColinM Apr 17 '20 at 18:48