top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

"My" Objects Of VB.NET

0 votes
255 views

Introduction

The VB.NET Version 2.0 as well as C# version 2.0 contains many new enhancements to the core language. One of the new cool feature added to VB.NET is "My" objects. My objects is a set of objects that are implicitly available to your application which provide short cut to many tasks otherwise seem tedious. For example, to know details about the computer on which the application is running such as name, memory, clock and network you have My.Computer object. No need to look anywhere else. All the necessary information is available at a single place! In all there are seven My objects and this article is going to give you peek into some of them.

"My" Namespace

VB.NET 2.0 provides a namespace called "My" (Me, MyBase, MyClass and now My...hmm...what next?). This namespacfe provides seven implecit objects to your application. The following table lists all of them:

Object Purpose
My.Computer Gives information about your computer such as its name, memory, clock, network details and so on.
My.Application Gives information about the current application such as path, assembly information (name, version etc.) and environmental variables
My.User Gives information about the current Windows user such as user name. Also, allows to check if user belongs to a specific role.
My.Forms Gives reference to all the forms from a project. (I am sure VB6 developers are going to like this feature a lot)
My.WebServices Gives access to proxy classes of the web services being used
My.Settings Allows to read or store application configuration settings
My.Resources Allows to read resources used by the application.

Example

As an example we are going to develop a Windows Forms application as shown in the following figure:

image

Here, we are displaying information about the computer, application and the user. We are also using an embedded resource. Displaying information about computer, application and user is straightforward. In order to retrieve embedded resource you must first add one to your application. In order to do so, double click on the Resources folder in the Solution Explorer. The interface of the resulting dialog is simple and you can easily embed image files using Add Resource toolbar option. Once you add required image file, rename the embedded resource as DotNetLogo. Finally, add the following lines of code to the Form_Load event:

Label2.Text = My.Computer.Name
Label4.Text = My.Computer.Network.IsAvailable
Label6.Text = My.Computer.Screen.DeviceName
Label8.Text = My.Computer.Clock.LocalTime
Label10.Text = My.Computer.Screen.Bounds.Width & 
" X " & My.Computer.Screen.Bounds.Height

Label15.Text = My.Application.Info.DirectoryPath
Label16.Text = My.Application.IsNetworkDeployed
Label17.Text = My.Application.Info.AssemblyName
Label18.Text = My.Application.Info.Version.Major & "." 
& My.Application.Info.Version.Minor & "." 
& My.Application.Info.Version.Build & "." 
& My.Application.Info.Version.Revision
Label19.Text = "Welcome " & My.User.Name & "!"
PictureBox1.Image = My.Resources.DotNetLogo

The code simply uses My.Computer, My.Application, My.User and My.Resources objects. That's it! Run the application and see the power of "My" objects in action.

posted Jan 6, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Introduction

Microsoft introduced long running background application called Services along with Windows NT Operating System. These services are multithreaded. Writing a windows NT Service using VB 6 was a tedious job. However, VB.NET masks the task much easy. In this article I will show you how to create a simple Windows Service using VB.NET

Overview of Services

Windows service is a background process that performs certain task. You can start, stop, pause or resume services on remote and local computers. Windows also provide option to enable or disable a service. Objective of Windows service is to run application in the background in certain period even though machine is busy with other process or work. You can also set a user account that a service will use to log on.

Is Windows NT Service having an advantage over application running under Windows Scheduler?

As mentioned earlier, windows services are used to do certain task on regular intervals. Is this can be achieved by other way? Actually Yes. You can create an application for performing your task. Using windows scheduler you can call this application at regular intervals. What is the advantage of implementing Windows Service then? Normally windows services will not have a user interface. Windows services runs efficiently because of its own process. To start windows Scheduler, one of the user has to log on to system. Whereas Windows service can automatically started when system boots.

Creating a simple windows service

Before we start developing windows service one thing you have to remember. You can deploy the service only on OS that has NT based architecture. If you are working in Visual Studio .Net, you can see a windows service template on new project dialog. You have to code very less to test this simple windows service if you are using VS.NET. In this article we are going to create a simple windows service which will monitor a specific folder on the disk. If any file is renamed, created, modified or deleted, service will keep its track in the system log.

