Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are VBA strings immutable?

Tags:

string

excel

vba

I know .NET (and therefore VB.NET) strings are immutable. However, I'm using VBA 7.0 in Excel 2010. Are strings there immutable? I'm doing a lot of string processing and for small quantities, some (direct) string manipulation is fine, but I'm worried it won't scale - since every additional character moved from one string to another might create yet another instance of the string.

like image 710
PaoloFCantoni Avatar asked Feb 10 '14 05:02

PaoloFCantoni


People also ask

Are strings immutable in VB net?

Strings are not really immutable. They are just publicly immutable. It means you cannot modify them from their public interface.

How long can a VBA string be?

Variable length Thus, the maximum string length that can be handled by VBA is 2,147,483,647 characters.

What is a VBA string?

Advertisements. Strings are a sequence of characters, which can consist of either alphabets, numbers, special characters, or all of them. A variable is said to be a string if it is enclosed within double quotes " ".

Which language string is immutable?

In Java, C#, JavaScript, Python and Go, strings are immutable.


1 Answers

While VB.NET strings are immutable, as mandated by System.String, VBA (including VB6?) strings can be mutated (such as with Mid$). See Alex K's answer and note the StrPtr result after the operations.


Original answer; supported by documentation, in opposition to a counter-example.

VBA strings are immutable.

Just as with VB.NET, there is no way to "replace part of" or "append to" a string without creating a new string. Whether or not this matters - as modern computers are pretty darn fast - depends on the actual algorithm, data, and environment.


Unlike .NET documentation, such a behavior reference for VBA is [becoming] difficult to track down. From MS-VBAL: 2.1 Data Values and Value Types, we find this rare little gem

Individual data values are immutable. This means that there are no defined mechanisms available within a VBA Environment that can cause a data value to change into another data value.

where Strings represent "individual data values".

like image 110
user2864740 Avatar answered Sep 30 '22 21:09

user2864740