C# – Trouble getting an Enum to cooperate with me


I have this problem with my code, I get an exception during compilation. Can anyone help me out?

if (Page.IsPostBack != false)
                System.Drawing.KnownColor enClr;
                System.Collections.Generic.List<System.Drawing.KnownColor> ColorList;


I'm trying to follow this guide in VB.Net, but I only use C# so I'm trying to translate as I go, can anyone help?

Here's the original code:

If Not IsPostBack Then
Dim enClr As System.Drawing.KnownColor
Dim clrs As New  _
System.Collections.Generic.List _
(Of System.Drawing.KnownColor)
clrs.AddRange(System.Enum.GetValues _
DropDownList1.DataSource = clrs

Best Solution

First of all it's confusing which direction you're trying to translate. The tags say C# to VB, but the text says VB to C#. I'm assuming the latter. With that in mind, this:

If Not IsPostBack Then

and this:

if (Page.IsPostBack != false)

mean the exact opposite. Your C# should look like this:

if (!IsPostBack)

You also need to pay closure attention to the word "New" in the vb code. A full adaption looks like this:

if (!IsPostBack)
    DropDownList1.DataSource = System.Enum.GetValues(typeof (System.Drawing.KnownColor));

Finally, one more correction in your terminology: compile time errors are not exceptions. Excpetions are a run time construct.