How to have two instances of a class in VB.NET - vb.net

I have created a class and want to create two separate instances of this class. I've been able to achieve this in web applications, but I'm attempting to do this in a windows form and for some reason when I create the second instance of the class it has the variable data from the first instance.
I've tried to create the two instances as global variables as I want them to be accessed by separate sub routines. The two instances are declared as such:
Public Class Form1
Dim oTree1_Animation As New clsAnimation()
Dim oTree2_Animation As New clsAnimation()
I then attempt to populate the instance in a sub routine which fires on a MouseDown event:
Private Sub PictureBox1_MouseDown(sender As System.Object, e As System.EventArgs) Handles PictureBox1.MouseDown
Dim oFrame As New clsFrame(2, {0, -32}, {0, 0})
Dim timTimer As New Timer()
oTree1_Animation.Initialise(2, 100, oFrame, PictureBox1, timTimer)
oTree1_Animation.AnimationStart()
End Sub
The second is then populated in a similar fashion:
Private Sub PictureBox2_MouseDown(sender As System.Object, e As System.EventArgs) Handles PictureBox2.MouseDown
Dim oFrame As New clsFrame(2, {0, -32}, {0, 0})
Dim timTimer As New Timer()
oTree2_Animation.Initialise(2, 100, oFrame, PictureBox2, timTimer)
oTree2_Animation.AnimationStart()
End Sub
The class looks like this:
Public Class clsAnimation
Public Event Tick As EventHandler
Public Shared FrameCount As Integer
Public Shared FrameInterval As Integer
Public Shared CurrentFrame As Integer = 0
Public Shared FrameSet As clsFrame
Public Shared Image As PictureBox
Public Shared WithEvents Timer As Timer
''' <summary>
''' Creates an empty instance of the Animation class
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub
''' <summary>
''' Creates a new instance of the Animation class and preloads it with variables
''' </summary>
''' <param name="iFrameCount">Number of frames in this animation as Integer</param>
''' <param name="iFrameInterval">Frame transition speed (milliseconds) as Integer</param>
''' <param name="clsFrameSet">Frame information as clsFrame</param>
''' <param name="imgImage">The picturebox that we're animating as PictureBox</param>
''' <remarks></remarks>
Public Sub Initialise(ByVal iFrameCount As Integer, ByVal iFrameInterval As Integer, ByVal clsFrameSet As clsFrame, ByRef imgImage As PictureBox, ByRef timTimer As Timer)
FrameCount = iFrameCount
FrameInterval = iFrameInterval
FrameSet = clsFrameSet
Image = imgImage
Timer = timTimer
Timer.Interval = FrameInterval
End Sub
However, both oTree1_Animation and oTree2_Animation share the same variables. I don't know if I'm missing something small or if I'm just trying to use classes in an impossible way, any help on this would be much appreciated.

You have to remove Shared from all your class variables. Shared means that the values are shared between all instances of this class.
This is normally referred to as "static" (but for some reason VB.NET uses a different terminology).
See MSDN for Microsoft explanation (thanks benjineer).

Related

Adding a progress bar to show how much percentage of form had been loaded

I've a form having certain lines of code on the form_load event and thus it takes some time to be shown. I want to add a progress bar to show how much percentage of form has been loaded. How can I do so ..?
In short:- Like while browsing the web pages the progress as shown in web browser indicate the value of page has been loaded. I want to do so in vb.net.
How can I do so .?
You'll need to move your long running code into the form's .Shown event handler.
I would suggest adding a StatusStrip to your form and add a ToolStripProgressBar to that. I did this in a program I wrote and used the following bit of code work on it.
It's all pretty self explanatory :-)
''' <summary>
''' Sets minimum and maximum values for ToolStripProgressBar and shows it
''' </summary>
''' <param name="min">Minimum Value</param>
''' <param name="max">Maximum Value</param>
''' <param name="labeltext">Text to show in ToolStripLabel</param>
Private Sub SetAndShowProgressBar(min As Integer, max As Integer, labeltext As String)
ToolStripStatusLabel1.Text = labeltext
ToolStripProgressBar1.Minimum = min
ToolStripProgressBar1.Maximum = max + 5
ToolStripProgressBar1.Value = 1
ToolStripProgressBar1.Visible = True
StatusStrip1.Update()
End Sub
''' <summary>
''' Hides ToolStripProgressBar
''' </summary>
Private Sub HideProgressBar()
ToolStripStatusLabel1.Text = ""
ToolStripProgressBar1.Visible = False
StatusStrip1.Update()
End Sub
Private Delegate Sub incprogressbarDelegate()
Private Sub IncrementProgressBar()
If Me.InvokeRequired Then
Dim d As New incprogressbarDelegate(AddressOf IncrementProgressBar)
Me.Invoke(d, New Object())
Else
Me.ToolStripProgressBar1.Value += 1
StatusStrip1.Update()
End If
End Sub
Private Delegate Sub SetProgressBarDelegate(i As Integer)
Private Sub SetProgressBarValue(i As Integer)
If Me.InvokeRequired Then
Dim d As New SetProgressBarDelegate(AddressOf SetProgressBarValue)
Me.Invoke(d, New Object() {i})
Else
Me.ToolStripProgressBar1.Value = i
End If
End Sub

How to get connection string which is connected right now

How can i get the connection string which is right now connected to the database?
I can get all the connection strings but how can i get which one is connected?
Thanks in advance.
For connections that are not using the default connection e.g. .\SQLEXPRESS the following will give you the connections stored in app.config and a method to track the connection. In the form example I am using ms-access but this will work with sql-server also.
Edit: here is a fully function code example on Microsoft's code sample web sute I just created.
Supporting classes
Public Class ConfigItem
Public Property Data As System.Configuration.ConnectionStringSettings
Public Property Index As Integer
End Class
Public Class ConnectionInfo
Public Property Name As String
Public Property ConnectionString As String
Public Property Index As Integer
End Class
Worker class to get connection names, index and connection string
Imports System.Configuration
''' <summary>
''' Must add a reference to System.Configuration
''' to this project.
''' </summary>
''' <remarks></remarks>
Public Class ProjectConnections
''' <summary>
''' Storage for connections
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property Items As New List(Of ConnectionInfo)
''' <summary>
''' Used to remember the current connection
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property CurrentConnectionIndex As Integer
Private RemoveThis As String = ""
Public Sub New()
' look at parent assembly as this class is in a class project used by a
' forms project
RemoveThis = Reflection.Assembly.GetEntryAssembly.GetName.Name & ".My.MySettings."
' get connection data into the Items propery of this class
GetInformation()
End Sub
''' <summary>
''' Traverse through connection strings in app.config, exclude local sql-server connection
''' </summary>
''' <remarks>
''' tested with ms-access, sql-server attached and server based
''' </remarks>
Private Sub GetInformation()
ConfigurationManager.ConnectionStrings.Cast(Of ConnectionStringSettings)().Select(
Function(item, index) New ConfigItem With {.Data = item, .Index = index}).ToList _
.ForEach(
Sub(ConfigItem)
If ConfigItem.Data.Name.Contains(".") Then
Items.Add(
New ConnectionInfo With
{
.Name = ConfigItem.Data.Name.Replace(RemoveThis, ""),
.ConnectionString = ConfigItem.Data.ConnectionString,
.Index = ConfigItem.Index
})
End If
End Sub)
End Sub
End Class
Form utilizing the classes above. In this case there are two connections stored in app.config. The worker class is instantiated at form level so we can use it to track the current connection string. Optionally we could localize the class and use a private integer variable to remember the current connection. In form load, I am choosing which connection to use rather than the default, stored the index for that connection in the worker class instance, display connection string names in a ComboBox and exposing all information in a DataGridView. Pressing a button, we change the connection at runtime while a second button shows the underlying connection string.
Note the import statement, I placed the worker class into a class project so the form project must have a reference to it then followed by the import statement.
Imports ConfigurationLibrary
Public Class Form1
Private connections As ProjectConnections = New ProjectConnections()
Private Sub CustomersBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
Handles CustomersBindingNavigatorSaveItem.Click
Me.Validate()
Me.CustomersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Database1DataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' here I am loading a connection other than the default
CustomersTableAdapter.Connection.ConnectionString = connections.Items(1).ConnectionString
' for keeping track later as in cmdGetConnection.Click
connections.CurrentConnectionIndex = 1
Me.CustomersTableAdapter.Fill(Me.Database1DataSet.Customers)
cboConnections.DataSource = connections.Items
cboConnections.DisplayMember = "Name"
cboConnections.SelectedIndex = 1
dgvInformation.AutoGenerateColumns = False
dgvInformation.DataSource = connections.Items
CustomersDataGridView.ExpandColumns()
dgvInformation.ExpandColumns()
End Sub
Private Sub cmdSetConnection_Click(sender As Object, e As EventArgs) Handles cmdSetConnection.Click
Dim OrdinalIndex As Integer = CType(cboConnections.SelectedItem, ConnectionInfo).Index - 1
CustomersTableAdapter.Connection.Close()
CustomersTableAdapter.Connection.ConnectionString = connections.Items(OrdinalIndex).ConnectionString
CustomersTableAdapter.Connection.Open()
CustomersTableAdapter.Fill(Me.Database1DataSet.Customers)
cboConnections.SelectedIndex = OrdinalIndex
End Sub
Private Sub cmdGetConnection_Click(sender As Object, e As EventArgs) Handles cmdGetConnection.Click
Dim sb As New System.Text.StringBuilder
sb.AppendLine(cboConnections.Text)
sb.AppendLine(connections.Items(connections.CurrentConnectionIndex).ConnectionString)
MessageBox.Show(sb.ToString)
End Sub
End Class
Screenshot

Timer stops working after sometime in windows service

I have a windows service that runs every one minute and performs some DB and IO operations.
Everything works fine but I have observed that after some time service does not work.
It shows started but nothing is happening.
1) Can this be bcoz of timer being collected by GC? If yes then whats wrong with my code.
2) I have used timer.enable in onstart and onstop, should i use timer.start and timer.stop.
3) If I want to user Threading.Timer then what all do I need to change in the following code.
Any help will be appreciated
Imports System.ServiceProcess
Imports System.Timers
Imports System.Configuration
Public Class ReportingImmediate
Inherits ServiceBase
#Region "Members"
Private ReadOnly time As Timers.Timer
Private ReadOnly ReportingHelper As ReportingHelper
Private ReadOnly timeInterval As String
#End Region
Public Sub New()
' Initialize Logs
InitializeComponent()
' Initialize other components
time = New System.Timers.Timer()
timeInterval = ConfigurationManager.AppSettings("ImmediateReRunInterval")
time.Interval = Integer.Parse(timeInterval)
AddHandler time.Elapsed, AddressOf TimeElapsed
objReportingHelper = New ReportingHelper()
End Sub
#Region "Timer Event"
''' <summary>time Elapsed</summary>
''' <param name="sender">The object that raised the event sender</param>
''' <param name="e">Event data passed to the handler e</param>
Private Sub TimeElapsed(sender As Object, e As ElapsedEventArgs)
time.Enabled = False
objReportingHelper.GetReportsForExecutions(1)
' Enable the Timer
time.Enabled = True
time.Interval = Integer.Parse(timeInterval)
End Sub
#End Region
#Region "Service Events"
''' <summary>On Start</summary>
''' <param name="args">Arguments</param>
Protected Overrides Sub OnStart(args As String())
time.Enabled = True
End Sub
''' <summary>On Stop</summary>
Protected Overrides Sub OnStop()
time.Enabled = False
End Sub
#End Region
End Class

