473,385 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(completeList) with an arraylist(myArrList) to do this I'm using a for loop which also appends information from another arraylist(nameListArray) to a string(nameList)

What I have:
Expand|Select|Wrap|Line Numbers
  1. Hashtable completeList = new Hashtable();
  2. ArrayList myArrList = new ArrayList();
  3. string[] nameListArray = new string[20];
  4. string nameList = "";
  5.  
  6. //**********************************//
  7.  
  8. if(myArrlist.Count > 1)
  9. {
  10. //some code
  11.  
  12.  int j = arraystate;
  13.  for(int i = 1; i < 5; i++)
  14.  {                         
  15.   nameList += i+". "+nameListArray[j];    
  16.   completeList.Add("0"+i, myArrList[j].ToString());
  17.   j++;    
  18.  }
  19.  
  20.  if(arraystate != 15)
  21.  {completeList.Add("0", nameList+"N. for Next");}
  22.  
  23.  else
  24.  {completeList.Add("0", nameList);}
  25.  
  26. }
  27.  
  28. else
  29. {
  30. //some code
  31. }
  32.  
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?
Jun 24 '10 #1

✓ answered by Frinavale

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):

Expand|Select|Wrap|Line Numbers
  1. Hashtable completeList = new Hashtable();
  2. ArrayList myArrList = new ArrayList();
  3. string[] nameListArray = new string[20];
  4. string nameList = "";
  5.  
  6. if(myArrlist.Count > 1)
  7. {
  8. //some code
  9.  
  10.  int j = arraystate;
  11.  for(int i = 1; i < 5; i++)
  12.  {        
  13.  
  14. //Added an if statment to make sure that j is 
  15. //within the boundaries of the nameListArray
  16. if(j<nameListArray.Length){                 
  17.   nameList += i+". "+nameListArray[j];
  18. //Added a ToString() method call to i here since
  19. //you want to concatenate i to "0" (I think?) 
  20.   completeList.Add("0"+i.ToString(), myArrList[j].ToString());
  21.   j++;    
  22. }
  23.  }
  24.  
  25. //Shouldn't you be chekcking j here?
  26. //why check arraystate?
  27.  if(arraystate != 15)
  28.  {completeList.Add("0", nameList+"N. for Next");}
  29.  
  30.  else
  31.  {completeList.Add("0", nameList);}
  32.  
  33. }
  34.  
  35. else
  36. {
  37. //some code
  38. }
Have you attempted to step through the code?

-Frinny

7 1372
ThatThatGuy
449 Expert 256MB
@JakeMathews
What error are you facing
Jun 25 '10 #2
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
Jun 25 '10 #3
Frinavale
9,735 Expert Mod 8TB
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):

Expand|Select|Wrap|Line Numbers
  1. Hashtable completeList = new Hashtable();
  2. ArrayList myArrList = new ArrayList();
  3. string[] nameListArray = new string[20];
  4. string nameList = "";
  5.  
  6. if(myArrlist.Count > 1)
  7. {
  8. //some code
  9.  
  10.  int j = arraystate;
  11.  for(int i = 1; i < 5; i++)
  12.  {        
  13.  
  14. //Added an if statment to make sure that j is 
  15. //within the boundaries of the nameListArray
  16. if(j<nameListArray.Length){                 
  17.   nameList += i+". "+nameListArray[j];
  18. //Added a ToString() method call to i here since
  19. //you want to concatenate i to "0" (I think?) 
  20.   completeList.Add("0"+i.ToString(), myArrList[j].ToString());
  21.   j++;    
  22. }
  23.  }
  24.  
  25. //Shouldn't you be chekcking j here?
  26. //why check arraystate?
  27.  if(arraystate != 15)
  28.  {completeList.Add("0", nameList+"N. for Next");}
  29.  
  30.  else
  31.  {completeList.Add("0", nameList);}
  32.  
  33. }
  34.  
  35. else
  36. {
  37. //some code
  38. }
Have you attempted to step through the code?

-Frinny
Jun 25 '10 #4
@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.Length 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.

Expand|Select|Wrap|Line Numbers
  1. if(userinput == "N") 
  2.  {
  3.   arraystate = (arraystate == 10) ? arraystate = 15 : (userinput == "N" && arraystate == 0) ? arraystate = 5 : arraystate = 10;
  4.  } 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
Jun 25 '10 #5
Frinavale
9,735 Expert Mod 8TB
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
Jun 25 '10 #6
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
Jun 25 '10 #7
Frinavale
9,735 Expert Mod 8TB
I'm sorry but I still have no idea what you're trying to accomplish...
Jun 25 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kakaiya | last post by:
Error message: An unhandled exception of type 'System.InvalidCastException' occurred in test.dll Additional information: Specified cast is not valid. Public Class Form1 Inherits...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Ilann | last post by:
Hi, I'm trying to create a new Hashtable and use it: System.Collections.Hashtable hash_directory; hash_directory.Item("blabla"); and here is the error message I get:
4
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a...
9
by: Oberon | last post by:
My HashTable (Global.Games) is a static collection of objects of type Game. A Game object has 8 fields (exposed as properties). The key to the HashTable is also one of these fields (GameID, of type...
5
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
16
by: Daniel | last post by:
Hi Group, Why does the below code not generate an error? I have a table with x number of records. Each record is read from the DB into a hashtable. Then I add to the hashtable additional keys....
15
by: judge82 | last post by:
I made sure I closed all the bracket, why do i still get the error. on line 360, 424, 425 and 607 Please help /* * ShopCartServletHW3.java *
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
2
by: almurph | last post by:
H ieveryone, Can you help me please? I am trying to sort a hashtable but get the error: "Cannot implicity convert type void to System.Collections.ArrayList" I am doing the following: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.