StringBuilder - Remove the last element

Hi Experts,

I am iterating over a Hashtable to extract the Keys, then attempting to build a string to acheive something like this:    
"1, 2, 5, 9, 100, 50".  This string is then used in an SQL query as a set of integers.

the problem is removing the last "," off the end, i.e the last element of the String, I get index out of bounds, or value is less than 0 .... any help would be appreciated.


    public int calculatePriorityLevel()
    {
        StringBuilder builder = new StringBuilder(100);
        foreach (string strKey in myHashtable.Keys)
        {
            builder.Append(strKey);
            builder.Append(",");
        }
        builder.Remove((builder.Length - 1), 1);

        return DAL.GetPriorityLevel(builder.ToString());
    }
simonm_jpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
This code is working as expected, the last comma is removed correctly. Where is the error comming up becuse the code looks good and works on my machine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShaggydmpCommented:
try this instead
 
public int calculatePriorityLevel()
{
   string[] strArray= new string[myHashtable.Keys.length];
   for (int i=0; i<myHashtable.Keys.Length; i++)
   {
           strArray[i]=myHashtable.Keys[i];
   }
   return DAL.GetPriorityLevel(String.Join(',', strArray));
}
philowareCommented:
As FernandoSoto said your code works correctly so the exception is thrown elsewhere. If you write the line which throws exception we may be more helpful.

And if  you modify Shaggydmp's code by this way it'll work too.

public int calculatePriorityLevel()
{
  string[] strArray= new string[myHashTable.Keys.Count];
  myHashTable.Keys.CopyTo(strArray,0);
  return DAL.GetPriorityLevel(String.Join(",", strArray));
}
simonm_jpAuthor Commented:
Thanks for your input ...  An Exception of type ArguementOutOfRangeException was thrown on the line:  builder.Remove((builder.Length - 1), 1); Detail of exception:  "StartIndex cannot be less than zero. Parameter name: startIndex"  in a rare circumstance where myHashtable was null or had not yet been populated. my fault.

Cheers,


Fernando SotoRetiredCommented:
I am glad to see that you found your error.  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.