Hi Michael,
If you haven't experienced any issues then you might not want to concern yourself with modifying code that works, as I believe
Robinson was suggesting by asking questions related to the performance of your design, but I do understand if you only want to
perform some simple optimizations ahead of time.
That said, have you thought about using a StringDictionary instead of a StringBuilder or string buffer?
You might want to just parse the complete input string once, or parse each field on-demand and store the references to each of the
fields' values in case they are recalled at a later time. If you can design the class so that the parsing of each field is only
done once then a StringBuilder would be of no use, unless of course you need to rebuild the buffer for output with new values.
It might be cheaper to simply rebuild the buffer on-demand then it would be to perform string manipulation in each properties'
get/set accessor, but that of course depends on the number of property read/writes compared to the number of times your class will
need to rebuild the buffer for output and the actual tasks performed by each operation. I think, without those statistics, that
parsing in the get/set accessors each time will probably have an increased negative effect on your application's performance and the
GC more than if you were to cache the fields values in a StringDictionary and simply get/set them when required.
HTH
--
Dave Sexton
[quoted text, click to view] "Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message news:uwE14894GHA.4196@TK2MSFTNGP03.phx.gbl...
> OK, I can't figure out a way to optimize the following VB 2005 code using StringBuilders:
>
> Public Const RecSize as Integer = 105
> Private buffer As String
>
> Public Sub New()
> init
> End Sub
> Public Sub New(ByVal value As String)
> buffer = LSet(value, 105)
> End Sub
>
> Public Sub init()
> buffer = Space(105)
> End Sub
>
> Private Function fetch(ByVal start As Integer, ByVal chars As Integer) As String
> fetch = Mid(buffer, start, chars)
> End Function
> Private Sub putch(ByRef s As String, ByVal start As Integer, ByVal chars As Integer)
> Mid(buffer, start, chars) = LSet(s, chars)
> End Sub
>
> Public Property BUF() as String
> Get
> Return fetch(1, 105)
> End Get
> Set(ByVal value as string)
> putch(value, 1, 105)
> End Set
> End Property
>
> Public Property KEY0() as String
> Get
> Return fetch(1, 19)
> End Get
> Set(ByVal value as string)
> putch(value, 1, 19)
> End Set
> End Property
>
> ' Additional fields are also defined in the same manner.
>
> The public interface to this class must remain as strings, but I'd like to optimize the implementation. Note that this is a
> sample from our interface to a VMS BASIC environment and this code is actually being generated by a VB 6 program from the VMS
> Record Management Services (RMS) definition files. Once a data record is in this object, I need to be able to change it, but don't
> want to drive the GC nuts allocating/deallocating the internal buffer string. For each class, the total length of buffer is
> fixed. I can't use a simple record because RMS allows overlapping variants and the VB 6 program has been coded to support that.
>
> Mike Ober.
>
>