Thursday, June 17, 2010

C# vs VB - The debate continues...

Just getting my feet wet with C#, after being a VB developer for awhile, realizing that C# is getting all of the jobs. With that being said, have run into a few difficulties in converting over, and I'll list a couple here, to be added to as I go along. Consider this a newbie's version of headaches you may face when switching to C#...




  1. In VB, if you want to locate an element inside of a control, you would use:

    Dim control as CONTROLTYPE = CType(CONTAINER.FindControl("ControlID"), CONTROLTYPE)


    In C#, you would write the same expression as:

    CONTROLTYPE control = (CONTROLTYPE)CONTAINER.FindControl("ControlID");

  2. In VB, if you want to place an IIf statement inside of a Label Control, you're free to do so. Simply use

    <%#IIf(Convert.ToString(EXPRESSION)=Something, True, False)%>

    However, when you attempt to use this in C#, you will get an error, as IIf isn't recognized.

    Here's the fix:

    <%# (Convert.ToString(EXPRESSION) == Something ? True : False)%>.

  3. Of Course, & is not recognized, it is replaced by +.

  4. I know when I was creating a VB Side HTML Message, or if I was creating a very long string, I would use '& vbCrLf _'. However, in C#, this changes a bit. Declare your String, and append it just as you would in JS. Example?


    string A = "";

    A += "This is a string ";
    A += " and the second line of the string...";



I will modify this as I go, but if you are like me, and have done VB this whole time, I would recommend taking any current sites you have done, and redo them from scratch in C#. Get Hung Up? Use THIS as a last resort, it's a pretty good tool.


Good Luck!

0 comments: