Tuesday, July 13, 2010

Introducing The .Net Framework 4.0

As a Visual Basic 2010 developer, you need to understand the concepts and technology that empower your applications: the Microsoft .NET Framework. The .NET Framework (also simply known as .NET) is the technology that provides the infrastructure for building the next generation’s applications that you will create.
If you upgrade to Visual Basic 2010 from Visual Basic 2008, the main difference that
you notice is that .NET 4.0 is a standalone infrastructure. You may remember that .NET
Framework 3.5 was instead an incremental framework that needed the prior installation
of .NET 2.0 and .NET 3.0. For example, LINQ was part of .NET 3.5 whereas WPF was
part of .NET 3.0 and Windows Forms was part of .NET 2.0. With .NET 4.0 this incremental structure disappears, and all the
frameworks, BCL, and tools are part of the new version.

Writing a C# Program

Now that you’ve spent some time learning what C# is and how it fits into the .NET Framework,
it’s time to get your hands dirty and write some code. You use Visual Studio 2010 (VS) and Visual C# 2010 Express (VCE) throughout this post, so the first thing to do is have a look at some of the basics of these development environments.
VS is an enormous and complicated product, and it can be daunting to first-time users, but usingit to create basic applications can be surprisingly simple. As you start to use VS in this chapter,
you will see that you don’t need to know a huge amount about it to begin playing with C# code.
Later you’ll see some of the more complicated operations that VS can perform, but for now a basic working knowledge is all that is required.
VCE is far simpler for getting started, and in the early stages of this book all the examples are described in the context of this IDE. However, if you prefer, you can use VS instead, and everything will work in more or less the same way. For that reason, you’ll see both IDEs, starting with VS.
After you’ve had a look at the IDEs, you put together two simple applications. You don’t need to worry too much about the code in these for now; you just prove that things work. By working through the application creation procedures in these early examples, they will become second
nature before too long.
The first application you create is a simple console application. Console applications are those
that don’t make use of the graphical windows environment, so you won’t have to worry about buttons, menus, interaction with the mouse pointer, and so on. Instead, you run the application in a command prompt window and interact with it in a much simpler way.
The second application is a Windows Forms application. The look and feel of this is very familiar to Windows users, and (surprisingly) the application doesn’t require much more effort to create. However, the syntax of the code required is more complicated, even though in many cases you don’t actually have to worry about details.
You use both types of application over the next two parts of the book, with slightly more emphasis on console applications at the beginning. The additional flexibility ofWindows applications isn’t necessary when you are learning the C# language, while the simplicity of console applications enables you to concentrate on learning the syntax and not worry about the look and feel of the application.

Creating a Simple Console Application
  1. Create a new console application project by selecting File ➪ New ➪ Project in VS or File ➪ New Project in VCE.
  2. In VS, ensure that the Visual C# node is selected in the
    Installed Templates pane of the window that appears,
    and choose the Console Application project type in
    the middle pane. In VCE, simply
    select Console Application in the Templates pane. In VS, change the Location text box toC:\BegVCSharp\Chapter01 (this directory is created
    automatically if it doesn’t already exist). For both VS and
    VCE, leave the default text in the Name text box (ConsoleApplication1)
    and the other settings as they are.
  3. Click the OK button.
  4. If you are using VCE, after the project is initialized click
    the Save All button on the toolbar or select Save All from
    the File menu, set the Location field to C:\BegVCSharp
    \Chapter01, and click Save.
  5. Once the project is initialized, add the following lines of code to the file displayed in the main window:
    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    // Output text to the screen.Console.WriteLine("The first app in Beginning C# Programming!");
    Console.ReadKey();
    }
    }
    }
  6. Select the Debug ➪ Start Debugging menu item.
  7. Press any key to exit the application (you may need to click on the console window to focus on
    it first).
