giving a value to a variable... I really don't get what i m doing wrong.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

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

  1. #1
    Join Date
    Jul 2012
    Posts
    3

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

    Hello everyone I've been working on this for hours trying to fix it to no avail so i decided to look for some online help and found this forum by chance.

    this is basically the piece of code:

    Code:
                //get cards quantities
                int[] cardm1 = null;
                i = 1;
    
                while (i < countcardsquantity)
                {
                    int m = i - 1;
    
                    cardm1[m] = int.Parse(cardquantity[i]);
    
                    i = i + 2;
                }
    there is nothing wrong with the values of countcardsquantity and cardquantity[i] since i tested them separately but anyway countcardsquantity = 140 if you were wondering.

    Anyway after trying to put message boxes everywhere i realized that anything after this line of code
    Code:
    cardm1[m] = int.Parse(cardquantity[i]);
    wasn't showing the message box even if the message box code is actually outside the loop so i m guessing something's wrong with this line I just have no idea what the problem is...

    and by the way commenting out the line made everything else work normally. So yeah I've been wondering for a while what is wrong with that simple line?

    Thank you, appreciate the help.

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

    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 09:36 PM.

  3. #3
    Join Date
    Jul 2012
    Posts
    3

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

    Thank you for all your help, the thing is I need the number of the array for cardm1 later on in my project and no there was no error with the numbers 0,2 ... that's what i wanted it to do, I don't really understand why i should do countcardsquantity - 1 as the number of elements though shouldn't it just be countcardsquantity / 2 anyway since I'm not going to use the other numbers or do arrays usually have to be numbered 0,1,2,3... when i specify i number for them?

    I did know that i could debug the thing is debugging didn't show me any problems at all everything looked fine (well to my untrained eyes)

    I can't really use a list here because i need to compare arrays using the i inside of the [] though so that wouldn't really work for what I need.

    Anyway thank you very much you were a great help and I've actually learned a lot from that single post

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

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

    In C#, and most other popular languages, array indices are zero-based (you start counting from 0, instead from 1). So, for example, a 5 element array has elements at positions 0, 1, 2, 3, and 4. (0 to length-1).

    In the first case, where I said that you should use countcardsquantity - 1, it's because arrays have a fixed size: if you're skipping elements, the skipped elements are still going to stay there. Since you must not access elements beyond the last, you must make sure that the array has enough elements when you create it.
    Look at this:
    Code:
               i:  .  1  .  3  .  5 
    cardquantity: [a][b][c][d][e][f]   -----length: 6
                      |     |     |
                      |     |     |
                      |     |     |
                      |     |     |
    m = i-1           |     |     |
                  m:  0  .  2  .  4 
             cardm1: [b][.][d][.][f]   -----length: 5
    vs this:
    Code:
               i:  .  1  .  3  .  5 
    cardquantity: [a][b][c][d][e][f]   -----length: 6
                      |     |     |
                      |   __|     |
                      |  |        |
                      |  |   _____|
    m = (i-1)/2       |  |  |
                  m:  0  1  2  
             cardm1: [b][d][f]   -----length: 3
    Lists support [] just like normal arrays, and you can allways get an array from a list by calling the ToArray() method.

  5. #5
    Join Date
    Jul 2012
    Posts
    3

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

    Oh ok , thank you for explaining everything so thoroughly i got what you mean. Well then thanks again

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center