Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 132,423 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,460 people online right now. Registration is fast and FREE... Join Now!




Introduction to Console (vb8)

 
Reply to this topicStart new topic

Introduction to Console (vb8)

m2s87
post 6 Jan, 2007 - 07:15 PM
Post #1


D.I.C Regular

Group Icon
Joined: 28 Nov, 2006
Posts: 390



Thanked 1 times

Dream Kudos: 1225
My Contributions


[Preliminaries]
This tutorial uses vb 8, if you do not have it, you can download it here.

[Intro]
Console is quite a new feature in VB. It's support was mainly added to be more compatible with C#, this evolves some nice new features.

Never before has writing console project in vb been as easy as it is now. Even now VB classic does not have direct support for console, and one has to write his own using several API calls, but its possible.

Console is in a convenient form, meaning one could run the program from command line or just use it as an executable.

[Example]
Lets write a simple console application example, that will use Pythagoras' theorem to find hypotenuse, parameters will be a and b. Code is intentional not written in English, so one could pay attention on how formating and output is being managed.
CODE
Module Module1

    Sub Main()
        Dim tekst As String = "Sisesta kolmnurga külg "
        Dim t$ = "{0}Külgede {1} ja {2} korral, on täisnurkse kolmnurga hüpotenuus {3}"

        Call info()
        Console.Write("{0}a: ", tekst) : Dim a As Double = Console.ReadLine()
        Console.Write("{0}b: ", tekst) : Dim b As Double = Console.ReadLine()
        Console.Write(t, Chr(10), a, b, Math.Sqrt(a ^ 2 + b ^ 2))

        Console.ReadLine()
    End Sub

    Sub info()
        Dim lisa As New String(" ", Console.WindowWidth)
        Dim triip As New String("_", Console.WindowWidth)
        Dim tekst As String = "{2}Pythagorase teoreem:" & _
        New String(" ", 60) & "{3}Programm arvutab välja täisnurkse kolmnurga hüpotenuusi," & _
        New String(" ", 20) & "{3}kui talle anda parameetritena kolmnurga küljed a ja b." & _
        New String(" ", 22) & "{1}"

        Console.BackgroundColor = ConsoleColor.White
        Console.ForegroundColor = ConsoleColor.Black
        Console.WriteLine(tekst, Chr(10), triip, lisa, "    ")
        Console.ForegroundColor = ConsoleColor.Gray
        Console.BackgroundColor = ConsoleColor.Black
    End Sub

End Module

If you open cmd and run the compiled version of this code one would see something like:
IPB Image

As basic is a language that could be understood just by natural reading, it might not need any explaining. But to be certain that one did not misunderstood anything, following is to be noted:

1] functions console.writeline() and console.write() have only one laconic difference - console.writeline() ends the line.
2] Just like printf(); in C, console.write() and console.writeline() have a string substitution function built in. This is a fancy feature that one do not need to use, but should understand how it works.
3] New String(" ", x) makes a string by adding " " substrings together x times.
4] with function Console.BackgroundColor you can change the background of the text and with the function Console.ForegroundColor you can change the color of the text in console.
5] Console.WriteLine(tekst, Chr(10), triip, lisa, " ") would be same as to write Console.WriteLine(lisa & "Pythagorase teoreem:" & _
New String(" ", 60) & " " & "Programm arvutab välja täisnurkse kolmnurga hüpotenuusi," & _
New String(" ", 20) & " " & "kui talle anda parameetritena kolmnurga küljed a ja b." & _
New String(" ", 22) & triip)
without string substation.
6] Console.ReadLine() at the end of main function waits the user to press enter, so the program could close. While you would use cmd to execute the program, this line is there without a reason. But when used as it was an executable, without that line your program could end prematurely.

[Example 2]
This example makes a clock in console. Clock is updated about every 0,2 second and the hole thing is in infinite loop. Clock time is self defined.
CODE
Module Module1

    Sub Main()
        Do
            System.Threading.Thread.Sleep(200)
            Console.SetCursorPosition(40, 0)
            Console.WriteLine(Now.TimeOfDay)

            Console.SetCursorPosition(40, 1)
            Console.Write(Format$(Now, "dddd, mmmm dd, yyyy"))
        Loop
    End Sub

End Module


[Summary]
Console is a good alternative to any GUI, that does not need graphics. Consoles are also small and easy to read, what makes them perfect for automation scripts and difficult commands.

This tutorial did not cover all the that there is to it. It just gave a hunch what you can expect to know when dealing with consoles.
User is offlineProfile CardPM

Go to the top of the page

William_Wilson
post 6 Jan, 2007 - 10:04 PM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,969



Thanked 15 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


very nice!
VB 8 has added some features which need to be brought to the attention of it's devoted coders. An excellent job explaining all of the code used in your tutorial smile.gif
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 7 Jan, 2007 - 08:39 PM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Neat. I've always wondered how Console Apps are written in .NET
Good Work smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 09:47AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month