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
What are benefits & Limitation of  Hidden fields? The benefits of using Hidden fields are as shown below :- 1)    They are very simple to implement. 2)    As the data is

What is Data Independence? Data independence means that "the application is independent of the storage structure and access method of data". In other words, the ability to cha

What is Dispose method in .NET? The .NET provides "Finalize" method in which we can clean up our resources. But relying on this is not always better so the best is to implement


Experienced Microsoft .NET web developer The developer must be reliable as well as most of all use best practices for coding as well as application design. a) .NET 3.5 (ideal

What is the use of DataView? User-defined view of a table is contained in a DataView. A complete table or a small section of table depending on some criteria can be shown by an

Write a XML with database with book details (BOOK ID, Title, Author, subject, published Year, language, vendor, price)

Using the attached XML file (xml_ind.xml) create a XSLT and CSS file for the following information: select the following information for ONLY the cars that cost more than $50,00

What are the core functionalities in XML .NET framework? The XML API for the .NET Framework includes the following set of functionalities: XML readers With XML readers

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4