Programming Elements Supported Differently

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET


Several programming elements are supported differently in Visual Basic .NET than they were in VB6. This section highlights existing functions and what-nots that are still supported, but some fundamental aspect of their behavior or value has been modified for .NET.

Calendar

The Calendar class is an abstract class defined in the System.Globalization namespace. There are several subclasses derived from Calendar, supporting current and historic calendars like the Gregorian calendar.

The Calendar class contains methods for managing time, including adding increments of time, cultural information, and functions like IsLeapYear. The following example creates an example of a JulianCalendar, used in the West and in Western Europe, and asks whether 1966 is a leap year.

 Dim Calendar As New System.Globalization.JulianCalendar() MessageBox.Show(Calendar.IsLeapYear(1966)) 

The fragment uses the fully qualified namespace because the sample program did not import the System.Globalization namespace. The MessageBox.Show method displays False; 1966 was not a leap year.

Date

Several aspects of dates have changed in Visual Basic .NET. The Date function has been replaced with the Today function for getting and setting the system date.

Date types are no longer implemented as Double precision numbers where the whole number represents the date and the fraction represents the time. In Visual Basic .NET, date and time values are implemented as a ValueType structure. You can use the procedure shared method Convert.ToDouble to convert a DateTime value in Visual Basic .NET to a Double and existing Visual Basic for Applications (VBA) double-dates can be converted to Visual Basic .NET DateTime values by calling the Shared method FromOADate. (FromOADate is shorthand for From Office Automation Date, or the old double-style date still used in VBA.)

Date$ has been replaced by DateString in Visual Basic .NET. DateString can be used to get the system date as a string or set the system date from a string.

Empty Has Been Replaced with Nothing

Visual Basic 6 supported the notion of null values with Empty, Null, "", and Nothing. Fewer variations representing nothingness exist in Visual Basic .NET; Empty and Null have been replaced with Nothing. Where you would have used Empty and Null in VB6, replace them with a comparison to Nothing.

The IsEmpty and IsNull functions are not supported in Visual Basic .NET.

Now

Now and Timer are ReadOnly Shared properties in Visual Basic .NET that return the current date and time and the seconds and milliseconds since midnight, respectively. Get a value from the Now property by using the DateTime structure or a DateTime instance. The Timer property is defined in the Microsoft.VisualBasic namespace. (There are also Timer classes defined in the System.Timers and System.Windows.Forms namespaces.)

You can get the current date and time by calling DateTime.Now. You can use the Timer property to cache a reference time, and then compare the reference time to a future time (see Figure A.5).

Figure A.5. The MsgBox showing the value of the elapsed time after the thread sleeps for 1.5 seconds.

graphics/afig05.jpg

 Dim Start As Double = Timer System.Threading.Thread.Sleep(1500) MsgBox(Format("Elapsed time is {0} ", Timer - Start)) 

The fragment declares a Double and uses the Timer property to cache a reference number of milliseconds. The shared method Sleep puts the current thread to sleep for 1500 milliseconds and then displays the elapsed time.

Rnd and Round

The random function (Rnd) and the Round function, like many things in Visual Basic .NET, have been relocated to the System namespace in the Math class as Shared members . Rnd returns a random number, and Round performs rounding on floating-point numbers. The System namespace is automatically imported into your applications; just remember that these methods are shared members of the Math class.

Instead of the Rnd function, you can use the new Random class to generate random numbers in a more organized manner. The following code fragment demonstrates how to construct a Random object, passing the random number generator seed to the constructor.

 Dim R As New Random(100) Dim I As Integer For I = 0 To 9   Debug.WriteLine(R.Next(100)) Next 

The first statement constructs an instance of Random with a seed of 100. Random numbers are floating-point numbers between 0.0 and 1.0. In VB6 we multiplied these small decimal numbers by a scalar to generate random numbers within a particular range of numbers; for example, Rnd * 100 generated random numbers from 0.0 to 100.0. The statement R.Next(100) employs one of the overloaded Random methods that returns a number less than or equal to max value.

PSet and Scale Are Not Supported in Visual Basic .NET

PSet allows you to set a single pixel in VB6, and Scale allows you to define the coordinate system of forms and various other controls. These procedures have no direct support in Visual Basic .NET. Refer to the System.Drawing and System.Drawing.Design namespaces for graphics support in .NET. (Chapter 17 covers graphics programming in Visual Basic .NET.)

Sgn and Sqr Have New Home and Name in Visual Basic .NET

Like the Atn procedure, Sgn and Sqr are supported in Visual Basic .NET, but they have a new home and the names have been refined to make them more readable.

Math routines are defined in the System namespace in the Math class as shared methods Sign and Sqrt, respectively. Sign returns -1 if the value is less than 0, 0 if the value is 0, and 1 if the value is greater than 0. Sqrt returns the square root of a number.

String Class

Strings are immutable instances of the String class in Visual Basic .NET. When you modify a string, you are actually implicitly creating a new instance of a string, initialized with the new value.

String is derived directly from Object. You can declare string variables as you did in VB6, treating a string like a value type, or you can use the object-creation syntax and pass parameters to the string class. Strings behave like ValueType structures but are reference types.

In most cases, you will use Visual Basic .NET strings just as you would VB6 strings.

 Dim S As String S = "Some Text!" 

The String class is worth examining because it contains many new capabilities. Refer to Chapter 2's section "String Type" for more on strings in Visual Basic .NET.

Time

The Time function has been replaced with the DateTime.TimeOfDay property in Visual Basic .NET. The TimeOfDay property can be used to get and set the system time.

As with the Date$ function, the Time$ function has been replaced with the Microsoft.VisualBasic.TimString property. TimeString can be used to get the system time as a string or set the system time from a string.

VarType

The VB6 VarType function that returned an enumerated value indicating the type of data stored in a variant has been replaced with the Value__ field of System.TypeCode. To retrieve an enumerated representation of a type, access the Value__ property of the TypeCode enumeration. The following statement prints the Value__ of the sender object passed to an event handler.

 MsgBox(sender.GetType.GetTypeCode(sender.GetType).value__) 

IntelliSense does not provide a hint indicating the presence of the Value__ attribute of the TypeCode enumeration; you just have to know it is there. Keep in mind that code that is written based on underlying values of types is not very portable and should be avoided.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net