variable declaration in form1 accessible in form2...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcel Saucier

    #1

    variable declaration in form1 accessible in form2...

    Hello everybody,

    I want to declare an array in form1, load it in form1 and then access that
    array in form2. New in VB, It took me a while to understand that declaring
    variables right under class form1 make those variables accessibles everywhere
    in that form1:

    Public Class Form1
    Inherits System.Windows. Forms.Form
    Dim TextLine(66) As String
    ...

    So my question is: how to declare variables in form1 that will be accessible
    in form2, form3 & etc. ?

    Thank you

  • CT

    #2
    Re: variable declaration in form1 accessible in form2...

    Declare the variable as Public, instead of Dim. Then reference using
    form1.TextLine. Anotehr option is to have it as a global variable in a
    Module.

    --
    Carsten Thomsen
    Communities - http://community.integratedsolutions.dk

    "Marcel Saucier" <MarcelSaucier@ discussions.mic rosoft.com> wrote in message
    news:11D4AECE-ABFB-419D-982E-2D4B2D920A61@mi crosoft.com...[color=blue]
    > Hello everybody,
    >
    > I want to declare an array in form1, load it in form1 and then access that
    > array in form2. New in VB, It took me a while to understand that declaring
    > variables right under class form1 make those variables accessibles
    > everywhere
    > in that form1:
    >
    > Public Class Form1
    > Inherits System.Windows. Forms.Form
    > Dim TextLine(66) As String
    > ...
    >
    > So my question is: how to declare variables in form1 that will be
    > accessible
    > in form2, form3 & etc. ?
    >
    > Thank you
    >[/color]


    Comment

    • Chris

      #3
      Re: variable declaration in form1 accessible in form2...

      Marcel Saucier wrote:[color=blue]
      > Hello everybody,
      >
      > I want to declare an array in form1, load it in form1 and then access that
      > array in form2. New in VB, It took me a while to understand that declaring
      > variables right under class form1 make those variables accessibles everywhere
      > in that form1:
      >
      > Public Class Form1
      > Inherits System.Windows. Forms.Form
      > Dim TextLine(66) As String
      > ...
      >
      > So my question is: how to declare variables in form1 that will be accessible
      > in form2, form3 & etc. ?
      >
      > Thank you
      >[/color]

      It depends how your forms are set up. Is Form2/3 created in Form1? If
      so there is two easy ways to go about it. Either pass in a reference of
      Form1 and create a property in form1 to access the array... Or create a
      property in Form2 and set it from form1.

      this example forces the array to be passed into form2 when form2 is
      created.
      public class form2
      Dim TextLine() As String

      sub new (Arr as Array)
      TextLine = Arr
      end sub
      end class


      Chris

      Comment

      • Marcel Saucier

        #4
        Re: variable declaration in form1 accessible in form2...

        Hi Carsten,

        Public Class Form1
        Inherits System.Windows. Forms.Form
        Public TextLine(66) As String

        Public Class Form2
        Inherits System.Windows. Forms.Form

        Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        Button1.Text = Form1.TextLine( 1)
        End Sub

        At form1.textline( 1) I have the syntax error message: reference to a
        non-shared member requires an object reference.

        --
        Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
        programmer under Windows since september 2005 ! Most of you will agree to say
        that it was about time to... upgrade !


        "CT" wrote:
        [color=blue]
        > Declare the variable as Public, instead of Dim. Then reference using
        > form1.TextLine. Anotehr option is to have it as a global variable in a
        > Module.
        >[color=green]
        >> "Marcel Saucier" <MarcelSaucier@ discussions.mic rosoft.com> wrote in message[/color]
        > news:11D4AECE-ABFB-419D-982E-2D4B2D920A61@mi crosoft.com...[color=green]
        > > Hello everybody,
        > >
        > > I want to declare an array in form1, load it in form1 and then access that
        > > array in form2. New in VB, It took me a while to understand that declaring
        > > variables right under class form1 make those variables accessibles
        > > everywhere
        > > in that form1:
        > >
        > > Public Class Form1
        > > Inherits System.Windows. Forms.Form
        > > Dim TextLine(66) As String
        > > ...
        > >
        > > So my question is: how to declare variables in form1 that will be
        > > accessible
        > > in form2, form3 & etc. ?
        > >
        > > Thank you
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Chris

          #5
          Re: variable declaration in form1 accessible in form2...

          Marcel Saucier wrote:[color=blue]
          > Hi Carsten,
          >
          > Public Class Form1
          > Inherits System.Windows. Forms.Form
          > Public TextLine(66) As String
          >
          > Public Class Form2
          > Inherits System.Windows. Forms.Form
          >
          > Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
          > System.EventArg s) Handles MyBase.Load
          > Button1.Text = Form1.TextLine( 1)
          > End Sub
          >
          > At form1.textline( 1) I have the syntax error message: reference to a
          > non-shared member requires an object reference.
          >[/color]

          This is because you don't have a reference to the object created as
          form1. look at my post below.

          Comment

          • Marcel Saucier

            #6
            Re: variable declaration in form1 accessible in form2...

            Sorry Chris, I really dont understand your solution. I am probably missing
            very basic concepts.


            --
            Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
            programmer under Windows since september 2005 ! Most of you will agree to say
            that it was about time to... upgrade !


            Comment

            • Chris

              #7
              Re: variable declaration in form1 accessible in form2...

              Marcel Saucier wrote:[color=blue]
              > Sorry Chris, I really dont understand your solution. I am probably missing
              > very basic concepts.
              >
              >[/color]
              Any my first question and I'll be able to help you more.

              What is the relationship between form1 and form2. Is form1 creating
              form2 and doing a form2.showdialo g or form2.show?

              Chris

              Comment

              • Marcel Saucier

                #8
                Re: variable declaration in form1 accessible in form2...

                Hi Chris,

                Finally, in form1, it has to be defined Public SHARED...

                Thank you very much for all of your help.





                --
                Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
                programmer under Windows since september 2005 ! Most of you will agree to say
                that it was about time to... upgrade !


                "Chris" wrote:
                [color=blue]
                > Marcel Saucier wrote:[color=green]
                > > Hello everybody,
                > >
                > > I want to declare an array in form1, load it in form1 and then access that
                > > array in form2. New in VB, It took me a while to understand that declaring
                > > variables right under class form1 make those variables accessibles everywhere
                > > in that form1:
                > >
                > > Public Class Form1
                > > Inherits System.Windows. Forms.Form
                > > Dim TextLine(66) As String
                > > ...
                > >
                > > So my question is: how to declare variables in form1 that will be accessible
                > > in form2, form3 & etc. ?
                > >
                > > Thank you
                > >[/color]
                >
                > It depends how your forms are set up. Is Form2/3 created in Form1? If
                > so there is two easy ways to go about it. Either pass in a reference of
                > Form1 and create a property in form1 to access the array... Or create a
                > property in Form2 and set it from form1.
                >
                > this example forces the array to be passed into form2 when form2 is
                > created.
                > public class form2
                > Dim TextLine() As String
                >
                > sub new (Arr as Array)
                > TextLine = Arr
                > end sub
                > end class
                >
                >
                > Chris
                >[/color]

                Comment

                • Marcel Saucier

                  #9
                  Re: variable declaration in form1 accessible in form2...

                  Public Class Form1
                  Inherits System.Windows. Forms.Form
                  Public Shared TextLine(66) As String

                  It is working when it is define public SHARED...

                  Thank you




                  --
                  Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
                  programmer under Windows since september 2005 ! Most of you will agree to say
                  that it was about time to... upgrade !


                  "Marcel Saucier" wrote:
                  [color=blue]
                  > Hi Carsten,
                  >
                  > Public Class Form1
                  > Inherits System.Windows. Forms.Form
                  > Public TextLine(66) As String
                  >
                  > Public Class Form2
                  > Inherits System.Windows. Forms.Form
                  >
                  > Private Sub Form2_Load(ByVa l sender As System.Object, ByVal e As
                  > System.EventArg s) Handles MyBase.Load
                  > Button1.Text = Form1.TextLine( 1)
                  > End Sub
                  >
                  > At form1.textline( 1) I have the syntax error message: reference to a
                  > non-shared member requires an object reference.
                  >
                  > --
                  > Super Basic programmer under DOS since 1983. Absolutely dummy (& new) VB.NET
                  > programmer under Windows since september 2005 ! Most of you will agree to say
                  > that it was about time to... upgrade !
                  >
                  >
                  > "CT" wrote:
                  >[color=green]
                  > > Declare the variable as Public, instead of Dim. Then reference using
                  > > form1.TextLine. Anotehr option is to have it as a global variable in a
                  > > Module.
                  > >[color=darkred]
                  > >> "Marcel Saucier" <MarcelSaucier@ discussions.mic rosoft.com> wrote in message[/color]
                  > > news:11D4AECE-ABFB-419D-982E-2D4B2D920A61@mi crosoft.com...[color=darkred]
                  > > > Hello everybody,
                  > > >
                  > > > I want to declare an array in form1, load it in form1 and then access that
                  > > > array in form2. New in VB, It took me a while to understand that declaring
                  > > > variables right under class form1 make those variables accessibles
                  > > > everywhere
                  > > > in that form1:
                  > > >
                  > > > Public Class Form1
                  > > > Inherits System.Windows. Forms.Form
                  > > > Dim TextLine(66) As String
                  > > > ...
                  > > >
                  > > > So my question is: how to declare variables in form1 that will be
                  > > > accessible
                  > > > in form2, form3 & etc. ?
                  > > >
                  > > > Thank you
                  > > >[/color]
                  > >
                  > >
                  > >[/color][/color]

                  Comment

                  • Chris

                    #10
                    Re: variable declaration in form1 accessible in form2...

                    Marcel Saucier wrote:[color=blue]
                    > Public Class Form1
                    > Inherits System.Windows. Forms.Form
                    > Public Shared TextLine(66) As String
                    >
                    > It is working when it is define public SHARED...
                    >
                    > Thank you
                    >
                    >
                    >
                    >[/color]

                    Make sure you read up on what the "Shared" keyword does for variables in
                    a form.

                    Comment

                    Working...