Results 1 to 6 of 6

Thread: How can you create public variables that all forms can read? *resolved*

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    How can you create public variables that all forms can read? *resolved*

    I tried
    Public zText as String in form1, but form2 cant read it (undeclared)

    what am i doing wrong?
    Last edited by VaxoP; Aug 5th, 2003 at 01:22 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    You need to create a public class so that it becomes a shared member: as in:

    Public Class Result
    Public Shared Msg As String

    End Class

    then in one form:

    Result.msg = "set to my values"

    and in the other form interrogating the string

    my string = result.msg

    You do not need to create an instance of the class, it is there for any other class / form to use and the Public Shared can be anything you want, even an object. Take the time to read up on shared members, one of the most useful aspects of VB .Net

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    no luck..

    at the bottom of the code in form1 i have this

    End Class 'End form1's class
    Public Class Result
    Public Shared InfoAbout As String
    End Class

    and then on a listview doubleclick i have
    Dim Frm3 As New Form3()
    Result.InfoAbout = LVW.SelectedItems.Item(0).Text
    Frm3.Show()

    and in form3 load, i have
    MsgBox(Result.InfoAbout)

    i get a blank string..

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    How can you have a selecteditem before you've even shown the form?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    Originally posted by Edneeis
    How can you have a selecteditem before you've even shown the form?
    wha?
    VB Code:
    1. End Class 'End form1's class
    2. Public Class Result
    3. Public Shared InfoAbout As String
    4. End Class
    This is the code used to create the public class. the 'end class' at the beginning shows form1's end class - it just demonstrates i put the new class after the form1 class.

    VB Code:
    1. Dim Frm3 As New Form3()
    2. Result.InfoAbout = LVW.SelectedItems.Item(0).Text
    3. Frm3.Show()
    this code is in form1, on a listview double click i load up form3. i want to send the selected item text variable to form3 so it can read it.

    VB Code:
    1. MsgBox(Result.InfoAbout)
    this is the code in form3's form_load. it msgboxes an empty string.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269
    got it

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