locked
Sharing Data RRS feed

  • Question

  • Hi,

    I am trying to add a class that I class DataManager to my application.

    I declare it in my mainform,

    Public dataManager As New DataManager

     

    And then a in other forms throught-out my app I will add data, and access data like so,

    add some data,

     

    My.Forms.FormMain.dataManager.ConfigDataNumber = 6

     

    Access some data,

     

    configDataNumber = My.Forms.FormMain.dataManager.ConfigDataNumber

     

    I control access to data via properties, in DataManager,

     

    Private _configDataNum As Integer

    Public Property ConfigDataNumber() As Integer

    Get

    Return _configDataNum

    End Get

    Set(ByVal value As Integer)

    _configDataNum = value

    End Set

    End Property

     

    It strores data correctly, however when I access the data there is nothing there.

    Not sure what the issue is here.

    Thanks

    Jeff

    Friday, May 4, 2007 3:44 PM

Answers

  •  

    VB Is not case sensitive so try this

     

    Public MyDataManager As New DataManager

     

     and then instead of referencing it like this

    My.Forms.FormMain.dataManager.ConfigDataNumber = 6

     

    All you would have to do is this:

     

    MyDataManager.ConfigDataNumber=6

    Friday, May 4, 2007 8:39 PM
    Moderator
  • if you want it to be truly global to the app, then put it in a module. A module in .NET is nothing more than a class that everything is considered SHARED without having to declare it shared.

     

    If you want it to be a public variable in a form (as your code indicates) then you can only access it from other parts of your application if that part has a valid reference to an instance of your FormMain class...

     

    as in,

     

    Dim F as New FormMain

    F.myDataManager

     

    That would work, but

     

    FormMain.myDataManager will not (or if you are using the application framework, it will be pulled from the default instance of FormMain, versus any already created instance)

    Monday, May 7, 2007 2:25 PM
    Moderator
  •  

    Hi,

     

    You can also set a "default value" using the OPTIONAL statement.

     

    In the following code the default value is zero.>>

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    'Note here the "NEW" Sub doesn't require a value passing to it. :-)

    Dim test As New DataManager

     

    End Sub

     

    End Class

    '----------------------------------------------------

    'Here is a PUBLIC CLASS straight after FORM code. :-)

    Public Class DataManager

    'In a CLASS this would usually be PRIVATE.

    Public Shared _configDataNum As Integer

    'Add a NEW sub to set the value in your "NEW" statement.

    Public Sub New(Optional ByVal dataNum As Integer = 0)

    _congigDataNum = dataNum

    End Sub

     

    Public Property ConfigDataNumber() As Integer

    Get

    Return _configDataNum

    End Get

    Set(ByVal value As Integer)

    _configDataNum = value

    End Set

    End Property

    End Class

     

     

     

    Regards,

     

    S_DS

     

    Monday, May 7, 2007 9:18 PM
  •  Spidermans_DarkSide - VSIP wrote:

     

    As far as i'm aware PUBLIC exposes the variable to all FORMS and Functions and Sub's etcetera,

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    'Set test = 5.

    Dim test As New DataManager(5)

     

    End Sub

     

    End Class

     

    Regards,

     

    S_DS

     

     

    In both of your examples the "test" datamanager would only be visible within the form1.formload event...the example given by me where it is decalred as a public form field would make it visible to other classes with visibility to the original declaring form...or as klienma said for global visiblility it could be declared in a module....in either case the OP has found his answer by utilizing a form reference to the public variable

     

    Furthermore by exposing the field "__configDataNum" as a public variable (shared or not)...you have defeated the purpose of the property

    Monday, May 7, 2007 10:49 PM
    Moderator
  • Try Public Shared

     

    Class MainForm

    Public Shared dataManager as New DataManager

     

    Tuesday, May 8, 2007 2:29 PM

