none
free program's memory when a if statement is false RRS feed

  • Question

  • Hi.

    I am creating a puzzle game that has 15 tiles. I have made a string array variable to hold the names of the 15 tiles, which is initialized inside the "form_load" method. Now, it is really important that the string array is filled with 15 elements. Otherwise the program latter on will not know the names of the empty string tiles. However, in normal situations, having empty strings in an array is not illegal. Therefore, the program will never throw an error when using empty strings in the array during game play. So, my question is, I want to know how I could cause the program to throw an error so that the program releases its memory and exit out of the program? Could any of you help me out with this?

    Saturday, April 13, 2019 3:35 PM

Answers

  • There is no need to free your memory this is managed by the .net run time via the garbage collection when needed.

    Its not clear for what reason you want the error to thrown for? Is it when you find an empty string as an element in the array? because you mentioned " having empty strings in an array is not illegal. Therefore, the program will never throw an error "

    If for empty element you can simply loop throug the array elements and do your check then throw an exception as an alert (not sure if its a winforms app or else?) I don't see the reason why to quit the application.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays


    Fouad Roumieh


    Saturday, April 13, 2019 4:53 PM
  • You are developing a game, so the user should never face an error or quit the game because the developer failed to name the control, this is a bug, the user should play without errors.

    You can use the Tag property on the button control and assign just a numeric value instead of following the auto-gen naming of the control.


    Fouad Roumieh

    Saturday, April 13, 2019 6:27 PM

All replies

  • There is no need to free your memory this is managed by the .net run time via the garbage collection when needed.

    Its not clear for what reason you want the error to thrown for? Is it when you find an empty string as an element in the array? because you mentioned " having empty strings in an array is not illegal. Therefore, the program will never throw an error "

    If for empty element you can simply loop throug the array elements and do your check then throw an exception as an alert (not sure if its a winforms app or else?) I don't see the reason why to quit the application.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/using-foreach-with-arrays


    Fouad Roumieh


    Saturday, April 13, 2019 4:53 PM
  • I don't see the reason why to quit the application.


    Fouad Roumieh

    Maybe I am analyzing this too far, but the first problem I needed to solve, was turning 15 Button controls to represent as the 15 tiles. Next, display the numbers 1-15 in ascending order onto each Button control or known as tiles. Then, the program will scramble the tiles out of order, and the player has to re-order them back in consecutive order, 1 through 15.

    The next issue I found was how do I make the tiles display the proper number it belongs to. The best solution I could find was to use the name of the control and extract the number that gets added at the end when the button controls are duplicated with the same name. Ex.: button1, button2, button3, and so on. However, I had a concern that, if for any reason, one or more Button controls are improperly named, my RegEx expression will convert the improper name into an empty string, because Regex did not find a match. ... and like I said before, none of my tiles cannot have an empty string, because you cannot put empty strings in a sequenced order. Ex.: "1", "2", "", "4", "", "6" ... "15" is not possible. Therefore, in this example, how would the player know that the first empty string is a "3"? So, I thought it would be nice to display a friendly error that the player can report back to me, notifying me that this case has happened.

    Unless you think this is overkill?

    Saturday, April 13, 2019 6:17 PM
  • You are developing a game, so the user should never face an error or quit the game because the developer failed to name the control, this is a bug, the user should play without errors.

    You can use the Tag property on the button control and assign just a numeric value instead of following the auto-gen naming of the control.


    Fouad Roumieh

    Saturday, April 13, 2019 6:27 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 1:21 AM
    Moderator