Ask a questionAsk a question
 

QuestionI don't understand where to put this sample code

  • Wednesday, November 04, 2009 9:35 PMCamenwolf Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The following is sample code from MSDN, located here: http://msdn.microsoft.com/en-us/library/system.enum.aspx#

    I don't understand how I'm supposed to run this code.  I'm continuously finding this to be the case, so I must be overlooking some instructions or some common knowledge somewhere.

    If I create a VB console application and paste it in the default module I get an error about not having a Sub Main in the module.  I can add a Sub Main but then I don't know what to put in it.  I created a sub main and instantiated the class then ran it like <instance>.main, and in this case that seemed to work (although it immediately closed the window because I don't know how to pause a VB Console application), but still it seems that there must be some straightforward way of knowing where to paste sample code and how to run it.

    Can someone give me some guidance?



    Public Class EnumTest
        Enum Days
            Saturday
            Sunday
            Monday
            Tuesday
            Wednesday
            Thursday
            Friday
        End Enum

        Enum BoilingPoints
            Celsius = 100
            Fahrenheit = 212
        End Enum

        <FlagsAttribute()> _
        Enum Colors
            Red = 1
            Green = 2
            Blue = 4
            Yellow = 8
        End Enum

        Public Shared Sub Main()
            Dim weekdays As Type = GetType(Days)
            Dim boiling As Type = GetType(BoilingPoints)

            Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:")

            Dim s As String
            For Each s In  [Enum].GetNames(weekdays)
                Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(weekdays, [Enum].Parse(weekdays, s), "d"))

            Next s
            Console.WriteLine()
            Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.")
            Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:")

            For Each s In  [Enum].GetNames(boiling)
                Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(boiling, [Enum].Parse(boiling, s), "d"))
            Next s

            Dim myColors As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
            Console.WriteLine()
            Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors)
        End Sub
    End Class

All Replies

  • Friday, November 06, 2009 5:19 PMR Petrusha - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Basically, when you create a console application from the Console Application template, the Visual Basic compiler looks for a module named Module1 that is supposed to contain Main, the application entry point. In this example, the class that contains Main is named EnumTest. So if you copy the example and paste it to replace all of the original boilerplate code in the code module, you can get the code to compile by doing the following:

    1. Right-click on the project in the Solution Explorer window and select Properties.
    2. In the project Properties dialog, make sure that the Application tab is selected.
    3. In the Startup object drop-down list, change the selected value from Module1 to Sub Main.

    The example should now compile.

    I hope that this helps.

    --Ron Petrusha
      Developer Division User Education
      Microsoft Corporation