VB .NET - access variable in different form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesti
    New Member
    • Nov 2007
    • 144

    VB .NET - access variable in different form

    hi,

    how to access a variable in a form from a different form?

    i have a DataSet variable in form1 which needs to be accessed by form2. when i type the exact name as the DataSet variable in form1, the IDE tells me that

    Code:
    Name 'DataSet1' is not declared

    thank you
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Not sure why you need to access dataset from form 1? How about updating the data in form 1 then querying database again in form 2?

    Comment

    • jg007
      Contributor
      • Mar 2008
      • 283

      #3
      how have you declared the variable ?, is it public or private if it is public you can use ' form2.variablen ame ' and should be able to read it okay

      ( asuming you are just calling a variable dataset )

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        This isn't necessarily the best option available (see kenobewan''s post) but it is possible.

        Two things need to happen for this to work properly;
        • You need to declare your DataSet as Public
        • You need to pass a reference of the instance of form1 to the second form to use it though.


        The second part is a bit complicated. You need to modify Form2's constructor. If you don't know what that is, read this. The constructor is the Sub New() that your program may or may not contain.

        You need to add a global variable to the form like this:
        Code:
        Dim f1Ref as Form1
        and you need your constructor to take a parameter, like this:
        Code:
        Public Sub New(f1 as Form1)
            f1Ref = f1
        End Sub
        Now, when you want to show Form2, you need to do it like this
        Note that this is the correct way. Just saying Form2.Show() is wrong.
        Code:
        Dim f2 as New Form2(me)
        f2.Show()
        now you can get to Form1's DataSet like this (I'm assuming you called it DataSet1, so just replace that with whatever you need)
        Code:
        Dim ds as DataSet
        ds = f1Ref.DataSet1

        Comment

        • thesti
          New Member
          • Nov 2007
          • 144

          #5
          hi,

          thanks for the replies, it works!

          the DataSet is declared as a Friend, and yes i can access it with Form1.DataSet1.

          well, i create the dataset in form1 as it holds data entered by user and shows them in a datagridview, in form2 i want to show the data in the dataset (table 0) as a report using crystal report.

          but now, i'm getting an error of "the report has no tables".

          how to bind a dataset to a crystal report? should i use an XML schema?

          thank you very much.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Well, I may be wrong, but I believe that you are getting that error because you aren't actually referencing the one that you instantiated in Form1.

            Form1.DataSet1 isn't the correct way to reference that dataset. What that actually means is to try to find a Shared (static) member called DataSet1. I'm not sure how VB.NET handles trying to reference non-static members statically, but in C# you would get a compiler error.

            You need to try it the way I posted previously. That way, you are actually referencing the instance that was created when you ran the program.

            Comment

            • thesti
              New Member
              • Nov 2007
              • 144

              #7
              hi, insertAlias

              thanks for the help.

              yes, your way is logically right, since i'm referencing 'Form1' which is the class name not the object's name. i must be trying to reference a static property. but it just works with 'Form1.DataSet1 '

              here's two thins that i did

              first, i add a new item > DataSet named DataSet.xsd. from this schema (i don't know if this is called a schema file) i add a DataTable from the toolbox and define the columns just like the columns for the DataSet1.Table( 0) in Form1.

              next, i use the code

              Code:
              DataSet1.ReadXmlSchema(SCHEMA_FILE_PATH)

              i've tried your way by making a ref to Form1 from Form2 and it works, then i'm curious what if i just use Form1.DataSet1. and it works also. i don't know, maybe this is a special thing in VB?

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                I think that VB.NET "cheats" for you, since it seems to be a common mistake. Static (shared in vb.net) seems to be a difficult concept for many to grasp, so I guess that they compensate for that.

                Comment

                Working...