5

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?

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Neeraj Kumar Gupta
  • 2,157
  • 7
  • 30
  • 58
  • you are asking about code snippet equivalent for switch in vb.net.. – Niranjan Singh Dec 19 '13 at 09:22
  • yes @NiranjanKala I am looking for same as we use in c#.net – Neeraj Kumar Gupta Dec 19 '13 at 09:24
  • possible duplicate of [VB.NET Switch Statement GoTo Case](http://stackoverflow.com/questions/820104/vb-net-switch-statement-goto-case) – Nahum Dec 19 '13 at 10:05
  • this is a duplicate of: http://stackoverflow.com/questions/820104/vb-net-switch-statement-goto-case if you REALLY need it. you can try and write extension that will get the type using refletion and generate such a code copy it to clipboard and paste. – Nahum Dec 19 '13 at 10:07
  • Not a dupe - Neeraj is asking for snippet functionality to quickly insert a select case statement from an enum and not how to write a switch case equivalent in vb.net, which your suggested dupe is about. – Marijn Feb 28 '14 at 16:01

1 Answers1

5

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.

Obsidian Phoenix
  • 4,083
  • 1
  • 22
  • 60