Use the My objects to program common tasks – The new MY object provides easy access to various features that developers often need but don't necessarily know where to find in the sprawling .net class library.
-
My.Computer – This object provides info about the current computer, including its network connection, the mouse and keyboard state, the printer and screen and the clock. Also play a sound, find a file, access to the registry and windows clipboard.
-
My.Application – This object provides information about the current application and its context, including the assembly and its version, the folder where the application is running the culture and the command line arguments that were used to start the application, log an application event.
-
My.User – This object provides information about the the current user. You can use this objet to check the user's windows account and test what group the user is a member of.
-
My.Forms-this object provides a default instance of each windows form in your appplication. you can use this object to communicate between forms without needing to track from reference in another class.
-
My.WebServices – this object provides a default proxy class instance for every web service.for example, if your project uses two web ref. You can access a ready made proxy class for each one through this object.
-
My.Settings – This object allows you to retrieve custom settings from your application XML connection file.
-
My.Resources – this object allows you to retrieve resources – blocks of binary or text data that are compiled into your application assembly. Resources are typically used to store localization strings, images and audio.
Use Strongly Typed Resources – strongly typed resources let you embade static data such as images into your compiled assemblies, and access it easily in your code.
PictureBox1. Image = My.Resources.Image1
( ImageList control doesn't use typed resources.)
Use Strongly Configuration Settings – application commonly need configuration settings to nail down details like file locations, database connection strings and user pref. Rather then hard coding these settings .net let you add them to an application specific configuration file. This allows you to adjust values on a whim by editing a text file without recompiling your application
Visual Studio 2005 configuration settings are even easier to use. That's because they are automatically compiled into a custom class that provides strongly typed access to them.
Dim path as string
path = My.Settings.UserDataFilePath
(The application settings class is added in the my project directory and is named settings.designer.vb.
To see it select project->show all files.
Make Simple data type Null able – A NULL value is a special flag that indicates no data is present. Most developers are familiar with null object ref. Which indicates that the object has been defined but not created. Core data types like integers and strings can't contain null values. Numeric variables are automatically initialize to 0. boolean are false and strings are assigned to empty string(“”). Even if you explicitly set a simple data type to Nothing in your code it will automatically revert to the empty value.
The solution is System.Nullable class that can wrap any other data type. When you create a instance of Null able you specify the the data type. If you don't set a value this instance contains a null reference. You can test that whether this is true by testing the Nullable.HasType() method. And you can retrieve the underlying object through the Nullable.Value property.
Dim i As Nullable(Of Integer)
If Not i.HasValue Then
'this is true becaues no value has been assigned
MsgBox("i is a null value")
End If
If i.HasValue Then
MsgBox(i.Value)
End If
Split a class into multiple Files – .NET 2.0 has new feature named PARTIAL CLASS. Using the new Partial keyword, you can split a single class into as many pieces as you want. You simply define the same class in more then one place. Here is an example:
Public Class SampleClass
Public Sub methodA()
MsgBox("method a")
End Sub
End Class
Partial Public Class SampleClass
Public Sub methodB()
MsgBox("method b")
End Sub
End Class
in this example the two declaration are in the same file, one after another, but you can put it into two different class files.
Partial class doesn't offer you much help in solving programming problems, but they can be useful in breaking up extremely large, unwieldy classes.
Extend the My Namespace - The My objects aren't defined in a single place. Some come from classes defined in the Microsoft.VisualBasic.MyServices namespace. While others are generated dynamically as you add forms, web services, configuration settings and embedded resource. However as a developer you can participate in the My name space and extend it with your own ingredients.
Namespace MY
Public Class BusinessFunctions
Public Shared Function GenerateNewCustomerID(ByVal name As String) _
As String
Return name & "_" & Guid.NewGuid().ToString
End Function
End Class
End Namespace
use:
My.BusinessFunctions.GenerateNewCustomerID()
You can also extend some of the existing My objects thanks to Partial Classes.
Skip to the Next Iteration of a Loop – using continue statement.
For i As Integer = 0 To 100
If i Mod 5 = 0 Then
MsgBox(1 Mod 5)
Continue For
End If
' Task B if i mod 5 <> 0
' Task C if i mod 5 <> 0
Next
Dispose Objects Automatically - worried that you'll have objects floating around in memory, tying up resoures until Garbage Collector tracks them down ? With the USING statement you can make sure that disposable objects meets with a timely dismissed.
Using NewFile As New System.IO.StreamWriter("c:\readme.txt")
NewFile.WriteLine("Today is" & DateTime.Today.ToShortDateString)
NewFile.WriteLine("Done")
End Using
Using statement makes sense with all kinds of disposable objects, such as:
-
Files ( including FileStream, StreamReader and StreamWriter)
-
Database connections( including SqlConnection,OracleConnection and Olddbconnection)
-
Network Connection (including TCPClient, UDPClient, Network Stream, FtpWebResponse, HttpWebResponse)
-
Graphics
Evaluate Conditions Separately with Short-Circuit logic – Visual Basic 2005 introduce two new operators. AndAlso and OrElse. A common program scenario is the need to evaluate several conditions in a row. Often this involves checking that an object is not null and then examining one of its properties. You can do it using this two new keywords.
If MyObject Is Nothing AndAlso myObject.Value > 0 Then
'do something
End If
Maulik.Soni