Please support our sponsor:
DirectX4VB.Com - All You Need For Multimedia Visual Basic Programming

Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9
      Miscellaneous

 

Learning to Talk the Talk

This part of the tutorial is going to take a closer look at the changes to the programming language(s) that make up part of Visual Studio .Net; The Java component has not been included due to it not having been released at this point in time (should you be reading this in the future!).

Global Changes

The first thing that you'll notice is that all languages share a common library now - the .Net framework. This has been made integral to VB.Net and C#.Net, but is only an addition to the C++.Net language (Microsoft can't change that language, only add to it). The .Net framework is designed to be your only real interaction with the "system", whereas before you would have used the Win32 API calls (although this is still possible). The reasoning behind this is such that your application ends up being designed for the .Net framework, and whilst at the moment that is exclusively a windows platform, should in the future a Linux / Mac version of the frameworks appear your program should require very little alterations to run on the completely different operating system. If you stick to using the Win32 API (as in previous versions of languages) you are automatically restricting yourself to only using systems that have those particular libraries installed (ie, Windows machines only).

Accessing the .Net framework is automatic in most languages, and apart from obvious syntax differences is pretty much identical across all .Net languages. A quick example (in VB.Net code) of some common functions are shown below:

'write some data to the console window:
System.Console.WriteLine("")
'call a standard "DoEvents" operation:
System.Windows.Forms.Application.DoEvents()
'pause the application for 2 seconds
System.Threading.Thread.Sleep(2000)

It isn't very complicated once you get used to it - pretty much all components are part of the "System" object, and as we see above there are different sub catergories (well, there are 100's actually!), for example "System.Console" provides all the functions necessary for outputting/inputting data from a console window (DOS-Prompt style program), "System.Windows.Forms" handles everything to do with controls, forms, the user interface etc...

The only really complicated aspect of this is finding stuff in the first place - It is often a complete pain to find a simple function that you can easily use in a previous version of another language. This gets most annoying when it comes to finding replacement Win32 API calls - as I already said, its better to use the .Net framework than to rely on external library calls. Bare in mind that the API-Text viewer no longer seems to be installed with VB.Net (I only have the VB6 version), so if you're using VB it's much more difficult to find specific API declarations (not a problem in C/C++ though). I spent a good while looking for the "Sleep" API call on the first day - as simple as it seems, all searches in the MSDN help libraries came up with the C++ declaration for the Sleep API call, and not the .Net framework equivalent.

The other annoying thing about using these .Net frameworks, as you may have seen above, is that there is excessive typing to get to a simple function - back to the VB6 example, we used to just type "DoEvents" and it would recognise it... Luckily, once you get used to using namespaces and imports you can get back to this old method:

'Place these lines at the top of the source code file...
Imports System.Console
Imports System.Windows.Forms.Application
Imports System.Threading.Thread

'write some data to the console window:
WriteLine("")
'call a standard "DoEvents" operation:
DoEvents()
'pause the application for 2 seconds
Sleep(2000)

As you can see above, it looks a lot better now :) Once you've placed the relevent "Imports" lines at the top of any given file you can use all of its functions without having to type the full "path". Similiar methods exist in C# and C++. Thankfully, the Intellisense (Discussed earlier) brings up a list (in VB) as soon as you type a "." after the name, so you dont have to learn all the different sub-objects of each library - a bit of educated guess work can still be used!

As I've slightly hinted at above, these .Net frameworks are same in all the languages, so effectively you only need to learn ".Net" and not a different .Net for each connected language, this does give the huge advantage of being able to jump between languages very easily once you get used to the software.

Visual Basic .Net

VB.Net has changed, this is probably nothing new to most of you - news of these changes isn't anything new (they were announced at the beginning of VB.Net's development), and I've already seen plenty of message-board posts discussing the pro's and con's of these developments.

The general idea behind the changes appear to be two-fold. Firstly VB.Net has been brought completely up-to-date with the latest and greatest in Object-Orientated-Programming (OOP), Sure, VB6 had objects, classes and so on... but that is nothing in comparison with the fire-power we now possess. Secondly, VB has been brought into line with the other languages that make up the .Net family; this is probably just for convenience as well as being a requirement of sharing the same compiler and the same IDE.

