Define TextEncoding instance as constant - possible?

Hello,

i have a method within a class that is called repeatedly by a loop. Is there a way to initiate the following declaration only once to save some memory? Unfortunately I cannot define TextEncoding as a constant.

Dim encoding As TextEncoding = Encodings.UTF8

Thank you.

You can do a property or a static.

@Martin Trippensee — On my 7-year old iMac, that line of code take 0.2 microsecond to execute so you really shouldn’t bother. Also, it will not save memory if you somehow get rid of it.

In some other situations, you may want to consider Static as a replacement for Dim, as it will be executed only once. For example

Static value as integer = myTimeExpensiveMethod()

But as a rule of thumb, don’t try to optimize too much unless it becomes necessary.

I just checked: encodings.UTF8 is a function which always returns the same UTF-8 text encoding object.
So it’s probably created on first time, cached and returned when queried. So that function should be very fast.

Thanks to all members. I had the same feeling as Christian.

[quote=468863:@Martin Trippensee]Hello,

i have a method within a class that is called repeatedly by a loop. Is there a way to initiate the following declaration only once to save some memory? Unfortunately I cannot define TextEncoding as a constant.

Dim encoding As TextEncoding = Encodings.UTF8

Thank you.[/quote]
Constant cannot be aything like a class instance since when do you construct it ?
Properties can be but your responsible for properly getting it constructed & initialized
If what you want is the effect of a constant then a computed property that has just a getter that, the first time its called creates an instance, or uses a static instance, then that would be a decent way to go since it still behaves like a const in that its not changeable like a bare property would be

[quote=468933:@Norman Palardy]Constant cannot be aything like a class instance since when do you construct it ?
Properties can be but your responsible for properly getting it constructed & initialized
If what you want is the effect of a constant then a computed property that has just a getter that, the first time its called creates an instance, or uses a static instance, then that would be a decent way to go since it still behaves like a const in that its not changeable like a bare property would be[/quote]
So you suggest a (shared) computed property with a getter only:

[code]Static enc As TextEncoding

If enc = Nil Then
enc = Encodings.UTF8
End If

Return enc[/code]

something like that
I use this kind of “initialize on first access” pattern for things a fair bit

And you still have the right characteristics in that is not mutable - just like a constant would be - and because it IS a computed property you can shove break points in there to see accesses when you need to etc

So, I just tested both variants, the computed property and as a local variable directly in the method. Using the local variable is actually much faster. I did not expect this, but in my case it is. So I will continue to use the local variant.

if you only use this in one spot that makes sense
if you use it application wide and want to make it so you dont have to track down many instances of the one line local the computed, while a bit slower, would make that simpler as you change the one line in the getter and they all change