0

Say I have a list of stuffs that will never change.

Is there a way to make it constant?

Something like

Const NEVERTOOHIGH As String() = {"BTC", "BLR", "BIDR", "VAI", "IDRT", "USDT", "USD", "BUSD"}

I can't do this and this will cause compile errors because constant cannot be array

Basically I want the "array" to be accessed everywhere but I want it to be constant. I don't want anyone to change the content of the array or change the array.

Is it possible in vb.net?

In objective-c we can have constant pointer to immutable array. What's in vb.net?

After reading a lot, I can see that the right way to do it would be something like

Public Shared ReadOnly NEVERTOOHIGH As IReadOnlyList(Of String) = {"BTC", "BLR", "BIDR", "VAI", "IDRT", "USDT", "USD", "BUSD", "BRL", "AUD", "ETH", "LTC", "USDC", "UST", "USC", "DAI", "PAX", "EUR", "USDJ", "TUSD"}

Basically I add readonly keywords so NEVERTOOHIGH, even though public, cannot be changed to another list. Also, no one outside my class can change it because I suppose ireadonlylist don't have members like add or remove.

The thing is, I saw so many alternatives. There is IReadOnlyCollection and so on and so on.

I am not even sure I am doing this right though it seems good enough.

So how?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • "Anyone" being who.. ? You and... ? – Caius Jard Apr 16 '21 at 16:01
  • This one is for vb.net not c#. Clearly different question. If the answer is similar it is still a different question. – user4951 Apr 24 '21 at 03:53
  • But VB.NET and C# are the same thing? `IReadOnlyCollection` is `IReadOnlyCollection(Of T)`. I recommend you learn how to read C# - it doesn't take much but there are vastly more questions asked and answered about it than VB, and it's probably fair to say that they're of a higher level too. If you're reaching that level of skill where you're wanting to write high level VB and have some question, you're probably more likely to find it being asked and answered in C# so being able to mentally remove semi colons and flip < into ( will help you get the answers you need. (Or use a converter) – Caius Jard Apr 24 '21 at 06:30
  • I think it's better for community to have 2 answers. One for C# and one for vb.net For example, that other answer doesn't say how exactly I should declare nevertoohigh – user4951 Apr 24 '21 at 12:33
  • Okay, I do not know if it's better for the community to have 2 answers, but definitely, this question is different. It's for different language. I think it's up to the community to decide. If you delete this question, you made that decision instead of the community – user4951 Apr 24 '21 at 14:18
  • But it isn't a different language. Python is a different language. C# and VB are both .NET, just different syntaxes. It's like arguing that every answer that's posted in American English should also be reposted in Australian English for the benefit of the Australian community. The lead answer in the linked duplicate *barely even contains any C#*, it just hyperlinks to MSDN which, if you're a VB programmer, will probably already be displaying VB (but if it isn't then in the top right of the articles is a dropdown that changes all MSDN into VB for you - so there's your VB example, in MSDN) – Caius Jard Apr 25 '21 at 06:01
  • got link saying c# and vb.net is the same language? – user4951 Apr 28 '21 at 16:12
  • Actually the right answer is to just add the word readonly – user4951 May 08 '21 at 03:57
  • I modified the question so that it has a bunch of vb.net terms – user4951 May 08 '21 at 04:49
  • *Actually the right answer is to just add the word readonly* - no, because that doesn't meet your stipulation *I don't want anyone to change the content of the array ...* – Caius Jard May 08 '21 at 11:35
  • *After reading a lot I can see that the right way to do it would be something like `Public Shared ReadOnly NEVERTOOHIGH As IReadOnlyList(Of String) = {"BTC"}`* - no, because this stores a `String()` inside an IReadOnlyList(T), but you can easily do `DirectCast(NEVERTOOHIGH, String())(0) = "ETH"` and change the content of the array, which doesn't meet your stipulation *I don't want anyone to change the content of the array* ... – Caius Jard May 08 '21 at 11:42
  • Turn that into an answer. Of course, using directCast means very deliberate attempt to change stuffs. More like hacking. So if you have a better way to do it, by all means, turn that into an answer please. This one is good enough for me in a sense that I do not want anything to change the content of the array semi deliberately. Of course, the all caps variable name is also a tell tale sign not to – user4951 May 08 '21 at 12:37
  • I've heard there is a toImutable method something. I have no idea. This one seems to do my purpose enough – user4951 May 08 '21 at 12:38
  • Public Shared ReadOnly NEVERTOOHIGH As ReadOnlyCollection(Of String) = {"BTC", "BLR", "BIDR", "VAI", "IDRT", "USDT", "USD", "BUSD", "BRL", "AUD", "ETH", "LTC", "USDC", "UST", "USC", "DAI", "PAX", "EUR", "USDJ", "TUSD"}.ToList.AsReadOnly Like this? – user4951 May 08 '21 at 13:03
  • Not hacking at all, it's standard polymorphism, cornerstone of OO. Can't make it an answer; question is closed as a duplicate of the c# one, which links to MSDN, which gives a VB example of how to create a ReadOnlyCollection, which passes a "something that implements IList" (like an array) to ReadOnlyCollections's constructor eg `new ReadOnlyCollection(Of String)({"BTC", "BLR"});`, which in c# would be `new ReadOnlyCollection(new[]{"BTC", "BLR"});`, i.e. virtually identical cos it's same language.. back to my first question, if only you use this array, why bother? Just don't edit it – Caius Jard May 09 '21 at 05:22

0 Answers0