Object Orientated Programming is an amazingly complex area when taking to its full extent (you can get some rather large books about it), but at a simpler level it's really not that hard to get used to - and once you've really gotten used to it, you really start to like it. There are many arguments that OOP programming is slower and inferior to "traditional" methods, but I discard the majority of these comments. If you overuse OOP methods and use them in the wrong place/context then of course it'll be slower - thats just simple logic! However, if you use them in the right place at the right time you're fine - and the comparative "traditional" code is much more complicated and prone to errors anyway.

As a quick run-through of all the new/great OOP features:

1. Inheritance - This is where you can derive a class from a "base" class, or several other classes. This allows you to take a basic class and improve it and/or customize it without having to completely rewrite everything. This alone is exceptionally powerful, with respect to gaming/multimedia development it is brilliant. When writing multimedia applications/games in C++ before I've always loved using Inheritance to simplify certain features, and now that we have it in VB I'm really quite happy! As a short example, see this code output: here.

2. Class Constructors - If you've used classes in the VB6 or VB5 language you'll be aware that there is a "Class_Initialize" and a "Class_Terminate" function built in by default. These functions are called when the class is created, and when its destroyed (respectively). Typically they'll be used to initialise internal variables / destroy references etc... They are generally known as Constructors. VB.Net allows you to define your own constructors, and combined with polymorphism (see next entry) they can be extremely powerful indeed. Being able to customise these parts of the class allows you to pass parameters/references and objects to the class such that it can customise it's initialisation process accordingly. See this code ouput for an example

3. Polymorphism - This is an extremely powerful feature of OOP as well. It appears in the form of "Overloading", as you should be aware, declaring a function to take two "long" parameters, then attempting to call it using two "Single" parameters will cause a compilation error (invalid type). Using overloading you can define the same function twice - using the same name, but with different parameters (types and number). Once you've defined all of the different variations on a function you can use it exactly as normal - and the compiler will decide which function to call (based on the number and type of parameters you are using). See this code output for an example.

There are other OOP features that come up here-and-there, but they all tend to be based on, or associated with one of the catergories above. In my opinion, being able to use these features is almost worth the upgrade alone - as you can see in the inheritance example code, it really does simplify how things can work.

However, OOP is not the only change to the VB language, there are numerous other little tweaks and changes that help to speed things up, and make your life easier. Again, a quick run down of the main features:

Structured Exception Handling (SEH) - This is the new error handling, and is effectively the same as how C++ programmers handle errors. An "Error" as we're familiar with is now known as an "Exception", and we use a Try...Catch...End Try code structure to catch all our errors. This takes a bit of getting used to, and I've not decided whether I prefer it or not yet. It is however much more "Solid" - The good old "On Error Resume Next" mentality cant be used now, which forces less sloppy code on your part, and the "On Error Goto..." structure is effectively replaced, which was my favourite method - but arguably it did get a bit messy using lots of labels/Goto commands. See this code example for a comparison between the old and the new methods.

Declaring variables / New variable types - Due to the common runtime / .Net framework we get access to quite alot of new variables and objects, far too many to mention here. The key difference is that some of them have changed! An "Integer" is now a "Short" (16 bit signed variable) and the new "Integer" is effectively the same as the old "Long" (32 bit wide signed variable). This gets a little bit confusing at first, and I've tended to use the Int16,Int32 and Int64 variable types instead (their names indicate how big they are), a "long" in the new language is fairly platform dependent - such that on a 32bit system (as most currently are) it'll be 32 bits wide, however, on the 64 bit servers it'll become 64 bits wide. The other thing I am pleased they included is "unsigned" integers (16,32 and 64bit variations) - particularly useful when dealing with bit-shifting and binary-level manipulation in all forms of multimedia programming. The other new feature with respect to variable declaration is only minor, but is very useful. Previously where we'd have to do "Dim A as Long" and then "A = 10" to declare and initialise our Long to hold the number 10, we can now do "Dim A as Long = 10", which is quite cool (and something C/C++ programmers have had since day-1).

New maths operators - There are 4 new operators of particular interest, all of which inherited from C/C++ programming. They dont add any functionality to the language, just provide some shortcuts for regularly-performed operators. They are: +=, -=, *=, /=. These take the variable on the LHS and add the RHS value to it before storing it back in the LHS variable (A+=6 is the same as A=A+6). See this code example for further examples. Oddly enough, the language hasn't been given the "++" and "--" operators from the C language that are quite useful...

