views:

167

answers:

3

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;
                ColorList.AddRange(Enum.GetValues(enClr.GetType()));

            }

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 _
(enClr.GetType()))
DropDownList1.DataSource = clrs
DropDownList1.DataBind()
+1  A: 

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));
    DropDownList1.DataBind();
}

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

Joel Coehoorn
Don't they mean the exact same thing?
Sergio Tapia
No. The vb requires IsPostBack to be false. The C# requires it to be NOT false.
Joel Coehoorn
No. If not equal false means it is equal to true. That's why if(!IsPostBack) is preferred...or reads better 'is not postback'.
kenny
@Joel, your last C# sample won't compile because it would attempt to convert System.Array to IEnumerable<KnownColor> for the AddRange call.
JaredPar
Yeah, fixed that. I wanted to get rid of the list anyway- it was just extra.
Joel Coehoorn
A: 

Looks to me like you aren't instantiating your list object.

System.Collections.Generic.List<System.Drawing.KnownColor> ColorList;

Should be

System.Collections.Generic.List<System.Drawing.KnownColor> ColorList;
ColorList = New System.Collections.Generic.List<System.Drawing.KnownColor>();
Kibbee
A: 

Seems like i need some text up here... I think one of these will suit what you're looking to do and I agree with the others about the if (!this.IsPostBack)...

if (!this.IsPostBack)
{
  //with LINQ
  System.Collections.Generic.List<System.Drawing.KnownColor> ColorList = new List<System.Drawing.KnownColor>();
  ColorList.AddRange(((System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor))).ToList());

  //with LINQ (more explicit)
  ColorList = new List<System.Drawing.KnownColor>();
  System.Drawing.KnownColor[] colors = (System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor));
  ColorList.AddRange(colors.ToList());

  //without
  ColorList = new List<System.Drawing.KnownColor>();
  colors = (System.Drawing.KnownColor[])System.Enum.GetValues(typeof(System.Drawing.KnownColor));
  foreach (System.Drawing.KnownColor color in colors)
  {
    ColorList.Add(color);
  }
}
dovholuk