If I understand your question properly, this is a major reason variables
exist. They hold onto information in memory for as long as you need it.
Typically, you will want to minimize impact on your database. To do so, you
would read the data into variables, then work with the data, and finally
commit any changes back to the database.
Here is some sample code for you to browse and understand a little better.
This is a high stripped down set of code, and is not intended to be the only
approach when working with data. (I appologize to everyone on slow
connections, the code example is kinda long.)
Public Class SomeForm
'psuedo class to represent a form
' only the require portions of the form
' code logic are represented.
'Assume a datagrid to list multiple rows of data
' two text boxes to be bound to a single instance of data
' a button to save the data
Private Sub BindDataList()
Dim objList As System.Collecti ons.Generic.Lis t(Of GlobalData.myDa ta)
'Clean up our datagrid, incase are refreshing the list
Me.dbgData.data Source = Nothing
Me.dbgData.Tabl eStyles.Clear()
'Read the data in
objList = GlobalData.Data list("somefilte r")
'Bind it to our datagrid
Me.dbgData.data Source = objList
'Optionally set up tablestyles for the datagrid
'Code ommitted
End Sub
Private Sub BindDataItem()
'This would be called each time a row is clicked on the datagrid
Dim objDataList As System.Collecti ons.Generic.Lis t(Of
GlobalData.myDa ta)
Dim objData As GlobalData.myDa ta
'Clear any existing bindings that may be present
Me.txtValue1.da tabindings.clea r()
Me.txtValue2.da tabindings.clea r()
'Get the list the datagrid is bound to
objDataList = CType(Me.dbgDat a.datasource, ListBox(Of mydata))
'Get the selected row
objData = CType(objDataLi st(Me.dbgData.C urrentRowIndex) ,
GlobalData.myDa ta)
'Set this row to the current row
GlobalData.Load Data(objData)
Dim b As DataBinding
'bind value1
b = New DataBinding("Te xt", objData, "Value1")
Me.txtValue1.Da tabindings.Add( b)
'bind value2
b = New DataBinding("Te xt", objData, "Value2")
Me.txtValue2.Da tabindings.Add( b)
End Sub
Private Sub btnSave_Click(B yVal sender, ByVal e) Handles btnSave.Click
GlobalData.Save Data()
End Sub
End Class
Public Class GlobalData
Private Shared pstrConnectionS tring As String
Private Shared pobjCurrentData As myData
Shared Sub New()
'Initialize our connectionstrin g
pstrConnectionS tring = "ConnectionInfo Here"
End Sub
''' <summary>
''' Load on piece of data into memory
''' </summary>
''' <param name="filter"></param>
''' <remarks>The filter would ideally reference a unique column in the
database to return one row of data.</remarks>
Public Shared Sub LoadData(ByVal filter As String)
'Using filter information, load your data
'My example will just hardcode the values
pobjCurrentData = New myData
With pobjCurrentData
.Value1 = "Value1"
.Value2 = "Value2"
End With
End Sub
''' <summary>
''' Load one piece of data into memory
''' </summary>
''' <param name="dataObjec t"></param>
''' <remarks>Use this method if you have a list of data myData
objects.</remarks>
Public Shared Sub LoadData(ByVal dataObject As myData)
pobjCurrentData = dataObject
End Sub
''' <summary>
''' This is the target row of data that we are working with.
''' </summary>
''' <value></value>
''' <remarks></remarks>
Public Shared ReadOnly Property CurrentData() As myData
Get
Return pobjCurrentData
End Get
End Property
''' <summary>
''' Here we would save the data back to the database
''' </summary>
''' <remarks></remarks>
Public Shared Sub SaveData()
'Check if there are changes to worry about
If CurrentData.Has Changes Then
'Save the data
' database call is ommitted here
End If
End Sub
''' <summary>
''' Unload the current data from memory
''' </summary>
''' <remarks>Boun d object may still have a reference to the data. That
is ok, since the only way to save it is in the SaveDataMethod</remarks>
Public Shared Sub UnloadData()
pobjCurrentData = Nothing
End Sub
''' <summary>
''' Simulate loading multiple rows of data from the database.
''' </summary>
''' <param name="filter"></param>
''' <returns></returns>
Public Shared Function Datalist(ByVal filter As String) As
System.Collecti ons.Generic.Lis t(Of myData)
'again, I will simulate loading from the database
Dim objReturn As System.Collecti ons.Generic.Lis t(Of myData)
objReturn = New System.Collecti ons.Generic.Lis t(Of myData)
For i As Integer = 1 To 25
Dim objMyData As myData
objMyData = New myData
With objMyData
.Value1 = System.Guid.New Guid.ToString(" N")
.Value2 = System.Guid.New Guid.ToString(" N")
End With
objReturn.Add(o bjMyData)
Next
Return objReturn
End Function
''' <summary>
''' This class represents one row of data
''' </summary>
''' <remarks></remarks>
Public Class myData
'Data values read in from the database
Private pobjDatabaseVal ue1 As String
Private pobjDatabaseVal ue2 As String
'Working set of data, a copy if you will
Private pobjCurrentValu e1 As String
Private pobjCurrentValu e2 As String
'Flags to determine is the data is null
Private pblnIsNullValue 1 As Boolean = True
Private pblnIsNullValue 2 As Boolean = True
Public Property Value1() As String
Get
'Test if our value is null, if so throw an exception
If pblnIsNullValue 1 Then Throw New
System.Data.Dat aException("Val ue1 is Null")
'Otherwise return the value
Return pobjCurrentValu e1
End Get
Set(ByVal value As String)
pobjCurrentValu e1 = value
pblnIsNullValue 1 = False
End Set
End Property
Public Property Value2() As String
Get
'Test if our value is null, if so throw an exception
If pblnIsNullValue 2 Then Throw New
System.Data.Dat aException("Val ue2 is Null")
'Otherwise return the value
Return pobjCurrentValu e2
End Get
Set(ByVal value As String)
pobjCurrentValu e2 = value
pblnIsNullValue 2 = False
End Set
End Property
Public ReadOnly Property IsValue1Null() As Boolean
Get
Return pblnIsNullValue 1
End Get
End Property
Public ReadOnly Property IsValue2Null() As Boolean
Get
Return pblnIsNullValue 2
End Get
End Property
Public Sub SetValue1Null()
Value1 = String.Empty
pblnIsNullValue 1 = True
End Sub
Public Sub SetValue2Null()
Value2 = String.Empty
pblnIsNullValue 2 = True
End Sub
Public ReadOnly Property HasChanges() As Boolean
Get
If pobjCurrentValu e1.CompareTo(po bjDatabaseValue 1) <0 Then
Return True
If pobjCurrentValu e2.CompareTo(po bjDatabaseValue 2) <0 Then
Return True
End Get
End Property
End Class
End Class
"Jeff" <no****@george. comwrote in message
news:46******** *************** @free.teranews. com...
>
"Earl" <br******@newsg roups.nospamwro te in message
news:eH******** ******@TK2MSFTN GP06.phx.gbl...
>You should call it as a Function.
Public Function xxx() as Integer
Dim i as Integer = 9
Return i
End Function
Sub Test3
label1.text = xxx
End Sub
Thanks (both Amdrit and Earl). Reference the above, if I use the method
above and access xxx several times from several different subs and if the
value I'm placing into the variable is taken from a cell in a database,
will the database be accessed each time or is there some type of caching
involved where the db access actually occurs once?
In other words, is the above still a good strategy if instead of the
simple example: "Dim i as Integer = 9" i is assigned after a number of
commands that read from a database and then xxx is access multiple times
from several subs.
Jeff
--
Posted via a free Usenet account from http://www.teranews.com