Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Enum in select case statement

I have a enum with many items which I want to implement in select case statement of VB.NET as we do in c#.net.

Like In C#.net we just type switch then hit Tab key and type enum variable name and hit enter key all items of enum automaticaly impmeneted in case statement. I am looking for same way in VB.net so I do not need to type all cases manualy.

Is it possible in vb.net?

like image 781
Neeraj Kumar Gupta Avatar asked Dec 19 '13 09:12

Neeraj Kumar Gupta


People also ask

How we can use an enum in a switch-case statement?

An Enum keyword can be used with if statement, switch statement, iteration, etc. enum constants are public, static, and final by default. enum constants are accessed using dot syntax. An enum class can have attributes and methods, in addition to constants.

Can enum be checked in switch-case statement?

Can enum be checked in a switch-case statement? Yes. Enum can be checked. As an integer value is used in enum.

Can enum be implemented?

Yes, Enum implements an interface in Java, it can be useful when we need to implement some business logic that is tightly coupled with a discriminatory property of a given object or class. An Enum is a special datatype which is added in Java 1.5 version.

Can we use .equals for enum?

equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.


1 Answers

Unfortunately there is no way to do this built in. Looking at the C# snippet for switch, you can see that it performs a built-in function to get the case statements:

<Literal Editable="false">
    <ID>cases</ID>
    <Function>GenerateSwitchCases($expression$)</Function>
    <Default>default:</Default>
</Literal>

However, the VB snippet, just defines a couple of Case Literals:

<Literal>
  <ID>Case1</ID>
  <Type></Type>
  <ToolTip>Replace with a valid value of the expression.</ToolTip>
  <Default>1</Default>
</Literal>
<Literal>
  <ID>Case2</ID>
  <Type></Type>
  <ToolTip>Replace with another valid value of the expression.</ToolTip>
  <Default>2</Default>
</Literal>

Unfortunately you cannot define custom functions to use inside the Snippets, so you are only left with the Default ones, and GenerateSwitchCases appears to not work in VB. That means that you cannot even define your own Select Case snippet that will perform the same value.

I've tried using the below, but it just doesn't seem to want to perform the evaluation. In any case, I suspect we'd end up with : at the end of every case (which is C# notation).

<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Select Case Statement</Title>
      <Author>Microsoft Corporation</Author>
      <Description>Inserts a Select Case statement.</Description>
      <Shortcut>NewSelect</Shortcut>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>   
      <Imports>
      </Imports>
      <Declarations>
        <Literal>
            <ID>expression</ID>
            <ToolTip>Expression to switch on</ToolTip>
            <Default>switch_on</Default>
        </Literal>
        <Literal Editable="false">
            <ID>cases</ID>
            <Function>GenerateSwitchCases($expression$)</Function>
            <Default>Case Else</Default>
        </Literal>
      </Declarations>
      <Code Language="VB" Kind="method body"><![CDATA[Select Case $expression$
    $Cases$

End Select]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Checking out MSDN, you can see that there are only a couple of Snippet functions, and they are all C# only.

like image 106
Obsidian Phoenix Avatar answered Sep 18 '22 21:09

Obsidian Phoenix