Open your VS.NET and select windows service project and enter the name folderSPY and click ok. Double click the design form or Press Shift + F7. On the top section of code window, enter the following:

Imports System.IO
Public folderToWatch as fileSystemWatcher

On the OnStart procedure type the following

Dim strSysDir As String
folderToWatch = new fileSystemWatcher()

To watch system directory

strSysDir = System.Enviroment.SystemDirectory()

Set the path of the folder which you want to watch. Here our service is watching WINNT folder.

folderToWatch.path = strSysDir

To monitor above mentioned folder, you have to set certain filters.

With folderToWatch
	.notifyFilter = .notifyFilter 
				OR notifyFilters.FileName
	.notifyFilter = .notifyFilter 
				OR notifyFilters.Attributes
	'To watch sub directories....
	 .includeSubDirectories = True
End With

How to know whether any user changed the file name or file attributes?

To know this you need handlers. Following code will explain this.

AddHandler folderToWatch.Created, Addressof newLog
AddHandler folderToWatch.Deleted, Addressof newLog
folderToWatch.enableRaisingEvent = True

Next, write a sub procedure called newLog.

Private Sub newLog(byVal Source as Object, 
byVal evt as FileSystemEventArgs)
	If evt.ChangeType = WatcherChangeTypes.Created Then
		writeToLog(evt.fullPath,"Created")
	else if evt.ChangeType = WatcherChangeTypes.Deleted
  		writeToLog(evt.fullPath,"Deleted")
	End if
End Sub

Private Sub writeToLog(byVal filePath as String, 
byVal fileManip as String)
	Dim evtLog as new EventLog()
	If not evtLog.sourceExists("folderSPY") then
		evtLog.createEventSource
		("folderSPY", "Log of folderSPY")
	End if 
	evtLog.source = "folderSPY"
	evtLog.writeEntry
	("Folder spy Log", "File "  & filePath & " has " 
	& fileManip &  " by " & Environment.UserName.ToString,
	EventLogEntryType.Information)
End Sub

Installing the folderSPY windows service

The application that we have created is ready to test. Since it is not a traditional windows executable, you need to install it on your machine by special way. .NET Framework ships with InstallUtil.exe command line tool that installs the windows service on your machine. Before using this tool, however, we have to add an installer toy your project. This can done by right clicking your design screen and select add installer option from the menu. After you do that, you can see two more controls called ServiceProcessInstaller1 and ServiceInstaller1 added to your screen. To Change the display name of the service, open the ServiceInstaller1 control's property window and change the ServiceName property and Display name property to folderSPY (This is the service name we have given).

Since we are going to run our application on the local machine, set the account property of the ServiceProcessInstaller1 to LocalSystem. To make your service pause and continue, set the CanPauseandContinue property of your service to true. Now, compile the folderSPY application and run following command from Command Prompt:

InstallUtil <Project Path>\BIN\folderSPY.exe

If you want to uninstall the service use:

InstallUtil /U \BIN\folderSPY.exe

Running the Service

If Startup property of the service is not set to Automatic, you have to start the service manually or through some other program. To start the service open Services snap-in from the Control panel. Right click on the folderSPY service and click start.

READ MORE

Introduction

.NET is fully object oriented platform that allow languages to take full advantage of these OO features. The features include:

  • Namespace
  • Classes
  • Objects
  • Encapsulation
  • Overloading
  • Inheritance
  • Overriding
  • Interfaces
  • Polymorphism

Let's take a quick look at what each of this term means.

Namespace

Even though namespace is not really an OOPs feature, .NET use it extensively. Namespace is nothing but a logical container for various classes. Under given namespace class names must be unique. Namespace server two purposes - they provide logical organization of classes and they also avoid ambiguity.

Classes

Class is nothing but a template or blue-print for an entity. For example you may have a class that represents real life entity - Employee. The class will provide properties (Name, Age...) as well as actions (CalculateSalary, GoOnLeave...) of the entity.

Objects

