Hashtable and forloop error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JakeMathews
    New Member
    • Jun 2010
    • 4

    Hashtable and forloop error

    Hi everyone,

    First off this is my first post on this site so hello again.
    I'm a beginner at programming in C#/ASP so the code I'm about show you may be bad practice or even worse but I'm getting better I hope.

    What I want: I want to populate a hashtable(compl eteList) with an arraylist(myArr List) to do this I'm using a for loop which also appends information from another arraylist(nameL istArray) to a string(nameList )

    What I have:
    Code:
    Hashtable completeList = new Hashtable();
    ArrayList myArrList = new ArrayList();
    string[] nameListArray = new string[20];
    string nameList = "";
    
    //**********************************//
    
    if(myArrlist.Count > 1)
    {
    //some code
    
     int j = arraystate;
     for(int i = 1; i < 5; i++)
     {	 					
      nameList += i+". "+nameListArray[j];	
      completeList.Add("0"+i, myArrList[j].ToString());
      j++;	
     }
    
     if(arraystate != 15)
     {completeList.Add("0", nameList+"N. for Next");}
     
     else
     {completeList.Add("0", nameList);}
    
    }
    
    else
    {
    //some code
    }
    The problem:
    If my nameListArray contains less then 5 entries the if @line20 won't be executed but when it's larger then 5 no 'error' occurs. I've also found out when I'm commenting line 16 whether the array has less then 5 or more entries the program will execute it.

    Can anyone help me please?
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    Originally posted by JakeMathews
    Hi everyone,

    First off this is my first post on this site so hello again.
    I'm a beginner at programming in C#/ASP so the code I'm about show you may be bad practice or even worse but I'm getting better I hope.

    What I want: I want to populate a hashtable(compl eteList) with an arraylist(myArr List) to do this I'm using a for loop which also appends information from another arraylist(nameL istArray) to a string(nameList )

    What I have:
    Code:
    Hashtable completeList = new Hashtable();
    ArrayList myArrList = new ArrayList();
    string[] nameListArray = new string[20];
    string nameList = "";
    
    //**********************************//
    
    if(myArrlist.Count > 1)
    {
    //some code
    
     int j = arraystate;
     for(int i = 1; i < 5; i++)
     {	 					
      nameList += i+". "+nameListArray[j];	
      completeList.Add("0"+i, myArrList[j].ToString());
      j++;	
     }
    
     if(arraystate != 15)
     {completeList.Add("0", nameList+"N. for Next");}
     
     else
     {completeList.Add("0", nameList);}
    
    }
    
    else
    {
    //some code
    }
    The problem:
    If my nameListArray contains less then 5 entries the if @line20 won't be executed but when it's larger then 5 no 'error' occurs. I've also found out when I'm commenting line 16 whether the array has less then 5 or more entries the program will execute it.

    Can anyone help me please?
    What error are you facing

    Comment

    • JakeMathews
      New Member
      • Jun 2010
      • 4

      #3
      The error I'm facing is that when the arraylist myArrList contains less then 5 values the following if/else statement from line 20 won't get executed at all heck nothing gets executed after line 18 it just jumps out of the surrounding brackets which would be fine if I wanted that.

      I hope you can understand me now

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm not entirely sure what you are trying to do by looking at your code....but I've made some minor changes to it so to avoid potential problems (it may fix your problem actually):

        Code:
        Hashtable completeList = new Hashtable();
        ArrayList myArrList = new ArrayList();
        string[] nameListArray = new string[20];
        string nameList = "";
        
        if(myArrlist.Count > 1)
        {
        //some code
         
         int j = arraystate;
         for(int i = 1; i < 5; i++)
         {        
        
        //Added an if statment to make sure that j is 
        //within the boundaries of the nameListArray
        if(j<nameListArray.Length){                 
          nameList += i+". "+nameListArray[j];
        //Added a ToString() method call to i here since
        //you want to concatenate i to "0" (I think?) 
          completeList.Add("0"+i.ToString(), myArrList[j].ToString());
          j++;    
        }
         }
        
        //Shouldn't you be chekcking j here?
        //why check arraystate?
         if(arraystate != 15)
         {completeList.Add("0", nameList+"N. for Next");}
         
         else
         {completeList.Add("0", nameList);}
         
        }
         
        else
        {
        //some code
        }
        Have you attempted to step through the code?

        -Frinny

        Comment

        • JakeMathews
          New Member
          • Jun 2010
          • 4

          #5
          @Frinavale thanks for the reply
          I know with my code snippet it's impossible to know what I'm trying to do.

          The minor adjustment you suggested really helped me out. By changing Line16 nameListArray.L ength to myArrList.Count the code is working fine. Thank you very much.

          This is the reason I'm not checking j I know it is clumsy.

          Code:
          if(userinput == "N") 
           {
            arraystate = (arraystate == 10) ? arraystate = 15 : (userinput == "N" && arraystate == 0) ? arraystate = 5 : arraystate = 10;
           } else {arraystate = arraystate;}
          What I was trying to do is make some kind of loop: when user input is N go from 0 > 5 > 10 > 15 and back to 0
          but I'm stuck at 10 > 15 and back.
          Maybe I'm not seeing the picture here and using a really bad structure but I can't figure it out.
          If you could give some advise I'd really appreciate it.

          Thank you for helping me

          -JakeM

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            So the user selects "N" (as opposed to "M") and in this case you want to loop by multiples of 5? (and say multiples of 2 if they choose "M"?)

            ??

            -Frinny

            Comment

            • JakeMathews
              New Member
              • Jun 2010
              • 4

              #7
              Sorry for not being clear.
              Here's the thing arraystate = 0; but everytime the user inputs "N" 5 will be added to the current arraystate and when 15 is reached the arraystate becomes 0 again(not implemented yet though)

              There is no other input involved in this section

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                I'm sorry but I still have no idea what you're trying to accomplish...

                Comment

                Working...