CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: giving a value to a variable... I really don't get what i m doing wrong.

    The problem is that the cardm1 array is left uninitialized (null) - cardm1 is just a variable, but it's not pointing to anything.
    You have to replace that first line below the comment with:
    int[] cardm1 = new int[number_of_elements];

    This will create an array with number_of_elements number of "slots", that you can then use.

    But, there may be other problems here; as far as I can see, you're accessing every other element of cardquantity, starting with the second element (i = 1, 3, 5, 7...), and storing the result of the parse operation into the cardm1 array at positions m = 0, 2, 4, 6...? This could be what you want, but it's more likely it's an error in application logic, since there are unused elements of cardm1? If this is not an error, then the number_of_elements should be countcardsquantity - 1.
    If you wanted to have m = 0, 1, 2, 3..., you can use this: m = (i - 1) / 2.
    In this case number_of_elements should be countcardsquantity / 2.

    Tip: with generic List<T> class, which is a resizable array of type T, you can get many benefits at almost no real cost, and can for the most part forget about arrays.
    In your case, you would use it like this:
    Code:
    List<int> cardm1 = new List<int>();     // initializes the list object, contains 0 elements at this moment
    
    i = 1;
    while (i < countcardsquantity)
    {
        int cardQ = int.Parse(cardquantity[i]);
        cardm1.Add(cardQ);    // adds an element at the next position, cardm1.Count increases by 1
    
        i = i + 2;
    }
    Note that you didn't have to use a while loop; this has the same effect:

    Code:
    List<int> cardm1 = new List<int>();     // initializes the list object, contains 0 elements at this moment
    
    for (int i = 1; i < countcardsquantity; i = i + 2)
    {
        int cardQ = int.Parse(cardquantity[i]);
        cardm1.Add(cardQ);    // adds an element at the next position, cardm1.Count increases by 1
    }
    Finally, don't debug with message boxes, it's a real pain ;D.
    Set a breakpoint (from the Debug menu, or by clicking on the grayish vertical stripe left of your code, or by going to a line of code and hitting F9) - a big red dot will appear. When you start debugging (F5), when your program reaches a line with a breakpoint set, it will break execution temporarily, and the IDE will show you that line of code. You can then inspect the values of the variables by hovering your mouse pointer over them. You can also drag (or enter) variable names, and even expressions in the Watch window (or right click > Add watch) to keep track of their values as they change during the execution.
    Pressing F5 again will continue the execution, but you can also go step-by-step with F10 ("Step Over", also available in the Debug menu, and on the toolbar of the debugger).
    There are other interesting options, but I'll let you explore them yourself.
    Last edited by TheGreatCthulhu; July 31st, 2012 at 08:36 PM.

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

Featured