Corey Scheich
asked on
Maintaining a dataset across Controls
I have a group of custom controls and am attempting to maintain a DataSet across the controls I want to watch events of the dataset on one control (TreeView) to reload the data whenever the database has changed. It appears that when I assign the dataset to the separate controls separate copies are being created instead of them referencing the same dataset. I am using properties to hold the dataset.
Here is the property declaration being used
Private WithEvents _DataSet As siDataSet
''' <summary>
''' Gets or Sets the DataSet that is being read by this instance of the Tree
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property DataSet() As siDataSet
Get
Return _DataSet
End Get
Set(ByVal value As siDataSet)
_DataSet = value
End Set
End Property
''' <summary>
''' Raised when tree has been dirtied by a database Change
''' </summary>
''' <param name="Sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub _DataSet_dirtyTree(ByVal Sender As Object, ByVal e As System.EventArgs) Handles _DataSet.dirtyTree
RefreshTree()
End Sub
and it is being set from the main form using
Public Sub New ()
InitializeComponent()
myTreeView.DataSet = me.siDataSet1
End Sub
Now when running the code it appears that this is creating a copy of the dataset instead of pointing to the same instance. The thing that gets me is I used a similar approach earlier except I was passing the dataset from a form to some module classes and making modifications but in that case tha dataset was updating properly. Any Ideas what I am missing here? How can I cause the separate classes to reference the same instance of the DataSet.
Thanks
Corey
Here is the property declaration being used
Private WithEvents _DataSet As siDataSet
''' <summary>
''' Gets or Sets the DataSet that is being read by this instance of the Tree
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property DataSet() As siDataSet
Get
Return _DataSet
End Get
Set(ByVal value As siDataSet)
_DataSet = value
End Set
End Property
''' <summary>
''' Raised when tree has been dirtied by a database Change
''' </summary>
''' <param name="Sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub _DataSet_dirtyTree(ByVal Sender As Object, ByVal e As System.EventArgs) Handles _DataSet.dirtyTree
RefreshTree()
End Sub
and it is being set from the main form using
Public Sub New ()
InitializeComponent()
myTreeView.DataSet = me.siDataSet1
End Sub
Now when running the code it appears that this is creating a copy of the dataset instead of pointing to the same instance. The thing that gets me is I used a similar approach earlier except I was passing the dataset from a form to some module classes and making modifications but in that case tha dataset was updating properly. Any Ideas what I am missing here? How can I cause the separate classes to reference the same instance of the DataSet.
Thanks
Corey
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Check out the MSDN for byref and byval; passing a variable byval makes a copy of the object, so it will look the same at the time its passed but it is a spearte instance and will not update other instances.
Change the
Set(ByVal value As String)
_MyName = value
End Set
to
Set(byRef value As String)
_MyName = value
End Set
and prove me right (or, even possibly, wrong) ;-)
Change the
Set(ByVal value As String)
_MyName = value
End Set
to
Set(byRef value As String)
_MyName = value
End Set
and prove me right (or, even possibly, wrong) ;-)
ASKER
Unfortunately I won't be able to do any testing until later tonight. I will try the byref thing, I am also going to do some more testing to make sure the dataset on the one control is actuly updating there just incase it isn't.
lojk
Did you try to do that? With the Set for either the MyName or the Dataset properties in the code I posted? If so, did you not get this message?
>>
'Set' parameter cannot be declared 'ByRef'
<<
And one - but by no means the only - reason why I said "I don't think so" was this
http://msdn2.microsoft.com/en-us/library/eek064h4.aspx
And, in particular, this bit
>>
This means that when you pass a reference type by value, the procedure code has a pointer to the underlying element's data, even though it cannot access the underlying element itself. For example, if the element is an array variable, the procedure code does not have access to the variable itself, but it can access the array members.
<<
For "array", read "dataset".
Still, we'll see what Corey2 discovers when he returns to his real app ;-)
Roger
Did you try to do that? With the Set for either the MyName or the Dataset properties in the code I posted? If so, did you not get this message?
>>
'Set' parameter cannot be declared 'ByRef'
<<
And one - but by no means the only - reason why I said "I don't think so" was this
http://msdn2.microsoft.com/en-us/library/eek064h4.aspx
And, in particular, this bit
>>
This means that when you pass a reference type by value, the procedure code has a pointer to the underlying element's data, even though it cannot access the underlying element itself. For example, if the element is an array variable, the procedure code does not have access to the variable itself, but it can access the array members.
<<
For "array", read "dataset".
Still, we'll see what Corey2 discovers when he returns to his real app ;-)
Roger
(>blushing, quietly insists he thinks before speaking in future<)
Yeah hadnt actually tried it but then the reason for that is i tend to always pass these things byref via a constructor (real or alias) for a class anyway (its an old VB6 Habit so i wouldn't like to say is right or wrong) and so wouldnt normally use a property and just basically assumed (tut-tut) was correct. Nor indeed had i apparently fully read your previous post either, Sancler. How really rather embarassing.
Sorry if i have added any confusion corey2.
Everyday's a School Day, eh?
Yeah hadnt actually tried it but then the reason for that is i tend to always pass these things byref via a constructor (real or alias) for a class anyway (its an old VB6 Habit so i wouldn't like to say is right or wrong) and so wouldnt normally use a property and just basically assumed (tut-tut) was correct. Nor indeed had i apparently fully read your previous post either, Sancler. How really rather embarassing.
Sorry if i have added any confusion corey2.
Everyday's a School Day, eh?
lojk
No problem. As I did say, right at the start, "I wondered about that", too. But then I thought of all the times when I did pass datasets - or at least datatables - ByVal without any apparent problem, so started rooting a bit and experimenting.
Roger
No problem. As I did say, right at the start, "I wondered about that", too. But then I thought of all the times when I did pass datasets - or at least datatables - ByVal without any apparent problem, so started rooting a bit and experimenting.
Roger
ASKER
I second every bit of roger's last response including the past experience.
ASKER
Found the problem.
I had created my custom controls in the Designer and connected the DataSet to the DataView in that interface. This in turn was connecting the dataset that was initialized with the control instead of the one that was passed to it afterward. Then once the dataset of the control was re-assigned to the dataset of my main form I needed to reconnect the dataview and BindingManager to the correct dataset. The troubling thing about this all is that initially I directly bound to the DataSet thinking that if it were changed that all the dataviews would follow because essencially they would still be referencing the Declared Variable. But infact they are referencing the underlying element.
I am going to accept Slancer's first comment that there must be something else going on as an assisted answer, and this as the answer.
Thank you both for your time.
Corey2
I had created my custom controls in the Designer and connected the DataSet to the DataView in that interface. This in turn was connecting the dataset that was initialized with the control instead of the one that was passed to it afterward. Then once the dataset of the control was re-assigned to the dataset of my main form I needed to reconnect the dataview and BindingManager to the correct dataset. The troubling thing about this all is that initially I directly bound to the DataSet thinking that if it were changed that all the dataviews would follow because essencially they would still be referencing the Declared Variable. But infact they are referencing the underlying element.
I am going to accept Slancer's first comment that there must be something else going on as an assisted answer, and this as the answer.
Thank you both for your time.
Corey2
try this..
Public Property DataSet() As siDataSet
Get
Return _DataSet
End Get
Set(BYREF value As siDataSet)
_DataSet = value
End Set
End Property
or
private _dataSet as siDataset
Public sub SetDataSet(BYREF ds as Dataset)
_dataset = ds
end sub
Public Function GetDataSet as siDataseSet
return _dataset
end function
or to test, even declare that DataSet as public in the declarations, bypassing any other sub to access.
ie
Public DataSetToUse as siDataSet
hth..