Format function

dezreena

Member
Joined
Mar 29, 2007
Messages
20
Programming Experience
Beginner
Hello people,

im confused how to use Format function:

VB.NET:
Public Class Zahlen

Dim x as integer= 1

Label2.Text= x

End Class

what is the different if im using :

VB.NET:
Label2.Text=[B]Format[/B]( x)


I have tried both ways, and both are worked out. i just want to know what is the different by using this Format - function?
 
Format() is a legacy function and should not need to be used. In VB.NET you do not need to convert an integer to a string in order to set the text property. If you want to insure that the integer is indeed a string every object contains the tostring() function which returns a string value representing the object. The String.Format function can be used to format a string but the Format function is old and used to be used most commonly for formatting dates and things like that. But all that you have to do to set the text of that label to an integer value is do what you were doing in the first example.
 
What you are doing in your first code snippet is legal but it shouldn't be. The Text property of a Label is type String and so it can only accept a String. If you assign an Integer to it then it must first be converted to a String. In your first code snippet that conversion is happening implicitly, i.e. the system is doing it for you. That will happen if you have Option Strict turned Off, which it is by default. I strongly recommend that you turn Option Strict On immediately. In that case your code would produce a compilation error, telling you that you can't assign an Integer to a String property. That may sound like a step backwards but it isn't. By forcing you to make conversions explicitly the compiler is making you a better developer by ensuring you develop a better understanding of exactly how your code works. It also helps to catch potential issues at design time, when you can fix them, rather than at run time, when they may crash your app or make it behave in unexpected ways. It will also make your code execute faster because it reduces the amount of type checking that is required.
 
Back
Top