Hello Benj Nunez,

                           Here I mention what you miss in your code

TmpBuilder.Remove(TmpBuilder.Length - 1, 1); // problem  code

TmpBuilder.Remove(TmpBuilder.ToString().Length - 1, 1);// solution to
problem

@ Arsalan

                Mr.Arsalan  I thing your suggestion(First point only) is
wrong because  Use of StringBuilder is remove string operation
(concatenation )   within String buffer.

For eg.



StringBuilderBin1.Append(shEntry.shOldIDNumber + ",");

When you use  above code

Two Immutable string concat to new instance of string then finally append to
string buffer



in my opinion  below  line is correct

StringBuilderBin1.Append(shEntry.shOldIDNumber).Append(",");
Here doesn’t create any instance of string it only append to string buffer

2009/10/16 Benj Nunez <[email protected]>

>
> Thanks Arsalan for your tips. I'll consider this on my other
> projects. :)
>
> Here's what I did which solved my problem:
>
>
>        public static StringBuilder clearTrailingComma(ref
> StringBuilder ABuilder)
>        {
>            string Holder = ABuilder.ToString();
>
>            // Bad code: Holder.Remove(Holder.Length - 1, 1);
>            Holder = Holder.TrimEnd(',');
>
>            StringBuilder TmpBuilder = new StringBuilder(Holder);
>
>            return TmpBuilder;
>        }
>
>
>
> Regards,
>
>
> Benj
>
>
>
>
>
> On Oct 15, 12:37 pm, Arsalan Tamiz <[email protected]> wrote:
> > I can't find a real bug in your code but here are my suggestions
> >
> > 1) StringBuilderBin1.Append(shEntry.shOldIDNumber).Append(",");
> >
> > in my opinion (maybe NOT correct) above line should be written like this
> >
> > StringBuilderBin1.Append(shEntry.shOldIDNumber + ",");
> >
> > 2) "clearTrailingComma" <- this is actually a function returning
> > "StringBuilder" so there is NO need to pass the same thing by reference.
> > Simply don't use "ref" here.3) "if ( (TmpBuilder != null) ||
> > (TmpBuilder.Length > 0) )" <- you should use "&&" instead of "||"
> > 4) "if (TmpBuilder[TmpBuilder.Length - 1].ToString ().EndsWith(","))" <-
> > this check is strange, you are getting the last character, converting it
> to
> > string, and then using the "EndsWith" why? obviously it would be a single
> > character.
> > 5) In your function "clearTrailingComma" you should use parameter
> directly,
> > there is NO need to again assign that parameter to some other variable.
> > 6) Put some break points and check the values at different points of your
> > code
> >
> > Regards,
> > Arsalan Tamiz
> >
>  > On Wed, Oct 14, 2009 at 6:55 AM, Benj Nunez <[email protected]>
> wrote:
> >
> > > Hello everyone,
> >
> > > I'm stumped at the moment trying to figure out how to remove a quote
> > > (",") character from a single entry within a Stringbuilder object. I
> > > have code that looks like this:
> >
> > > public bool parseEntries(ref BackgroundWorker worker,
> > >                                  ref List<StringHolder> ACVMEntries,
> > >                                  ref DoWorkEventArgs e)
> > > {
> > >   ...
> > >  if (ACVMEntries != null)
> > >  {
> > >       ..
> > >      foreach (StringHolder shEntry in ACVMEntries)
> > >      {
> > >           string CVMEntry = shEntry.shType.ToLower().Trim();
> > >           ...
> >
> > >           if (CVMEntry == someConst.foo1)
> > >           {
> > >                 StringBuilderBin1.Append(shEntry.shOldIDNumber).Append
> > > (",");
> > >           }
> > >           else if  (CVMEntry == someConst.foo2)
> > >           {
> > >                 StringBuilderBin2.Append(shEntry.shOldIDNumber).Append
> > > (",");
> > >           }
> > >           ...  // and so on...
> > >      }  // of foreach
> >
> > >      // Remove extraneous comma at the end of each StringBuilder.
> > >      ...
> > >     StringBuilder1 = Utils.clearTrailingComma(ref StringBuilder1);
> > >     StringBuilder2 = Utils.clearTrailingComma(ref StringBuilder2);
> > >     ...
> >
> > > }
> >
> > > The implementation of clearTrailingComma() looks like this:
> >
> > >        public static StringBuilder clearTrailingComma(ref
> > > StringBuilder ABuilder)
> > >        {
> > >            StringBuilder TmpBuilder = ABuilder;
> >
> > >            if ( (TmpBuilder != null) || (TmpBuilder.Length > 0) )
> > >            {
> > >                // Remove last quote
> > >                if (TmpBuilder[TmpBuilder.Length - 1].ToString
> > > ().EndsWith(","))
> > >                {
> > >                    // Remove just one character at the end of the
> > > string.
> > >                    try
> > >                    {
> > >                        TmpBuilder.Remove(TmpBuilder.Length - 1,
> > > 1);   // Problem here***
> > >                    }
> > >                    catch (System.ArgumentOutOfRangeException)
> > >                    {
> > >                        throw;
> > >                    }
> > >                    catch (System.Exception)
> > >                    {
> > >                        throw;
> > >                    }
> > >                }
> > >            }
> >
> > >            return TmpBuilder;
> > >        }
> >
> > > My issue is with a call to this line:
> >
> > > TmpBuilder.Remove(TmpBuilder.Length - 1, 1);   // Problem here***
> >
> > > This line can handle several entries within the Stringbuilder object,
> > > but it *cannot* remove
> > > the "," character when there is only *one* and only *one* entry. How
> > > can this be?
> >
> > > Any advice?
>



-- 
Regards
*******************
*C.Arun Kumar *
*******************

Reply via email to