Nested class & readonly properties

Outside the class I'd like to be able to access CCTV.Camera.Brightness property as ReadOnly, but methods within the CCTV class should be able to modify the Brightness property. Please can you advise on how to achieve this?
I think I need to add an Interface that exposes some properties and hides others, but I'm not sure of the implementation. [Note the constructor and main sub are obviously contrived for this example and testing].
Public Class CCTV
Public Class Camera
Public Property Name as String
Public Property Brightness as Integer
End Class
Dim cameras as New Dictionary(Of String, Camera)
Public Sub New()
Dim cam As New Camera
cam.Name = "driveway"
cam.Brightness = 5
cameras.Add(cam.Name, cam)
End Sub
Public Sub ChangeBrightness(value as Integer)
cameras("driveway").Brightness = value
End Sub
End Class
Sub main()
Dim MyCCTV = new CCTV
MyCCTV.ChangeBrightness(10)
if MyCCTV("driveway").Brightness = 10 then Console.Write("Brightness is 10")
End Sub
Get getter and the setter of a property can have different accessibility modifiers. In this case you want Brightness to be readable by everybody but only the code you trust should be able to write it. You do so like this:
Public Class Camera
Private _brightness As Integer
Public Property Brightness As Integer
Get
Return _brightness
End Get
Friend Set(value As Integer)
_brightness = Value
End Set
End Property
'' etc...
End Class
Note the added Friend keyword, it limits access to the code in the same project that Camera class is a part of. It can also be Private or Protected if you want to limit access to only code inside the Camera class or its derived classes.
no interface is need in this case.You simply have to create your property as readonly.when you set the property as readonly assume that the value will stored within a private variable and at that point should be better pass it or to a method or to the subnew using overloads methods let me show you an example:
Public Class CCTV
Public Class Camera
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Brightness As String
Public ReadOnly Property Brightness() As String
Get
Return _Brightness
End Get
End Property
''' <summary>
''' Defautl sub new method
''' </summary>
''' <remarks></remarks>
Sub New()
End Sub
''' <summary>
''' second overload
''' </summary>
''' <param name="cameraName"></param>
''' <param name="brightness"></param>
''' <remarks></remarks>
Sub New(ByVal cameraName As String, ByVal brightness As Integer)
Me.Name = cameraName
Me._Brightness = brightness
End Sub
''' <summary>
''' Change brigthness
''' </summary>
''' <param name="value"></param>
''' <remarks></remarks>
Public Sub setUpCameraBrightness(ByVal value As Integer)
'take care to use private varibale so it will reflcet changes into propetry readonly
Me._Brightness = value
End Sub
End Class
Dim cameras As New Dictionary(Of String, Camera)
Public Sub New()
'two differnet approach
'first overaload:
Dim cam As New Camera()
cam.Name = "yourcamname"
cam.setUpCameraBrightness(10)
cameras.Add(cam.Name, cam)
''second approch declarative value:
Dim cam2 As New Camera("yourcamname", 10)
cameras.Add(cam2.Name, cam2)
End Sub
Public Sub ChangeBrightness(value As Integer)
cameras("driveway").setUpCameraBrightness(100)
End Sub
End Class
Sub main()
Dim MyCCTV = New CCTV
MyCCTV.ChangeBrightness(10)
If MyCCTV("driveway").Brightness = 10 Then Console.Write("Brightness is 10")
End Sub

Stop the screen saver and hibernation temporarily in VB.Net

I have a very long running syncronization task that cannot be interrupted by the screen saver or aggressive power saving modes. I want to make a single api call to stop power save mode and then restore it once the task is done.
The following code is peaced together from various other posts but it has no effect on XP's power management settings. What am I doing wrong? TIA!
Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As Long
Public Enum EXECUTION_STATE As Integer
ES_CONTINUOUS = &H80000000
ES_DISPLAY_REQUIRED = &H2
ES_SYSTEM_REQUIRED = &H1
ES_AWAYMODE_REQUIRED = &H40
End Enum
Public Shared Sub PowerSaveOff()
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
End Sub
Public Shared Sub PowerSaveOn()
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
End Sub
Here are the systems screensaver and powermode settings:
alt text http://img87.imageshack.us/img87/1600/25251376.jpg
alt text http://img403.imageshack.us/img403/8145/73347627.jpg
I added EXECUTION_STATE.ES_SYSTEM_REQUIRED, which "Forces the system to be in the working state by resetting the system idle timer," and prevents the system from entering a power saving state. I also changed the API calling convention to use EXECUTION_STATE, wrapped everything in a simple utility class with some documentation.
''' <summary>
''' Simple power manager class that enables applications to inform the system
''' that it is in use, thereby preventing the system from entering the sleeping
''' power state or turning off the display while the application is running.
''' </summary>
Public Class PowerManager
#Region " Private Sub New "
Private Sub New()
'keep compiler from creating default constructor to create utility class
End Sub
#End Region
''' <summary>
''' Enables applications to inform the system that it is in use, thereby preventing the system from entering the sleeping power state or turning off the display while the application is running.
''' </summary>
''' <param name="esFlags">The thread's execution requirements. This parameter can be one or more of the EXECUTION_STATE values.</param>
''' <returns>
''' <para>If the function succeeds, the return value is the previous thread execution state, as a EXECUTION_STATE value.</para>
''' <para>If the function fails, the return value is NULL.</para>
'''</returns>
''' <remarks>
''' <para>This function does not stop the screen saver from executing.</para>
''' <para>http://msdn.microsoft.com/en-us/library/aa373208.aspx</para>
''' </remarks>
Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
Public Enum EXECUTION_STATE As Integer
''' <summary>
''' Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
''' </summary>
ES_CONTINUOUS = &H80000000
''' <summary>
''' Forces the display to be on by resetting the display idle timer.
''' </summary>
ES_DISPLAY_REQUIRED = &H2
''' <summary>
''' Forces the system to be in the working state by resetting the system idle timer.
''' </summary>
ES_SYSTEM_REQUIRED = &H1
End Enum
Public Shared Function PowerSaveOff() As EXECUTION_STATE
Return SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
End Function
Public Shared Function PowerSaveOn() As EXECUTION_STATE
Return SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
End Function
End Class
Public Class Form1
Private _cancel As Boolean
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'set system standby to one minute
_cancel = False
PowerManager.PowerSaveOff()
Do Until _cancel
My.Application.DoEvents()
Loop
PowerManager.PowerSaveOn()
'do not forget to restore your power settings
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
_cancel = True
End Sub
End Class
This works for me:
Private Const SPI_SETSCREENSAVEACTIVE = 17
Private Const SPI_SETSCREENSAVETIMEOUT = 15
Private Const SPIF_SENDWININICHANGE = &H2
Private Const SPIF_UPDATEINIFILE = &H1
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Long, ByVal fuWinIni As Long) As Long
Private Sub Form_Load()
Call SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, 0, SPIF_UPDATEINIFILE + SPIF_SENDWININICHANGE)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, 0, SPIF_UPDATEINIFILE + SPIF_SENDWININICHANGE)
End Sub
Microsoft.Win32.RegistryKey rkScreenSaver =
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(#" Control Panel\Desktop", true );
if ( (string)rkScreenSaver.GetValue( "ScreenSaveActive" ) == "1" )
{
rkScreenSaver.SetValue( "ScreenSaveActive", "0" );
rkScreenSaver.Close( );
}
This should work for you.
Alternatively why not extend the time for the screen saver to 10 minutes?