VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
Good job, we have now come to the last part of this tutorial series, hope you have learned a lot so far. I will now teach you about Compiler directives. Compiler directives is called like so because they are "taken care of" before the compiling and they are "gone" when we compile the code compiling.
#Region and #End Region
The first one I will talk about is called #region (together with #end region). This is just to make your code easier to read and helps you to get a better overview. By adding a #region and an #end region tag around a part of the code you can choose to show or hide that region you just created. So if you have a long code that just makes your code long and hard to read you can just create a region for it and then hide it. And if you want to edit anything in that part you just show the region again. When the code later is compiled the #region blocks are just skipped over and doesn't do anything. Look at the example below:
First we have a code with two functions with "lots" of code(they aren't that long here but you understands what I mean):
[ATTACH]2269[/ATTACH]
But these functions are just in the way, we don't want them to be there in the way since we want to write code into our Form's load event. So therefor we group them together in a region called "Some Functions".
[ATTACH]2270[/ATTACH]
So now if we press at the "-" which the arrow is pointing at, it will now look like this:
[ATTACH]2271[/ATTACH]
#Const and #If statements
The #Constants and #If statements is working as the normal Constants and If statements but before the compiling instead when the program is actually used. The reason we wants to use them is when we want to make more then one version of our program to, for example, different clients, for different languages, a trial version vs a bought version etc. Of course you can do things like that in other ways but they have its downside. If we use normal if statements and constants all different versions will have the code to all versions even though it doesn't use it all. This will make the program bigger then it have to be and this could also be a security problem. Another way to create different versions could be by simply copy all code and paste it to another project and edit it from there. But what if you later wants to edit something, then you have to edit it in all your versions which also could lead to that you make a typo error in just one version which will cause that to fail.
In this simple example below I'll show you how it works:
Public Class frmMain
#Const Lang = "sv"
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If Lang = "sv" Then
Me.Text = "En Svensk rubrik"
cmdOk.Text = "Okej"
cmdCancel.Text = "Abvryt"
#ElseIf Lang = "no" Then
Me.Text = "En norsk tittel"
cmdOk.Text = "Okay"
cmdCancel.Text = "Avbryt"
#Else
Me.Text = "An English title"
cmdOk.Text = "Ok"
cmdCancel.Text = "Cancel"
#End If
End Sub
End Class
In this case #Lang = "sv" so therefor the code which will be compiled is:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "En Svensk rubrik"
cmdOk.Text = "Okej"
cmdCancel.Text = "Abvryt"
End Sub
End Class
And then if we want to compile the program in Norwegian we just change #Lang to "no", the this will be compiled:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "En norsk tittel"
cmdOk.Text = "Okay"
cmdCancel.Text = "Avbryt"
End Sub
End Class
If #Lang is either "sv" or "no", the program will be compiled in english:
Public Class frmMain
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "An English title"
cmdOk.Text = "Ok"
cmdCancel.Text = "Cancel"
End Sub
End Class
In the above example we made the program compile only the necessary code. If we wants the program in English there's no idea to have the Swedish and Norwegian parts to so they are just removed before the compiling. You can probably imagine of useful this is if you want to make more then one version of some bigger programs. For example if you want to remove some features in a trial version. The trial version will take less space and you don't have to worry about someone hacking the trial version to get the full version since the required info for the full version isn't included in the trial version at all.
And that's it, we have now come to the end of this 21 parts long tutorial. Before this last part really ends I want to say a few more things.
Many programs have secret things in them, it's always fun to add something fun to your program, for example if you're doing a game you can add a completely weird bonus level or if you're doing a text editor you can do so if the user presses a special key combination a very long strange text appear. All for the fun. So why am I even mention this at all? Well it could be because I've added a secret thing in this tutorial series. What? Haven't you seen it? You maybe have noticed that the intro of each part(the first sentence after the list of all parts) have been kinda weird sometimes, you'll probably understands the reason if you reads the first letter in the intro in each 21 parts beginning at part 1 all the way to part 21.
Have you found the secret message now? And you didn't have an idea of it when you first read the tutorial. Isn't it fun with some secret stuff? Well, I'll really hope you've enjoyed this long tutorial and I also really hope you've learned a lot of new things and I wish you good luck with you're programming. If there's something missing or something you want to know, just drop me a comment or send me a PM. Have fun coding everyone.
Attached Files
Edited by Vswe, 21 March 2010 - 02:40 PM.


Sign In
Create Account



Back to top