Stricter compiler - The compiler is now pretty much identical to the Visual C++ 6 compiler, in that you select "Build" and you get a window pop-up that gives you a log of how/what it compiled, and any errors are outputted here (before it fails). This way you dont have to go through the rather tedious affair that was VB6 compiling (if you had errors that is!) where it just popped up a message box and aborted compilation. When you get build errors now they are automatically added to the "Task List" so you then have to go about fixing all of the errors and then re-build the solution so that you can actually test it. This may seem like extra hassle, but it is actually a much more fluent and easy way of compiling your programs. As well as these changes, the compiler (and language) enforces much stricter guidelines on code - all functions much have opening and closing brackets (not always necessary in past versions), and all variables must be properly declared.

 

In general, the new language changes are a bit difficult to get used to; it really does feel for the first few days that you are learning a completely new language. I've been using VB since version 4 and consider myself to be a veteran of the language - I was quite disheartened for the first few days of using VB.Net that I had become a complete "newbie" at a language I've been using for more than 5 years now. However, once you cut through the ice and start to get your head around the new features and new rules it doesn't feel so bad. I have to say, apart from using alot of the new pure-code features, I'm still using VB.Net in much the same way as I use VB6, ignoring the .Net framework except for where I have to, and just getting on with work. That in itself shows that you can get away without upgrading "fully" to start with, but over time I'm sure I'll start using the .Net framework to more of it's potential.

Visual C++ .Net

Visual C++ hasn't really changed very much; apart from using the new IDE there are no major surprises. However, this is to be expected - the C++ language is defined by ANSI/ISO guidelines, so Microsoft cant change the language even if they wanted to! They can add additional features/libraries to the language, which is where the .Net framework fits into everything. Getting to work with .Net in C++ isn't very difficult at all, and if you've used it in any of the other languages (quite likely!) then it all looks the same...

Because the only thing that Microsoft can do to the actual language is add features, they've obviously added quite a lot. As will be familiar to any user of Visual C++ 6 there are a large number of project templates and "Application Wizards" which are very useful to have around, particularly when you're just getting started with one of the numerous application types.

Visual C# .Net

C# ("C Sharp") is an interesting addition to the .Net family, Microsoft decided that they needed to create a whole new language for the .Net generation, and this is the end result. The basic outline it seems, was to create a next-generation version of C++ by using C++ style syntax yet having as many benefits from the RAD tools such as VB. In my opinion, it's a bit odd really - Having looked through (and created) a few sample programs it doesn't come across as anything much more than a half-way language between C++ and VB!

As long as you're designing "proper" .Net services and applications then C# is probably the best language to use - it was designed from the ground up to use the Common Language Runtime (CLR) and to make full use of the .Net frameworks. Whilst the general nature of the .Net family means that you can do pretty much anything (for .Net) in any of the languages, it is a far easier approach to use a language that was custom designed for it.

With this in mind, C# probably wont bother many games/multimedia developers - its a perfectly valid language to use, but given the common grounding in VB and C++ there isn't enough to really warrant using C# instead. Should you want to, it's not that hard to learn - anyone with a decent general programming knowledge will be fine, and if you've used VB and C++ for a while then you're set for a rolling start...

Other langauges

The .Net frameworks are designed to be portable across as many platforms as possible, and they're also designed so that any number of languages can be "plugged in" and make use of the services. This is a very interesting feature - it'll allow for many languages to be used fairly easily all from the same place, and all in a similiar way - total freedom of language (as long as you want to use the .Net frameworks of course!).

The Visual Studio.Net help files list the following languages as being "in development", what they'll actually turn out to be like cant be known yet - but you may find a favourite of yours in the list:

COBOL, Perl, Eiffel, Python, Pascal, Mercury, Mondrian, Oberon, Salford FTN95 (Fortran), Smalltalk, Standard ML and Dyalog

 


To continue with the review click here or click here to return to the "The New IDE".

Introduction: Introducing the software, and the aims of this review.
Getting Started With Visual Studio .Net: The installer, version, prices etc...
The new IDE: New things in the Integrated Development Environment, and is it an improvement?
• Learning to Talk the Talk: Learning the new language (C#) and the changes to Visual Basic
Visual Studio .Net in the Real World: Performance and real world capabilities
Conclusion: Summing everything up in a neat way

 

DirectX 4 VB © 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster
This site is hosted by Exhedra Solutions, Inc., the parent company of RentACoder.com and PlanetSourceCode.com