In VS, the preceding display appears only if the Visual C# Developer Settings are applied, as described earlier in this chapter. For example, with Visual Basic Developer Settings applied, an empty console window is displayed, and the application output appears in a window labeled Immediate. In this case, the Console.ReadKey() code also fails, and you see an error. If you experience this problem, the best solution for working through the examples in this book is to apply the Visual C# Developer Settings — that way, the results you see match the results shown here. If this problem persists, then open the Tools ➪ Options dialog and uncheck the Debugging ➪ Redirect all Output.

Introducing C#

.NET Framework Fundamentals
The .NET Framework is Microsoft’s latest development platform, and is currently
in version 4. It includes a Common Type System (CTS) and Common Language
Runtime (CLR). .NET Framework applications are written using Object Oriented
Programming (OOP) methodology, and usually contain managed code. Memory
management of managed code is handled by the .NET runtime; this includes
garbage collection.

.NET Framework Applications
Applications written using the .NET framework are first compiled into Common Intermediate
Language (CIL). When an application is executed, the Just-In-Time (JIT) compiles this CIL into native code. Applications are compiled and different parts are linked together into assemblies that contain the CIL.

C# Basics
C# is one of the languages included in the .NET Framework. It is an evolution of previous languages such as C++, and can be used to write any number of applications, including both web sites and Windows applications.

Integrated Development Environments (IDEs)
You can use Visual Studio 2010 to write any type of .NET application using C#. You can also use the free, but less powerful, express product range (including Visual C# Developer Express) to create .NET applications in C#. Both of these IDEs work with solutions, which can consist of multiple projects.

Thursday, July 8, 2010

Getting started

Defining basic terms

Application

An application is a collection of objects that work together to accomplish something useful. In VB the application is called a Project. A Project could be a the management of a Video store, the calculation of mortgages, a dating service or the Payroll for 1000 employees ...

Object

An object is a piece of software that has properties and functions that can be manipulated. Whew! You're here so, you must be somewhat familiar with the Windows environment. A window is an object. It has properties: size, color, position on the screen, etc. (The purists among you may want to talk about a class rather than an object but, at this point we just want to keep it simple, and the underlying concept is the same). The window has functions, also called methods, that can be manipulated: change the size, move it around, open it and close it. You do not have to write code to resize a window - you just click and drag. But somebody had to write code at some point. Fortunately for us, when they did they put it all in a nice little package and called it a window object. Now, whenever you need a window in your Project you can make a copy of the window object, change its properties for color or size very easily, and paste it where you want it. Then you can use its built-in methods to open it, close it when you want or resize it whenever necessary. When you create an application using objects and combining them to produce results, you are working in an object-oriented environment.





Event-driven

To produce an application in COBOL, a procedural language, you write COBOL source programs, you compile them into machine code and then you run them via a control interface such as JCL. A program can contain 1000's of lines of source code and could run for hours with no human intervention. In fact, in large installations, a jobstream can consist of a dozen programs, all automatically accepting input from the previous program and producing output for the next. The programmer can be blissfully unaware that the program has run unless something catastrophic happens.







In a VB project, the processes that occur have to be associated with events. An event is something that happens - the user clicks on a button, a form is opened, the result of a calculation is too large. The operation is event-driven because everything that executes does so as the result of some kind of event. The role of the programmer is to anticipate the events and to write the code that will be executed when the event occurs. A VB application is interactive in the sense that the user is constantly interacting with the program. The user inputs a Customer Id, the program checks the Id in the database and immediately brings up the customer's file or displays a message that the particular Id is invalid.









Jumping right in!

Project description

We want to create a Scoreboard for a football game (there it is already!) between the Giants and the Redskins. To begin with the simplest task we will only count the touchdowns and display appropriate messages.

Please note: although we will create a complete functional Project with controls and code and so on, the purpose of this exercise is to show what can be done. In the following lessons we will be explaining scripts and the use of controls in a lot more detail. If you study this example you should be able to relate it to what you already know of programming and judge whether this tutorial will be easy or hard for you to do.

