Shared (vb.net)/static(c#) variables, DOT NET Programming

What are shared (VB.NET)/Static(C#) variables?

Shared/Static classes are used when a class gives functionality which is not specific to any instance. In brief if you want an object to be shared between multiple instances you will use a Shared/StaticĀ  class.

The key features of Static/Shared classes are as follows:-

1) They can't be instantiated. By default the object is created on the first method call to that object.

2) The Shared/Static classes can not be inherited.

3) The Shared/Static classes can have only static members.

4) The Shared/Static classes can have only static constructor.

Public Class ClsShared

Shared intCount As Integer

Public Function AddCount() As Integer intCount = intCount + 1

Return intCount

End Function

End Class

Public Class FrmSharedClasses

Inherits System.Windows.Forms.Form

Private Sub CmdInstance1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdInstance1.Click

Dim pobjClsShared As New ClsShared() MessageBox.Show("The count at this moment is" &

pobjClsShared.AddCount.ToString()) End Sub

Private Sub CmdInstance2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdInstance2.Click

Dim pobjClsShared As New ClsShared() MessageBox.Show("The count at this moment is" &

pobjClsShared.AddCount.ToString())

End Sub

End Class

961_shared in action.png

Figure :-Shared/Static In Action

Posted Date: 9/22/2012 3:14:50 AM | Location : United States







Related Discussions:- Shared (vb.net)/static(c#) variables, Assignment Help, Ask Question on Shared (vb.net)/static(c#) variables, Get Answer, Expert's Help, Shared (vb.net)/static(c#) variables Discussions

Write discussion on Shared (vb.net)/static(c#) variables
Your posts are moderated
Related Questions
Question 1 Discuss the following with respect to .NET Technologies Web Applications Web Server Connections Question 2 Write a sample program using ASP.NET explain

I need C# AAC sound receiver over WCF Project Description: I want a WCF method which is able to receive a byte stream of an AAC sound file from iPhone device and save it on t

What are the steps involved to fill a dataset ? The sample code is shown below: Private Sub LoadData() Dim strConnectionString As String strConnectionString = AppSetti

Project Description: It is online portal for hotel n packages sales. The project requires to be built on sql, Jquery and c#. The project should be submitted with all coding a

What is a monitor object? Monitor objects are used to ensure that a block of code runs without being interrupted by code running on other threads. In other words, code in other

Finding Largest Array Element - C# Program I am unable to make out the Largest Array Element in C# language. Can anyone suggest me any example for this.

create a table using class file

inheritance real time examples

What is the difference between int and int32. There is no difference among int and int32. System.Int32 is a .NET Class and int is an alias name for System.Int32.

Install Modules to enhance Site Proformance Project Description: As shown, I need the subsequent modules installed, to improve loading time for my graphics and images, improv