converting this code form to C#

Discussion in 'ASP .Net' started by Jeff, Jan 12, 2009.

  1. Jeff

    Jeff Guest


    ..Net 3.5

    Below is some code I'm trying to understand and convert to c#. Okay
    the 2 first lines are okay. but the last 2 lines I have trouble with. For me
    it looks like an if test,

    TextBox1.Text = Calendar1.SelectedDate.ToShortDateString()
    Dim div As System.Web.UI.Control = Page.FindControl("divCalendar")

    If TypeOf div Is HtmlGenericControl Then
    CType(div, HtmlGenericControl).Style.Add("display", "none")
    End If

    I converted the if test to this:
    if (typeof(div) == HtmlGenericControl)

    not sure that is correct translation

    and another thing that bugs my mind is that in the code AFAIK, the if
    test if div is of type HtmlGenericControl. okay but the code inside the if
    test tryes to covert to HtmlGenericControl. don't see how a covert is needed
    there... I'm confused here

    any suggestions?
    Jeff, Jan 12, 2009
    1. Advertisements

  2. Jeff

    bruce barker Guest

    vb is a typed language like c#. As div is declared as a Control (base
    class), then if it is type HtmlGenericControl it can be cast as
    HtmlGenericControl, to call methods/props.

    the c# code is:

    if (div is HtmlGenericControl)
    ((HtmlGenericControl) div).Style.Add("display", "none");

    -- bruce (
    bruce barker, Jan 12, 2009
    1. Advertisements

  3. Jeff

    David Anton Guest

    (See Bruce's answer)
    The problem is keyword overlap - the same keywords (such as TypeOf/typeof)
    have different meanings in VB and C#.
    VB: C#:
    TypeOf x Is Foo x is Foo
    x Is Foo x == Foo
    GetType(x) typeof(x)
    C++ to C#
    C++ to VB
    C++ to Java
    VB & C# to Java
    Java to VB & C#
    Instant C#: VB to C#
    Instant VB: C# to VB
    Instant C++: VB, C#, or Java to C++
    David Anton, Jan 13, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.