Results 1 to 5 of 5

Thread: [RESOLVED] VS 2013 Is there a way to "lock" a variable value?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    8

    Resolved [RESOLVED] VS 2013 Is there a way to "lock" a variable value?

    First of all, hello, as I am new to this forum.
    I am also new to programming (decided to see what it is about out of curiosity - it's not work-related for me), so if someone answers I would appreciate the answer to be easily understandable.

    I am trying to simulate drawing cards from a deck and then display drawn cards. Here is the code (the most relevant part is at the end):

    Code:
    Do Until DeckContains.Contains(randomValue)
                randomValue = generator.Next(1, 14)
            Loop
            i = i + 1
            Card = randomValue
            MyHandContains.Add(randomValue)
            DeckContains.Remove(randomValue)
            Select Case Card
                Case Is = 1
                    cardName = "Ace of Hearts"
                    cardValue = 11
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/3.png")
                Case Is = 2
                    cardName = "2 of Hearts"
                    cardValue = 2
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/51.png")
                Case Is = 3
                    cardName = "3 of Hearts"
                    cardValue = 3
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/47.png")
                Case Is = 4
                    cardName = "4 of Hearts"
                    cardValue = 4
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/43.png")
                Case Is = 5
                    cardName = "5 of Hearts"
                    cardValue = 5
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/39.png")
                Case Is = 6
                    cardName = "6 of Hearts"
                    cardValue = 6
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/35.png")
                Case Is = 7
                    cardName = "7 of Hearts"
                    cardValue = 7
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/31.png")
                Case Is = 8
                    cardName = "8 of Hearts"
                    cardValue = 8
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/27.png")
                Case Is = 9
                    cardName = "9 of Hearts"
                    cardValue = 9
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/23.png")
                Case Is = 10
                    cardName = "10 of Hearts"
                    cardValue = 10
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/19.png")
                Case Is = 11
                    cardName = "Jack of Hearts"
                    cardValue = 10
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/15.png")
                Case Is = 12
                    cardName = "Queen of Hearts"
                    cardValue = 10
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/11.png")
                Case Is = 13
                    cardName = "King of Hearts"
                    cardValue = 10
                    MyCardSource.UriSource = New Uri("ms-appx:/Assets/7.png")
            End Select
    
            Description.Text = Description.Text & " & " & cardName
            HandValue = HandValue + cardValue
     Select Case i
                Case Is = 3
                    MyImage3.Source = MyCardSource
                Case Is = 4
                    MyImage4.Source = MyCardSource
                Case Is = 5
                    MyImage5.Source = MyCardSource
     End Select
    My problem is that when i draw 4th card my 3rd card gets overwritten and they both display the same card. Is there a way to "lock" MyImage3.Source value so it wouldn't change even after next card is drawn (which causes MyCardSource to change value)?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,238

    Re: VS 2013 Is there a way to "lock" a variable value?

    Your problem is in understanding how reference types work. A reference type would be any class, such as whatever type MyCardSource is. A reference type variable doesn't hold the actual value. The value is stored somewhere out in memory. All the variable holds is a reference to that memory. In other words, it pretty much just holds the memory address of the actual value.

    What you are doing is changing MyCardSource, but that hasn't changed the object it is pointing to, just a part of the object (the URISource). You then set the MyImageX.Source to MyCardSource, but remember that MyCardSource is just the address of the object out in memory. So, what you are putting into MyImage3.Source, MyImage4.Source, and MyImage5.Source are just copies of what is in MyCardSource, which is just an address to an object sitting out in memory. So, all you are doing is making three copies of the address, but there is still only the one object out in memory. Similarly, making a copy of your house address doesn't give you a new house.

    What you need to be doing is creating a new MyCardSource instance for each MyImage. Exactly what that entails I can't say, as you haven't shown what MyCardSource is, but that's what you have to do. Just remember that MyCardSource doesn't hold the value of the object, it is just the address of the object out in memory. You have to make sure that you are creating a new object each time, which you have not done.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: VS 2013 Is there a way to "lock" a variable value?

    Just offering an observation that may help others focus on your issue.

    Based your code syntax, I suspect that this is a WPF application. If it is, please edit your post and clearly state that it is.

    The majority of the questions on this forum are for WinForm applications, so people tend to assume that as the case.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    8

    Re: VS 2013 Is there a way to "lock" a variable value?

    Thanks for the reply and for taking time to explain things.
    Based on what you said I created separate instances of MyCardXSource instead of just one MyCardSource. Then I just transfered the first part of code (card drawing) 3 times into the part which keeps track of how many cards were drawn. It is not pretty (it tripled in volume) but at least it works now as intended. Thanks!

    @ TnTinMN
    Sorry, but I don't understand what WPF or WinForm are. This code is a part of a default application created in Visual Studio Express (New Project --> BlankApp(Windows))

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,238

    Re: [RESOLVED] VS 2013 Is there a way to "lock" a variable value?

    I don't think I would recognize WPF, so I'm not sure what TnTinMN is seeing.

    WPF stands for Windows Presentation Foundation. It's a means of creating applications with very rich, graphics intensive, interfaces. From your description, I would say that you aren't doing WPF.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width