Results 1 to 7 of 7

Thread: Jeepers is this really the right way to do this??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518

    Unhappy Jeepers is this really the right way to do this??

    I've been looking for a way to refer to a DataSet created in one form with code in a second form, and I've found what appears to be a way to do it (playing with it right now) - is this the "right" way?? It seems majorly hackish!

    http://msdn.microsoft.com/library/de...adingtonet.asp


    Global data in .NET
    Earlier I mentioned that global variables are not available in Visual Basic .NET, and now I'm going to tell you how to make something global. How will you ever trust me after this? Actually, I am not being dishonest in either case; global variables are not allowed in Visual Basic .NET, but you can achieve similar functionality using Shared (called static in C#) class members. A Shared class member, as used by the Visual Basic Upgrade Wizard when it adds the DefInstance property to your forms, is available without creating an instance of a class and, if it is a property, its value is shared across your entire application. You could therefore create a class like this:

    Code:
    Public Class myForms
        Private Shared m_CustomerForm As CustomerForm
        Public Shared Property CustomerForm() As CustomerForm
            Get
                Return m_CustomerForm
            End Get
            Set(ByVal Value As CustomerForm)
                m_CustomerForm = Value
            End Set
        End Property
    End Class
    When you first create an instance of your form you could store it into this class:
    Code:
    Dim myNewCust As New CustomerForm()
    myNewCust.Show()
    myForms.CustomerForm = myNewCust
    After the CustomerForm property has been populated with an instance of your form, you could then use this class to access that same instance from anywhere in your code:
    Code:
    Module DoingStuffWithForms
        Sub DoExcitingThings()
            myForms.CustomerForm.Text = _
                DateTime.Now().ToLongTimeString
        End Sub
    End Module
    Storing your form in this manner comes as close to Visual Basic 6.0's Global variables as you are going to get. The next level of variable scope below this level is class (module, class or form, actually) scope, where a variable is declared within a class and is available to any code in that class, and below that is procedure scope, where a variable is local to a single routine.


  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Thats really wierd because you can use modules in .net the same as in vb6. You can just declare a public dataset in a module and use it on any form in the project. You could also pass the dataset from form to form in the constructor or just through a method.

    I wonder if that info is based on a beta build of .net.

    Although the way they post works to, so what was the question? Or were you saving everyone else some time? (So they don't have to search for the same info)

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The more I look at this code the more confused I get. It seems that you could cut out the module completely, unless I am missing something, especially with any object other than a form.
    VB Code:
    1. Public Class myForms
    2.     Private Shared m_CustomerDS As New Dataset()
    3.     Public Shared Property CustomerDS() As DataSet
    4.         Get
    5.             Return m_CustomerDS
    6.         End Get
    7.         Set(ByVal Value As DataSet)
    8.             m_CustomerDS = Value
    9.         End Set
    10.     End Property
    11. End Class
    12.  
    13. 'then inside the same form you can just reference the dataset directly
    14. m_CustomerDS.ReadXML("MyFile.xml")
    15.  
    16. 'and from another form like this
    17. Msgbox(myForms.CustomerDS.Tables(0).Rows(1).Item("Name"))

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    My question was how to refer to a dataset created in Form1 with code in Form2 - although if you say I should have started from a module rather than a form I guess I should go bang my head against the wall for a few minutes and then do it that way. I'm using the DataSet builder component, which does not appear when working with modules. I have about 20 tables to build, some of which have around 10 fields, and it's a good bit of typing, so I was trying to save the effort - but what did I save really

    And yes, I should probably be using an XML schema to define all these tables, but I'm not smart enough to do it that way quite yet. Maybe in a week or two

    I was just passing this along in case there was an easy way to use the dataset collection editor and still have the dataset be shareable between windows (looks like this is not the case without the property code above?)

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could cut and paste the code vb generated for you which is usually in the Region at the top of a form. Also if you don't need to share the dataset with everything in the project but just those two froms then pass it to the other form via constructor or method.

    VB Code:
    1. 'if Form1 makes the dataset and makes Form2 then
    2.  
    3. 'in form2
    4. Private _ds as Dataset
    5.  
    6. Public Sub New(ByRef ds as Dataset)
    7.    Me.New()
    8.    _ds=ds
    9. End Sub
    10.  
    11. 'and in form1 if the created dataset is named ds
    12.    dim frm as New Form2(ds)
    13.    frm.Show

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I thought about that, but the dataset code generated by Windows Form Designer is a) interspersed with tons of other miscellaneous crap from other controls etc. in the order they were created and b) extraordinarily ugly due to my allowing the collection editor to select a control name automatically for the rows rather than trying to keep each control name unique by hand (something like 130 controls just for the rows in my dataset). Thanks for the tip of passing a reference, I'll try it that way if I can't work up the motivation to rebuild my dataset in a module by hand. *rubs sore forehead*

    What I need is an automatic clue-bat to bonk me on the head about once an hour. Thanks again!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    I tried out the module way, and as you originally suggested, it makes a giant amount of sense to me now to define the dataset by hand in a module. By god, I'll save TENS OF NANOSECONDS' worth of overhead!!

    Guess I better get cracking on redoing my dataset *goes off to bang head some more*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width