EnsureCapacity()

Namespace ›› System.MfText ›› StringBuilder ›› Methods ››
Parent Previous Next

EnsureCapacity()

OutputVar := instance.EnsureCapacity(capacity)

EnsureCapacity(capacity)

Ensures that the capacity of this instance of StringBuilder is at least the specified value.

Parameters

capacity

The minimum capacity to ensure.
Can be any type that matches IsInteger or var integer.

Throws

Throws MfArgumentOutOfRangeException capacity is less than zero -or- Enlarging the value of this instance would exceed MaxCapacity.

Returns

The new capacity of this instance as var integer.

Remarks

If the current capacity is less than the capacity parameter, memory for this instance is reallocated to hold at least capacity number of characters; otherwise, no memory is changed.

Example

sb :=  new MfText.StringBuilder()
sb1 := new MfText.StringBuilder("abc")
sb2 := new MfText.StringBuilder("abc", 16)
sb.AppendLine()
sb.AppendFormat("a1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
sb.AppendLine()
sb.AppendFormat("a2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
sb.AppendLine()
sb.AppendFormat("a3) sb1.ToString() = '{0}', sb2.ToString() = '{1}'", sb1.ToString(), sb2.ToString())
sb.AppendLine()
sb.AppendFormat("a4) sb1 equals sb2: {0}", sb1.Equals(sb2) = true?"true":"false")
sb.AppendLine()

sb.AppendLine()
sb.AppendLine("Ensure sb1 has a capacity of at least 50 characters.")
sb.AppendLine()

sb1.EnsureCapacity(50)
sb.AppendFormat("b1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
sb.AppendLine()
sb.AppendFormat("b2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
sb.AppendLine()
sb.AppendFormat("b3) sb1.ToString() = '{0}', sb2.ToString() = '{1}'", sb1.ToString(), sb2.ToString())
sb.AppendLine()
sb.AppendFormat("b4) sb1 equals sb2: {0}", sb1.Equals(sb2) = true?"true":"false")
sb.AppendLine()
sb.AppendLine()
sb.AppendLine("Set the length of sb1 to zero.")
sb.AppendLine("Set the capacity of sb2 to 51 characters.")
sb1.Length := 0
sb2.Capacity := 51

sb.AppendLine()
sb.AppendFormat("c1) sb1.Length = {0}, sb1.Capacity = {1}", sb1.Length, sb1.Capacity)
sb.AppendLine()
sb.AppendFormat("c2) sb2.Length = {0}, sb2.Capacity = {1}", sb2.Length, sb2.Capacity)
sb.AppendLine()
sb.AppendFormat("c3) sb1.ToString() = '{0}', sb2.ToString() = '{1}'", sb1.ToString(), sb2.ToString())
sb.AppendLine()
sb.AppendFormat("c4) sb1 equals sb2: {0}", sb1.Equals(sb2) = true?"true":"false")

MsgBox % sb.ToString()
/*
a1) sb1.Length = 3, sb1.Capacity = 16
a2) sb2.Length = 3, sb2.Capacity = 16
a3) sb1.ToString() = 'abc', sb2.ToString() = 'abc'
a4) sb1 equals sb2: true

Ensure sb1 has a capacity of at least 50 characters.

b1) sb1.Length = 3, sb1.Capacity = 50
b2) sb2.Length = 3, sb2.Capacity = 16
b3) sb1.ToString() = 'abc', sb2.ToString() = 'abc'
b4) sb1 equals sb2: false

Set the length of sb1 to zero.
Set the capacity of sb2 to 51 characters.

c1) sb1.Length = 0, sb1.Capacity = 50
c2) sb2.Length = 3, sb2.Capacity = 51
c3) sb1.ToString() = '', sb2.ToString() = 'abc'
c4) sb1 equals sb2: false
*/