reference variable instances

here's my code:
Public Class frmMain

    Private Sub btnShopRO_Click(sender As Object, e As EventArgs) Handles btnShopRO.Click

        Dim sform As New frmShopHandMain
        sform.Show()

    End Sub
End Class

Open in new window

there is other code there but I condensed it to the pertinent data.
sform variable opens and app starts fine however I would like to know the best way to access  this variable instance of update some text boxes when I launch another form from the sform instance (I don't want to make in a GLOBAL VARIABLE or anything close to that)
I would like to know the best way to declare the FORM variable and keep it as narrow instance as possible.
 I am trying to understand the FRIEND, PROTECTED FREIND, etc. access levels but would like some input as this is my first incursion to this coding area.
MBHEY131Asked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Yes that is possible to do.

In the form which you want to access the other form from without using Global module you can do the following.

At class level meaning outside of any function or subroutine with in a class such as another form define a variable of the type you want access to like the following.

Public Class TestForm

    '' The form you will access from this form, Class level variable
    Private f1 As Form1 = Nothing

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        '' Find the form and assign it to the variable
        f1 = Application.OpenForms("Form1")

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        '' Make sure you have a reference to the form
        If f1 IsNot Nothing Then
            '' Setting the value of Form1.TextBox1.Text property
            f1.TextBox1.Text = " Is Working"
        End If

    End Sub
End Class

Open in new window

0
 
F PCommented:
By definition, the variable scope of your subroutine is Private. That means that the variables you're creating are only going to be seen within that private method, within that class. You pretty much have only a couple options aside from going global.

Local storage on the client side with a cookie, session storage on the server side with a server side cookie, or throw it into a database field and access it again when you need.
0
 
MBHEY131Author Commented:
Local storage on the client side with a cookie,

++++++++++++++++++++++++++++++++++++++
that sounds like the best option in that I have been down the Global route before and learned some painful lessons
could you give me some instruction (very general would be fine) as I need to learn this -

or in this case as my project is going to be on a server - and thus many clients will be launching the app from the server - is staying away from GLOBAL a wise idea (as I don't even like that word)

any advice would be appreciated
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
MBHEY131Author Commented:
I have been looking into:
"Shared sform as frmshophandmain"  declared in the form level would not serve my purpose I don't believe
0
 
F PCommented:
You need to make sure you've declared the include for HttpCookieCollection and have a look here about how to use Response.Cookie and Request.Cookie to access, create, and general usage of client side storage in ASP.NET:

How to work with Client-Side State Management in ASP.NET:
http://mrbool.com/how-to-work-with-client-side-state-management-in-asp-net/26364

ASP.NET State Management Recommendations:
https://msdn.microsoft.com/en-us/library/z1hkazw7%28v=vs.140%29.aspx
0
 
F PCommented:
Unless you're using another .NET technology, in which case client side storage is still an option.
0
 
Fernando SotoRetiredCommented:
Hi MBHEY131;

Visual Basic allows you to get a reference to a form by first getting a collection of FormCollection using the Application.OpenForms Property then you can find the form by name by using then FormCollection.Item Property (String) of the FormCollection.
0
 
MBHEY131Author Commented:
Visual Basic allows you to get a reference to a form by first getting a collection of FormCollection using the Application.OpenForms Property then you can find the form by name by using then FormCollection.Item Property (String) of the FormCollection.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Yes, I'm familiar with the above Property and use it to check for an open instance.

e.g. - "If Application.OpenForms().OfType(Of frmShopHandMain).Count > 0 Then", etc
but how to I access the open instance by the variable "sform" and then reference the textboxes on the said form
without going GLOBAL

============================================================================================
0
 
MBHEY131Author Commented:
Let me ask this:
Is it possible to DIM "sform" in a new block and set it equal to (my.application.openforms oftype(frmshophandmain))
or something similar.
0
 
MBHEY131Author Commented:
perfect:
I will test FIRST thing in the AM
0
 
MBHEY131Author Commented:
That worked perfectly and solved all current issues
Thanx a bunch
0
 
MBHEY131Author Commented:
thanx again
0
 
Fernando SotoRetiredCommented:
Not a problem MBHEY131, glad I was able to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.