Instances of classes are called as objects. For example there might be three instances of the Employee class mentioned above. They might represent individual employees - John, Bob and Tom.

Encapsulation

Each object typically deals with some kind of data or the other. Not all the data needs to be exposed to external systems. This can be controlled via data encapsulation.

Overloading

Overloading refers to the methods or functions having same name but varying parameters. The parameters should vary with respect to data types and order.

Inheritance

Inheritance refers to extending functionality of existing class. Inheritance is useful when developing "object models" for your system. .NET supports only single inheritance.

Overriding

Overriding refers to the methods in the child class having the same signature (name as well as parameters) as of the parent class methods.

Interfaces

Interfaces are nothing but models of class properties and methods without any implementation. The class implements the interface. Once a class implements any interface it must implement all the properties and methods (although the implementation can be empty or null implementation).

Polymorphism

Polymorphism refers to the ability of the system to call correct implementation of methods with the same name. For example, Clerk as well as Manager class might have a method called CalculateSalary(). However, at runtime depending on whether the underlying object is of type Clerk or Manager correct version of the method is called.

Creating namespaces

Namespaces are created using keyword - Namespace (namespace in C#). Following example shows how to declare a namespace.

[VB.NET]
Namespace MyNamespace
...
End Namespace

[C#]
namespace MyNamespace
{
...
}

Note that one assembly can have one or more namespaces. Also, one namespace can span across multiple assemblies. You can create nested namespaces as follows:

[VB.NET]
Namespace MyNamespace
   Namespace MuSubNamespace
   ...	
   End Namespace	
End Namespace

[C#]
namespace MyNamespace
{
   namespace MySubNamespace
   {
   ...
   }
}

If you are using VS.NET then the project name acts as the default namespace name.

Creating classes

Creating classes is similar to creating namespaces.

[VB.NET]
Public Class Class1
  ...	
End Class

[C#]
public class Class1
{
  ...
}

Generally classes will be part of some of the namespace.

Creating Properties

Properties encapsulate data members of your class. Properties can be read-write, read only or write only. Here is how you create read-write properties:

[VB.NET]
Public Class Employee

   private strName As String
   
   Public Property Name As String
      Get
  	return strName;
      End Get
	   
      Set(value As String)
	strName=value;   
      End Set
   End Property
End Class

[C#]
public class Class1
{
   public string Name
   {
   	string strName;
   	get
   	{
   	   return strName;
   	}
   	set
	{
	   strName=value;
	}
   }
}

Here,

  • VB.NET uses Property keyword to declare properties. C# does not have this keyword
  • Property definition consists of two parts Get and Set. The get part returns the property value and set par sets some private variable.
  • The value in Set routine is received via implicit variable called value in C#. VB.NET allows you to change this.

Creating methods

Methods represent actions performed by the object. In VB.NET functions and sub routines are collectively called as methods. In C# everything is function.

[VB.NET]
Public Sub CalculateSalary()
...
End Sub

[C#]
public void CalculateSalary()
{
...
}

Method overloading

Method overloading refers to methods with same name but different types or order of parameters. Following example make it clear:

[VB.NET]
Public Sub CalculateSalary()
...
End Sub

Public Sub CalculateSalary(month as Integer)
...
End Sub

[C#]
public void CalculateSalary()
{
...
}

public void CalculateSalary(int month)
{
...
}

In VB.NET you can also use optional parameters to achieve similar functionality. However, it is recommended to use overloading instead to make your code consistent across languages.

Inheritance

Inheritance is the ability to extend existing class further. Unlike languages like C++ that allow multiple inheritance .NET allows only single inheritance. This means that at a time you can inherit from a single class.

[VB.NET]
Public Class Manager
	Inherits Employee
  ...	
End Class

[C#]
public class Manager : Employee
{
  ...
}

In the above example, we create a class called Manager that inherits from Employee class. As you can guess Manager is specific implementation of generic Employee class. VB.NET uses Inherits keyword to indicate the parent class where as C# uses : operator to indicate that.

Method Overriding

In order to override method in child class they need to be marked as Overridable (VB.NET) or virtual (C#) in the parent class.

[VB.NET]
Public Overridable Function CalculateSalary() As Integer
...
End Function

[C#]
public virtual int CalculateSalary()
{
...
}

Then in the child class you can create a method with the same signature and specify that it overrides the base class method.

[VB.NET]
Public Overrides Function CalculateSalary() As Integer
...
End Function

[C#]
public override int CalculateSalary()
{
...
}

Note that if you do not provide the Overrides (VB.NET) or override (C#) keywords in the child class the compiler issues a warning that you are hiding a base class member. In this case you can either put the above keywords or use Shadows (VB.NET) or new (C#) keywords. Using these keywords ,however, will hide the base class members.

Creating Interfaces

Just like classes are templates for real life entities, interfaces can be thought of as templates for classes. They bring uniformity in your object model.

[VB.NET]
Public Interface IEmployee
	Property EmployeeID() As Integer
    Property Name() As String
    Property Age() As Integer
    Function CalculateSalary() As Integer
End Interface

[C#]
public interface IEmployee
{
	int EmployeeID
	{
		get;
	}

	string Name
	{
		get;
		set;
	}

	int Age
	{
		get;
		set;
	}

	int CalculateSalary();
}

As you can see VB.NET uses Interface keyword to define an interface. Similarly, C# uses interface keyword. Note, how they contain only property and method signatures and no code at all.

Implementing Interfaces

The main difference between inheritance based programming and interfaces based programming is that - interfaces just specify signatures of properties and methods for a class. Your class "implements" the interface by providing implementation for various properties and methods. Unlike inheritance there is no "code" inherited from interfaces. Your class can implement one or more interfaces.

[VB.NET]
Public Class Manager
	Implements IEmployee
  ...	
Public Function CalculateSalary() 
As Integer Implements IEmployee.CalculateSalary
...
End Function  
End Class

[C#]
public class Manager : IEmployee
{
  ...
	public int CalculateSalary()
	{
		...
	}
  
}

Above example shows how VB.NET uses Implements keyword to implement an interface. Note how VB.NET also requires the use of Implements keyword for each property and method. You must have guessed from this that in VB.NET you can give different name to the implemented member than the interface. This feature is not available in C#. C# do not have a special keyword and uses the same : operator to implement the interface.

Polymorphism

Consider following lines of code:

[VB.NET]
Dim emp As Employee
emp = New Clerk()
Console.WriteLine
("Clerk Salary :{0}", emp.CalculateSalary())
emp = New Manager()
Console.WriteLine
("Manager Salary :{0}", emp.CalculateSalary())

[C#]
Employee emp;
emp=new Clerk();
Console.WriteLine
("Clerk Salary :{0}",emp.CalculateSalary());
emp=new Manager();
Console.WriteLine
("Manager Salary :{0}",emp.CalculateSalary());

Here, we have declared a variable of type Employee. A variable of parent class type can point to instance of any of its children. First, we point it to an instance of Clerk class. Then we point it to an instance of Manager class. Even though the variable is of type Employee, depending on which child type it is pointing to it calls the correct implementation of CalculateSalary() method. The underlying system does this via inheritance polymorphism. Similar thing can also be achieved in interface polymorphism.

[VB.NET]
Dim emp As IEmployee
emp = New Clerk()
Console.WriteLine
("Clerk Salary :{0}", emp.CalculateSalary())
emp = New Manager()
Console.WriteLine
("Manager Salary :{0}", emp.CalculateSalary())

[C#]
IEmployee emp;
emp=new Clerk();
Console.WriteLine
("Clerk Salary :{0}",emp.CalculateSalary());
emp=new Manager();
Console.WriteLine
("Manager Salary :{0}",emp.CalculateSalary());

Summary

In this article we saw many OO features of VB.NET and C#. You must have got idea how VB.NET as well as C# provide closely matching features and keywords in this area.

READ MORE

Introduction

Visual Basic as a language never had provision of pointers or raw memory management as in C or C++. However, in VB.NET you can do so using certain .NET framework structures and classes. They include IntPtr, Marshal and GCHandle. These structures and classes allow you to communicate with managed and unmanaged environments. In this article I (Bipin Joshi) will show you how to use these structures and classes to perform pointer and unmanaged memory operations. Though the examples I give are in VB.NET, converting them to C# should not be a problem. Also note that, for the sake of simplicity I have not included any unmanaged code samples here. My aim is to illustrate use of these structures and classes so that one can use them as per individual requirements.

About IntPtr structure

IntPtr is a structure that acts as an integer pointer that is designed to be platform specific. This structure can be used in languages that do or do not support pointers. .NET file IO classes use this structure extensively for holding file handles.

About Marshal class

The IntPtr structure simply acts as a pointer to platform specific integer. It does not have any ability to write or read data at that memory location. You need to use Marshal class residing in System.Runtime.InteropServices namespace. This class provides collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types. Make sure you import System.Runtime.InteropServices in your code.

About GCHandle structure

The GCHandle structure provides a means for accessing a managed object from unmanaged memory. This can be used to control garbage collection of your object when unmanaged code is using it.

Writing and reading an integer value

In our first example, we will store an integer value in memory using Marshal class. Then we will get pointer to its memory location and store it in IntPtr structure. Finally, we will read value back using Marshal class again.

Dim ptr As IntPtr
Dim n as Integer=123
ptr = Marshal.AllocHGlobal(4)
Marshal.WriteInt32(ptr, n)

Here, we declared a variable of type IntPtr. We then used AllocHGlobal shared method of Marshal class that allocates a memory from global heap of 4 bytes and returns its address. We collect the address in our IntPtr variable. We then called the WriteInt32 method of Marshal class and passed memory address for writing operation followed by integer to be written.

You can read value from a memory location as follows:

Dim myint as Integer=Marshal.ReadInt32(ptr)

Writing and reading strings

In this example we will write a managed string to heap and then read it back.

Dim str as String = "hello world"
Dim ptr As IntPtr = Marshal.StringToHGlobalAuto(str)
Dim mystring As String = Marshal.PtrToStringAuto(ptr)

Here, we used StringToHGlobalAuto() method of marshal class that copies the contents of a managed string into unmanaged memory. We collect the memory address in IntPtr structure. To read the string back we used PtrToStringAuto() method of Marshal class that returns a string at specified memory location.

Writing and reading structures

In the next example we will see how to write and read structures using Marshal class and IntPtr structure.

Public Structure Point
   Dim x As Integer
   Dim y As Integer
End Structure

Dim mystruct1,mystruct2 As Point
mystructure.x=100
mystructure.y=200
Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(mystruct))
Marshal.StructureToPtr(mystruct, ptr, True)
mystruct2 = Marshal.PtrToStructure(ptr, New Point().GetType)

Here, we will be using a structure called Point. This code is similar to example 1 but note how we used SizeOf method of Marshal that returns size of our structure variable. The StructureToPtr method accepts the structure variable to be written, the IntPtr instance that will be returned and flag indicating whether Marshal class should call DestroyStructure method. It is recommended that you set this flag to True as setting it to false can cause memory leaks. Finally, we used PtrToStructure method of Marshal class to read the structure back.

Writing and reading objects

Up till now we saw how to read and write value types using Marshal and IntPtr class. The managed objects work a bit differently. You need to use GCHandle to read and write these objects. Following code shows how:

Public Class Employee
    Public Name As String
    Public Salary As Decimal
End Class

Dim gh As GCHandle
Dim emp As New Employee
emp.Name = "John"
emp.Salary = 12345.67
gh = GCHandle.Alloc(emp)
Dim emp2 As Employee = gh.Target
gh.Free()

Here, we used GCHandle (Garbage Collector Handle) class to allocate and de allocate memory to our instance of employee class. The shared (static) Alloc method accepts the object to store in the memory and returns an instance of GCHandle class. We can point to the reference of our object via Target property of this object. Once we are done with the object we can release memory consumed by it using Free method of GCHandle instance.

Summary

Pointer like operations and unmanaged operations were never easy in VB.NET. However, IntPtr and GCHandle structures and Marshal class allow you to achieve similar effect.

READ MORE

Introduction

Access modifiers decide accessibility of your class or class member. There are five accessibility levels in VB.NET. They are:

  • Private
  • Protected
  • Friend (internal in C#)
  • Protected friend (protected internal in C#)
  • Public

This article examines all them with examples. Even though the examples are in VB.NET, they can be easily ported to C# as most of the keywords are same.

Public Access

Many programmers have habit of making everything public in their applications. This is fine for test applications but when you are developing real life applications you should expose only the data or functionality that is necessary by the user of your class. Classes and class members marked with Public access modifier are available in the same class, all the classes from the same project and to all other projects as well. This access specifier is least restrictive. Following is an example of public access specifier.

Public Class Book
   Public Title As String
End Class

Public Class BookUser
   Public Sub SomeMethod()
	Dim x as new Book()
	x.Title="VB.NET Programming"
  End Sub
End Class

Restricting access to classes and class members is not only a good programming practice but is also necessary to avoid any accidental misuse of your classes. Let us now discuss each of the remaining access modifiers in detail

Private Access

Private access modifier is applicable only to the members of a type. It restricts access to the members within the type itself. Consider following class:

    Public Class Book
        Private strTitle As String

        Public Property Title()
            Get
                Return strTitle
            End Get
            Set(ByVal Value)
                strTitle = Value
            End Set
        End Property
    End Class

Here, the member variable strTitle is declared as private inside a class Book. Hence, it cannot be accessed from outside the class Book. Remember that private access modifier is applicable only to type members not to the type itself. This means you cannot declare a class as private but if your class is a nested then it can be declared as private. For example following declaration is invalid:

Namespace n1

    Private Class Book

    End Class

End Namespace

However, following declaration of nested class is valid:

Namespace n1
    Public Class Book
        Private Class NestedBook
        End Class
    End Class
End Namespace

Protected Access

Private access modifier allows us to hide members from others but what if some one is inheriting from your class? In many cases you want that members of base class should be available in derived class. This cannot be achieved with private modifier. Protected access specifier provide such access. The members marked with protected access modifier can be accessed in the same class and all the classes inherited from it but they are not available to other classes. Following is an example of using protected access specifier:

    Public Class Class1
        Protected age As Integer

        '... other code
    End Class

    Public Class Class2
        Inherits Class1

        Public Sub SomeMethod()
            age = 99 'OK
        End Sub
    End Class

    Public Class Class3
        Public Sub SomeMethod()
            Dim x As New Class1()
            x.age = 99 'ERROR
        End Sub
    End Class

Friend Access

Now going one step further let us assume that you want that all the classes from your project should be able to access to your class members but classes external to your project should not be able to do so. In this case neither private nor protected can help. Only your �Friend� can help you out. You guessed it! The friend access modifier is used to declare your class members such that any class from the same project will be able to access them but external classes cannot. Note that this access modifier is applicable to class definitions also. Following are the examples of using Friend members and classes.

Assembly1.dll

   Public Class Class1
        Friend age As Integer
        '... other code
   End Class

    Public Class Class2
        Public Sub SomeMethod()
            Dim x As New Class1()
            x.age = 99 'OK
        End Sub
    End Class

Assembly2.dll

    Public Class Class3
        Public Sub SomeOtherMethod()
            Dim x As New Class1()
            x.age = 99 'ERROR
        End Sub
    End Class

When applied to class the class will be available only in the project in which it is declared.

Assembly1.dll

    Friend Class Class3
        Public Sub SomeMethod()

        End Sub
    End Class

    Public Class Class4
        Public Sub SomeOtherMethod()
            Dim x As Class3 'OK
        End Sub
    End Class

Assembly2.dll

    Dim x As TestComp1.n1.Class3 'ERROR

Note: In C# internal keyword serves the same purpose as Friend keyword in VB.NET.

Protected Friend Access

Protected Friend access modifier is a combination of protected and friend access modifiers and allows access to class members in the same project and all the inherited types.

Summary

This article examined various access modifiers available in .NET. Restricting access to your class members is not only a good practice but also make your classes less error prone and robust. They will also avoid any accidental misuse of class members.

READ MORE
...