All replies

  • It sounds like an instance referencing problem...Instead of having datamanager as a class within formMain make it a standalone class
    Friday, May 4, 2007 5:45 PM
    Moderator
  • Hi DMan1,

     

    I found that if I make the data in the class shared, it works fine.

    In makeing it a standalone class, how you exactly do that?

    You mean do not create an instance of it in my mainform?

    If so, how would I write and read to it?

    Make the class itself shared?

     

    Thanks

    Jeff

    Friday, May 4, 2007 6:31 PM
  • If making the data shared corrects the issue then it is def a instance referencing issue....

    to make a standalone class....go to Project....Add New Item...Class.  Name your class DataManager and then cut all of the existing properties and methods to your new class file

    Friday, May 4, 2007 7:44 PM
    Moderator
  • Hi DMan1,

     

    That is actually how I did create this DataManager class.

    Then I created an instance of it in the mainform.

    I thought doing that would then give the me scope I needed, did not since I had to used shared.

     

    So is there a way I can reference this class without creating an instance?

     

    Thanks

    Jeff

     

    Friday, May 4, 2007 8:01 PM
  •  

    VB Is not case sensitive so try this

     

    Public MyDataManager As New DataManager

     

     and then instead of referencing it like this

    My.Forms.FormMain.dataManager.ConfigDataNumber = 6

     

    All you would have to do is this:

     

    MyDataManager.ConfigDataNumber=6

    Friday, May 4, 2007 8:39 PM
    Moderator
  •  

    Hi DMan1,

    What you have outlined above is exactly the performance I want/expect. However it does not work.

    In my mainForm class I have,

     

    Public Class FormMain

     

    Public myDataManager As New DataManager

     

    And the only place I can do myDataManager.ConfigDataNumber = 6

    is in the MainForm class. Any other class does not recognize myDataManager.

    Not sure what to make of this.

     

    Also, have you ever had any trouble when responding to posts in the forum being picked-off by the

    below page. Whenever I try to respond I am routed to the below page.

     

    http://www.microsoft.com/worldwide/default.aspx

     

    Thanks

    Jeff

    Monday, May 7, 2007 12:36 PM
  • if you want it to be truly global to the app, then put it in a module. A module in .NET is nothing more than a class that everything is considered SHARED without having to declare it shared.

     

    If you want it to be a public variable in a form (as your code indicates) then you can only access it from other parts of your application if that part has a valid reference to an instance of your FormMain class...

     

    as in,

     

    Dim F as New FormMain

    F.myDataManager

     

    That would work, but

     

    FormMain.myDataManager will not (or if you are using the application framework, it will be pulled from the default instance of FormMain, versus any already created instance)

    Monday, May 7, 2007 2:25 PM
    Moderator
  • Hi kleinma,

     

    I have not ever used a module.

    So could I name a module, such as moudle dataManager. Put private data in it, and then use properties to control access?

     

    Thanks

    Jeff

     

    Monday, May 7, 2007 3:04 PM
  • Yes you could put private members in a module and expose them via public properties or functions...

     

    Just remember that you should only making something global in an application if there is really a need for it. Also take into account the implications of having something global, that the value can be changed by a number of different places, which can sometimes produce unexpected results.

    Monday, May 7, 2007 3:17 PM
    Moderator
  • Hi kleinma,

    Yes, I thought this one out after I emailed you and decided not to go with the module.

    The data itself is being collected in one class(form in this case) and used in another.

    So I am just going to do something like this,

     

    Dim F as New FormMain

    F.myDataManager

     

    Seems like that should work.

    Thanks

    Jeff

    Monday, May 7, 2007 3:54 PM
  •  

    Hi,

     

    You can use a NEW sub in a CLASS to set your value and create a PUBLIC CLASS straight after FORM code like this.

     

    The CLASS CODE doesn't have to be separate.

     

    As far as i'm aware PUBLIC exposes the variable to all FORMS and Functions and Sub's etcetera, SHARED allows a variable to be shared between applications should you create a CLASS LIBRARY FILE.  I've not tested that though.

     

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    'Set test = 5.

    Dim test As New DataManager(5)

     

    End Sub

     

    End Class

     

    '--------------------------------------------------------------------------------------------------------------------

     

    'Here is a PUBLIC CLASS straight after FORM code. :-)

    Public Class DataManager

    'In a CLASS this would usually be PRIVATE.

    Public Shared _configDataNum As Integer

    'Add a NEW sub to set the value in your "NEW" statement.

    Public Sub New(ByVal dataNum As Integer)

    _congigDataNum = dataNum

    End Sub

     

    Public Property ConfigDataNumber() As Integer

    Get

    Return _configDataNum

    End Get

    Set(ByVal value As Integer)

    _configDataNum = value

    End Set

    End Property

    End Class

     

     

    Regards,

     

    S_DS

     

    Monday, May 7, 2007 9:12 PM
  •  

    Hi,

     

    You can also set a "default value" using the OPTIONAL statement.

     

    In the following code the default value is zero.>>

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    'Note here the "NEW" Sub doesn't require a value passing to it. :-)

    Dim test As New DataManager

     

    End Sub

     

    End Class

    '----------------------------------------------------

    'Here is a PUBLIC CLASS straight after FORM code. :-)

    Public Class DataManager

    'In a CLASS this would usually be PRIVATE.

    Public Shared _configDataNum As Integer

    'Add a NEW sub to set the value in your "NEW" statement.

    Public Sub New(Optional ByVal dataNum As Integer = 0)

    _congigDataNum = dataNum

    End Sub

     

    Public Property ConfigDataNumber() As Integer

    Get

    Return _configDataNum

    End Get

    Set(ByVal value As Integer)

    _configDataNum = value

    End Set

    End Property

    End Class

     

     

     

    Regards,

     

    S_DS

     

    Monday, May 7, 2007 9:18 PM
  •  Spidermans_DarkSide - VSIP wrote:

     

    As far as i'm aware PUBLIC exposes the variable to all FORMS and Functions and Sub's etcetera,

     

    Public Class Form1

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

    'Set test = 5.

    Dim test As New DataManager(5)

     

    End Sub

     

    End Class

     

    Regards,

     

    S_DS

     

     

    In both of your examples the "test" datamanager would only be visible within the form1.formload event...the example given by me where it is decalred as a public form field would make it visible to other classes with visibility to the original declaring form...or as klienma said for global visiblility it could be declared in a module....in either case the OP has found his answer by utilizing a form reference to the public variable

     

    Furthermore by exposing the field "__configDataNum" as a public variable (shared or not)...you have defeated the purpose of the property

    Monday, May 7, 2007 10:49 PM
    Moderator
  • Hi,

    A lot of decent idea's.

    I was also thinking I could make the data class a singleton. I have done that in C++.

    Jeff

    Tuesday, May 8, 2007 12:25 PM
  • Unless you create multiple instance of your main form and want each dataManager to be unique, why not declare it Shared?

     

    Public Class FormMain
        Shared dataManager as New DataManager

     

    Then you can access it by:
    FormMain.dataManager.xxx

     

    You will get exactly one instance per application which is I think what you are trying to do.

    Tuesday, May 8, 2007 12:50 PM
  • Hi Larry,

    I have tried that and I get a compile error, the dataManager is not accessable in this context because it is Private.

    The datamanager class is public, I added a public constructor, so I am not sure what it thinks is Private.

    But you are correct, I think that would do what I want.

    What do you think the compiler is referring to here?

    Thanks

    Jeff

    Tuesday, May 8, 2007 1:58 PM
  • Try Public Shared

     

    Class MainForm

    Public Shared dataManager as New DataManager

     

    Tuesday, May 8, 2007 2:29 PM
  • Hi Larry,

    Well that did it. This dataManager class is working very nice now.

    Can now save the data from an entry form to the dataManager, and access that data from another class.

    Exactly what I wanted.

    Thanks Much

    Jeff

    Tuesday, May 8, 2007 4:29 PM