3 Replies - 19054 Views - Last Post: 22 July 2010 - 06:11 PM Rate Topic: -----

#1 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Introduction to Console (vb8)

Posted 06 January 2007 - 08:15 PM

[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.
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:
Posted 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.
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.

Is This A Good Question/Topic? 2

Replies To: Introduction to Console (vb8)

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Introduction to Console (vb8)

Posted 06 January 2007 - 11:04 PM

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 :)
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

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

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Introduction to Console (vb8)

Posted 07 January 2007 - 09:39 PM

Neat. I've always wondered how Console Apps are written in .NET
Good Work :)
Was This Post Helpful? 0
  • +
  • -

#4 mosak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-July 10

Re: Introduction to Console (vb8)

Posted 22 July 2010 - 06:11 PM

nice 1 hehe!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1