1

Is there a simple way to determine if a string (i.e. "Dim") is a keyword for VB.NET.

I could create a list of keywords, and loop through them, but I was hoping for something more reliable.

1 Answers1

3

There is no built-in way to do this, so you have to maintain your own list.

Depending on how you intend to use it, a HashSet(Of T) could be used to provide better performance than a List(Of T) or an array.

Microsoft has got a list of VB.NET's Keywords here.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • I personally think an array would be better for this sort of thing - a constant defined in the class as it would be a fixed array and it wouldn't change, supposed, for what they need. Despite what I mentioned in the comment on the question. – Ally Mar 15 '17 at 09:45
  • 1
    @AlexM. : However the issue with an array or a `List(Of T)` is that to check if an object exists you would have to iterate the list (which `Contains()` does). This causes the operation to be `O(n)`, whereas in a `HashSet(Of T)` you calculate the hash of the object and check if it exists (which has the operation `O(1)`). – Visual Vincent Mar 15 '17 at 09:49
  • Ah okay, that makes sense. I'll have to look into `HashSet`, I suppose. – Ally Mar 15 '17 at 09:52
  • 3
    @AlexM. : Some interesting posts: [**\[ 1 \]**](http://stackoverflow.com/questions/6391738/what-is-the-difference-between-hashsett-and-listt), [**\[ 2 \]**](http://stackoverflow.com/questions/150750/hashset-vs-list-performance), [**\[ 3 \]**](http://stackoverflow.com/a/4558802/3740093). – Visual Vincent Mar 15 '17 at 09:54