When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Sunday, January 27, 2002

From VB.NET to C# and Back Again, Part 2
By Darren Neimke and Scott Mitchell


  • Read Part 1

  • In Part 1 we examined some of the primary differences between the syntax of VB.NET and C#. Additionally, we looked at how to two languages declared variables differently. In this part we'll look at each language's use of operators, arrays, and functions!

    - continued -

    Examining the Operators
    VB.NET and C# have roughly the same set of operators. For example, to perform addition between two numbers, use the + operator in either language. The one major difference is the string concatenation operator. VB.NET uses the ampersand (&), while C# uses the +. Both languages also have a short-hand notation for this - &= and +=, which takes a string on the left hand side and concatenates the string on the right hand side. That is, if we had:

    // C# example...
    someString += someOtherString;

    ' VB.NET example...
    someString &= someOtherString

    at the conclusion, someString would equal whatever value it equaled before we reached the line shown above, concatenated with the value of someOtherString More clearly, if someString initially equaled "Scott and someOtherString equaled "Mitchell", at the conclusion of the above line, someString would equal "Scott Mitchell".

    Arrays in VB.NET and C#
    Arrays in VB.NET and C# are created and managed a bit differently. First of all, to index an element in an array in VB.NET, you use parenthesis, as in the following example:

    arrayVariable(index)

    In C#, however, square brackets are used, like so:

    arrayVariable[index]

    When creating arrays in VB.NET, the syntax used is, essentially, the syntax used to create any variable. If you wanted to create an array named ages with 0 to n Integer elements, you'd do:

    Dim ages(n) as Integer

    Again, note that this would create a total of n + 1 elements (0 .. n). In C#, the array declaration is a bit different:

    int [] ages = new int[n];

    Note that we place the [] before the variable name; this indicates that ages is going to be an array. Next, we have to allocate n ints by saying = new int[n]. Note that this creates a total of n elements, elements indexed from 0 to n - 1! This is different than in VB.NET, which creates a total of one extra element when compared to the way C# does it.

    Creating and Using Functions and Procedures
    In VB.NET there are two classes of functions: those that return values and those that don't. Those functions that don't return values are called subroutines, and are created a bit differently than functions. In VB.NET, to create a function, which returns a value, use the following syntax:

    Function FunctionName(arg1 as Type, ... argN as Type) as ReturnType
        ...
        Return someVariableOfReturnType
    End Function

    Note that when creating a function you must specify a return type at the end of the first line of the function declaration. Then, somewhere in the body of the function (usually at the end), you must return a value of the proper ReturnType. Functions, of course, can take zero to many input parameters, arg1 through argN, each with their own Type. A subroutine (or procedure, as it's sometimes called), is defined a bit differently:

    Sub ProcedureName(arg1 as Type, ... argN as Type)
        ...
    End Sub

    Note that the Sub keyword is used instead of the Function keyword, and that no value need be returned.

    In C#, to create either a function or a procedure (a function that doesn't return a value) you must use the following syntax:

    ReturnType FunctionName(Type arg1, ... Type argN)
    {
        ...
        return someVariableOfReturnType
    }

    In C# if you don't want the function to return a value, specify the ReturnType as void. Note that the function's body is delimited by the curly braces ({ ... }) and that, as with the VB.NET function, we must have a return statement, returning a variable of the proper type (with functions whose ReturnType is void no return is necessary). Also note that in the function arguments the Type comes before the arg name, just like when declaring variables in C#.

    Casting
    While it may help to think of a variable as nothing but a simple value, a variable can be formally defined by a number of properties, such as its value, scope, lifetime, and type. The type of a variable determines the set of legal values that it can be assigned; for example, a variable of type integer (Int32) can be assigned values from -231..231-1. When you attempt to assign two variables of unequivalent types, say assigning a string to an integer, a process called casting must occur.

    Essentially, casting is the process of converting a variable from one type to another (from a string to an integer in our previous example). Casting comes in two flavors: implicit and explicit. Implicit casting, as the name implies, happens automatically; with explicit casting, extra syntax must be used to specify that a cast should occur. VB.NET allows for a lot of implicit casting, while C# requires casts to be explicit. For example, if we wanted to have an ArrayList of Integer values, and then wanted to work with a particular values, we need to use explicit casting in C#:

    ArrayList ages = new ArrayList();
    ages.Add(23);
    int x = ages[0]; // ERROR: ages[0] returns an object - need to cast to int

    However, the above code (in VB.NET syntax, of course) will work fine. To cast in C#, simply place a (DesiredType) section in front of the variable you wish to cast. To make the above code compile, we need to cast s to an int:

    ArrayList ages = new ArrayList();
    ages.Add(23);
    int x = (int) ages[0]; // No error due to explicit cast to int

    Not all types can be casted to other types. If you attempt to illegally cast a type, you will get an error message informing you that the cast is illegal. In VB.NET, explicit casting can be used via the CType function. The syntax is as follows:

    VariableOfTypeConvertToType = CType(ObjectToCast, ConvertToType)

    Now that we've examined the fundamental differences between VB.NET and C#, let's examine an actual exercise of translating an ASP.NET Web page from C# to VB.NET. We'll do this in Part 3.

  • Read Part 3!


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Win Server ‘08
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES