How do I make a variable available to another form?

How do I make a variable available to another form

I am trying to use a variable from one user control on another. Can anyone help




Answer this question

How do I make a variable available to another form?

  • LotusExigeS1

    One option is to stored your variable in the session cache.

    ' This method does not require navigation between the two pages

    ' and will persist the object in the cache until removed

    ' add an item to the session cache on page one

    Dim myObj As Integer

    Session.Add("myItem", myObj)

    ' retrieve on a different page

    Dim retrievedObj As Integer

    retrievedObj = Session.Item("myItem")

    If Not Nothing = retrievedObj Then

    ' do page logic based on available variable

    ' remove from cache

    Session.Remove("myItem")

    End If



  • histonftm

    Almost any variable you can make you can use with Settings. Use Configuration Settings (go to the application's properties, then the settings tab) to store variables such as integers, boolean variables, strings, whatever...and you can choose to save it forever or just for the lifetime of the current application instance. They are very convenient. Try it out.

    Edit 1: I forgot this was Visual Basic. You can access settings by going to My.Settings


  • X-Tatic

    declare the variable global (in a module) you can access it every where.

    or else you can make the variable in the form as public and access it in another form as <form name>.<variable name>

    or else creating property you can access all the variable in the form.



  • mspinder

    perhaps this post will guide you better

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=700751&SiteID=1

    Another way I guess would be to make a class/module static so you can set variable as required and let other forms access it without passing references. There are a couple of ways to go about it but hope the above link will help you at least



  • Sammy2006

    Could you elaborate on the Public methood I have tried this and I'm getting a warning stating that the variable is not part of the user control.

  • J Tatta

    As per the example provided if you are actually using an type of integer you would actually check for non zero value as the integer will be initilaised as zero.

    If retrievedObj > 0 Then

    End If


  • BillyB

    I've woke up now! Sorry about that one!


  • MarkBosley

    winforms dont act/have webform features - Session() is for webforms, not winforms

  • How do I make a variable available to another form?