VB.net to C#

Last post 07-01-2009 3:39 PM by JeffreyABecker. 5 replies.

Sort Posts:

  • VB.net to C#

    07-01-2009, 1:55 PM
    • Member
      518 point Member
    • JsonTerre1
    • Member since 08-16-2004, 7:40 AM
    • Posts 179

    I am in the process of converting some applications to C# by request of a client. I am new to C# so and have been stumped on some things. This is VS 3.5.

    1)

    I have a datarow that has a value in the ID column(Guid) and Title column (String). I am trying to set variables of the same type to the values of the datarow columns like so:

    _id = dr["ID']

    _title = dr["title"]

    i am getting an error when building. Cannot implicitly convert type 'object' to 'Guid'. An explicit conversion exists (are you missing a cast?). Since the type is a uniqueidentifier (guid) from the query would that error converting guid to guid? There are times that the result set returns NULL so i don;t want to cast it. That would cause an error.

     

    2)

    In vb.net you can call a function and pass params in like so:

    dothis( _

    a, _

    b, _

    c)

    in c# it won;t let you. You have to place them in a long string. I find that breaking the params into seperate lines is easier to read when you have some methods that have 20 or 30 params. is there an equivalent to this in c#?

     

    3)

    Is there an equivalent to the WithStatement in c#?

     

    thanks all

    j

     

  • Re: VB.net to C#

    07-01-2009, 2:24 PM
    Answer
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    Try this
    1.
    if (dr["ID"] != null)
            {
                _id = dr["ID"].ToString();
            }
            if (dr["title"] != null)
            {
                 _title = dr["title"].ToString();
            }

    2.replace the _ by +

    dothis(+
                +"a", +
                +"b", +
                +"c");

    3.
    try use using statement

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: VB.net to C#

    07-01-2009, 2:40 PM
    Answer
    • Participant
      1,546 point Participant
    • RemithR
    • Member since 01-19-2009, 7:46 PM
    • Posts 266

    1) use Convert.IsDBNull to check for DBNull

    _id = Convert.IsDBNull(dr["ID"]) ? string.Empty : Convert.ToString(dr["ID"]);

    _title = Convert.IsDBNull(dr["title']) ? string.Empty : Convert.ToString(dr["title']);

    2) use without _

    dothis(
    a,
    b,
    c)

    3) please refer to http://msdn.microsoft.com/en-us/vcsharp/aa336816.aspx

    VB
    With Button1
      .Text = "Hello"
      .BackColor = Color.Blue
    End With

    c#
    Button1.Text = "Hello"
    Button1.BackColor = Color.Blue

  • Re: VB.net to C#

    07-01-2009, 2:48 PM
    • Member
      518 point Member
    • JsonTerre1
    • Member since 08-16-2004, 7:40 AM
    • Posts 179

    How would this work for a guid? Convert.IsDBNull(dr["ID"]) ? string.Empty : Convert.ToString(dr["ID"]);

     

     

    ------

    i got it

     

    _ID  = Convert.IsDBNull(dr["ID"]) ? Guid.Empty : (Guid)dr["ID"];

     

    thanks again

    j

  • Re: VB.net to C#

    07-01-2009, 3:06 PM
    • Member
      518 point Member
    • JsonTerre1
    • Member since 08-16-2004, 7:40 AM
    • Posts 179

     2 more questions on this.

    How do you handle DateTimes with this. You don't want to set a member of type DateTime to a value that is not a datetime. A string can be string.Empty. Do i just set the DateTime to Nothing or is there something else i shoudl do?

     

    j

  • Re: VB.net to C#

    07-01-2009, 3:39 PM
    Answer
    • Star
      14,184 point Star
    • JeffreyABecker
    • Member since 10-04-2004, 4:27 AM
    • Philadelphia, PA
    • Posts 2,903

    1)

    C# requires that you explicitly convert types for everything but converions to base classes.  The error you're getting is the compiler telling you that you've violated this rule.  

    If the DataSet can return null, what should the value of your _id field be when it does? (as an asside why are you ever having an ID that is nullable?)  If you need to store a null in there, I suggest declaring it as a Nullable<Guid> rather than just a Guid.  In this case you can reference System.Data.DatasetExtensions in your project and use the following:

    _id = dr.Field<Nullable<Guid>>("ID");
    _title = dr.Field<string>("title");
     

    2)
    do as such
        dothis(
            a,
            b,
            c);
    Since linefeeds arent statement terminators in C#, you dont need any special syntax for continuing a statement on the next line.  the compiler sees a linefeed just like it sees a space.

    3) No, but many of the uses of with statements can be simulated using the object initializer syntax

Page 1 of 1 (6 items)