after searching on the web, i know that Me keyword is use to Refer to the Current Instance of an Object, but if i want to Refer to the others Instance of an Object, what is the keyword that i need to use?

Dani AI

Generated

Short answer: there is no VB keyword that magically means "the other instance." You refer to whatever variable or object reference holds that other instance. For a WinForms scenario like yours you typically either (a) create/pass a reference to the second form, or (b) use the VB default form instance (with caveats). The safer, clearer patterns are constructor injection or a public method/property on the target form.

Example — constructor injection:

' Caller form
Dim f As New DetailForm(myTable)
f.Show()

' DetailForm
Public Class DetailForm
    Private ReadOnly _table As DataTable

    Public Sub New(table As DataTable)
        InitializeComponent()
        _table = table
    End Sub

    Private Sub DetailForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        grid.DataSource = _table
    End Sub
End Class

Example — set a property or call a method:

Dim f As New DetailForm()
f.SetTable(myTable)
f.Show()

Public Class DetailForm
    Public Sub SetTable(table As DataTable)
        grid.DataSource = table
    End Sub
End Class

Practical tips: avoid making controls Public — expose a method/property instead. If you need one shared, long-lived instance store that reference in a module or a parent form property and pass it around. Be careful with VB default form instances (they can be convenient but can also hide lifetime/initialization issues) — see Microsoft docs on default form instances for details. As asked for examples and noted control names, these patterns keep form ownership explicit and avoid directly manipulating another form's internals.

Recommended Answers

All 4 Replies

What do you mean by:

... but if i want to Refer to the others Instance of an Object?

Can you show a simple code example?

Dim dt As New DataTable
        'fill data to datatable
        da.Fill(dt)

        'offer data in data table into datagridview
        Me.DataGridView1.DataSource = dt
    Me keyword is use to Refer to the Current Instance of an Object,for example,the coding above will show the data in datagrid inside the current form. but what if i want to show the datagrid on another form inside the same project?

In your case, Me is not necessary, since you have this code on the same form as the datagridview control. You use Me in cases where is possible that comes to an ambiguity.
Like this example:

    Public Class Employee
        Private name As String
        Public Sub New(name As String)
                'this.name refers to private variable in the Employee class, while name refers to the method`s parameter
            Me.name = name
        End Sub
    End Class

Each object (control) that you create is given a unique name by default, such as TextBox1, TextBox2, etc. which you are free to change as long as it remains unique. You can see that name in the properties view. Just refer to the object by that name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.