Creating the Project

First thing to do is to create a Directory where you will store all your VB Projects. Call it VBApps, for example. Then start VB. The first screen will ask whether you want to open a new project or an existing one - it's obviously a new one and it will be a Standard EXE. Then, maximize all the windows (it's easier to work with - some of the examples in the tutorial had to be reduced for the sake of the presentation). Now, save your project. It will first ask you to save the form - call it Score.frm - and then the Project - call it Scorebrd.vbp. From now on, do File-->Save Project very, very frequently.





Before you start to build-up the form, it will make it easier if you change the color of the form. Otherwise you will be working with grey controls on a grey background. To change the color, just click anywhere on the form, go to the properties window, find the property called BackColor and change it to the standard Window background (teal) or to any color you want in the palette.

In our first example we will need 6 labels and 2 command buttons. Each one of these objects that you put on a Form is called a control. To get a control you go to the Toolbox, click on the control you want, come back to the Form and click and drag the control to the size and position you want. Position the controls somewhat like in the diagram below.




IMPORTANT NOTE: If this is your first experience with VB, don't be afraid to experiment. This is hands-on stuff! Remember that VB is a Microsoft product, therefore it works with the standard Windows interface. All the functions you know from MS-Office work the same way here: Copy, Cut, Paste, (Ctrl)+(Click), (Shift)+(Click), drag the mouse over a group of controls to select them all, etc. The Undo button is a nice one to keep handy - when you modify a control you can always Undo the change - remember this when you get to the part about aligning the controls, making them all the same size and so on. That part can get tricky. If you accidentally end up in the Code window while palying around, go down a few paragraphs and you will see how to get back to the Form. At this point the worst that can happen is that your Form will get all messed up. So what! You can just scrap it and start over again, but you will have learned something.



Now that we have a bunch of controls on the form, we have to jazz them up a bit. We do this by changing the Properties of the controls in the Properties window. Each control has a whole series of properties, most of which we won't need right now. The ones we do need are:
Alignment = how text aligns in the control
BackColor = choose the color of the background
Caption = the text that will appear in the control
Font = choose the font type and size
ForeColor = choose the color of the text (foreground)
As with all Windows applications, you can select multiple controls with (Ctrl)+(Click) and change a property for all of them at once. For example, if all backgrounds are white, select all controls, change ForeColor to white and all of them are modified. Change your form to look like the one below. Note that you do not have to change the Caption for Label4, Label5 and Label6 and that you can't change the color of the buttons. They insist on being what was called in the old days "IBM grey". Don't forget to save your project often as you go along!








If you Run the application at this point, you should see your Form appear, just the way you created it. However if you click on any of the controls, absolutely nothing happens! There are events that occur; the form opens, a button is clicked, etc. But, there is nothing that tells the form what to do when it sees an event. That is why we have to write code, also called script.





To switch between the Code window and the Form window, use the buttons just over the Project Explorer window (diagram on the left).
Once in the Code window, you have the option of seeing all the code for the Project or the code for one event at a time. Use the buttons in the lower left-hand corner (diagram on the right).
To select the object and the event you wish to code, use the two Listboxes at the top of the Code window. The one on the left for the object and the one on the right for the event. Start with General ... Declarations and then Form ... Load, etc.


At this point you might want to download the sample program and study it. In the following lessons we'll add functionality to the exercice and we'll explain what the code means. But for the moment, a good exercice would be to write part of the code and then try to figure out how to improve certain aspects of the program.

You can download the application here: FootScoreboard.zip






Now we can Run it and see something happen. When the Form loads, it will initialize the fields that we specified in the code.

Now code the Command1 button and Run it to see the result.





For additional resources you might find this site useful: Free Visual Basic 6 tutorials, samples and source code examples.
 
Copyright 2010. Programming Tutorial News.