Making a call to a shared Shared Method  
Author Message
ron nash





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

Hi,

Can not workout answer to this problem:

I have, for example, a MDI form (Form1) a child form (Accounts) and a Class (Add_Account)

Within Form1 I have a shared method as follows:

Shared Sub testshare()

MsgBox("testshare")

Form1.Text = ("Test Share")

End Sub

I call this shared method from the Add_Account class like this:

Form1.testshare() '\\Calls shared method in form1(MDI form)

This all works fine. What I cannot understand is this:

I have aslo created a shared method within the Accounts form as follows:

Shared Sub stat()

MsgBox("status bar")

End Sub

The issue is I cannot see this shared method when making a call to it from the Add_Account class as follows:

accounts. 'In intellisense I can see all the normal methods/procedures etc, but cannot see the shared sub stat()

What I am trying to do is this: From the Add_Accounts class I make a call to the Accounts form, which is the active child, shared method. In this method I will add code to change this forms Status bar's text.

Ron



Visual Studio Express Editions2  
 
 
ron nash





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

Update,

I have sorted the above issue, however, when I call the following shared method the code makes a new instance of the accounts class and therefore the tool strip will not change on the current displayed form.

Shared Sub test2()

MsgBox("status bar")

Accounts.ts2.Text = "eeeee"

End Sub

The above method is within the Accounts form.
When I make a call to this and the Accounts.ts2.text
line executes it starts to make a new instance of Accounts form.

This is what I need my code to do:

The Accounts form is in display as a child form of Form1(MDI)

The Add_ Account class makes a call to the Accounts form to change the text of the Accounts form Tool Strip.text

Any help would be appreciated

ron


 
 
DMan1





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

It sounds as if you are having an issue referencing the correct instance...a couple of suggestions any of which will work:

1: Use a Global Reference to your Form

Module M

Pulbic F as New AccountsForm

End Module

2: Use a Global Variable to share data:

Module M

Pulbic Var as String

End Module

3: Use events and event handlers to pass data



 
 
ron nash





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

Thanks for the reply,

I understand Scope and the use of declaring objects variables as Public, however, I beleive this is not the issue here.

If I understand it correctly

Using shared methods within a class means that the shared method will be available to all instances of the class. You do not need to define an instance of the class containing the shared method to call it.

The issue seems to be the way you reference controls etc within the shared method,

For example, if I have the following in the Accounts form

public sub Test1()

me.control.text="Test"

end sub

I can use the me.control.text="Test" syntax, however, when using a shared method you need to reference the control like this :

Accounts.control.text="Test"

Where Accounts is the Class containing the control.


I have used withevents /raiseevents etc, but need to understand why the above is causing an issue.

ron


 
 
nobugz





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

You need Public Shared Sub to make it visible outside the class, I think you already got that. To reference it, you need to prefix the namespace name. In other words, the name of the class in which the method is defined. This is different from a non-shared method, it is prefixed with the variable that contains a reference to the class. An example might help:

Public Class Accounts
Public Shared Sub StaticSub()
Public Sub InstanceSub()
End Class
...
'--- Calling the shared method, no need for an object but prefix with namespace
Accounts.StaticSub()
'--- Calling the non-shared method, need an object
Dim myAccounts As New Accounts
myAccounts.InstanceSub()



 
 
ron nash





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

Hi thanks for this,

I have no issues calling the shared method, the issue I have is referencing controls within the form(class) where the share method is defined.

Forexample:

Public class form1

shared sub myshare()

form1.control.text="Shared Test"

end sub

Where form1 is the class that contains the control

This example will show the issue:

Create 2 forms named Form1, Accounts
Create a class named Class1

Paste the following code in the forms,class

Form1
Set as MDI form add a tool strip and status strip
Add a button to the toolstrip
Add a tool strip lable and name it TS1

Run the app and click on the toolbar icon this will open the child form which contains a button. This button, when clicked, will create an instance of class1 and call the test method.
This method ten calls the shared method in the Accounts form that should change the Status Strip(TS1) text.

Ron

Public Class Form1

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

Call Accounts()

End Sub

Private Sub accounts()

Dim frm As Form

For Each frm In Me.MdiChildren

If frm.Text = "Customer Accounts" Then

frm.Close()

End If

Next

Static Accounts As New Accounts

If Accounts.IsDisposed Then

Accounts = New Accounts

End If

Accounts.MdiParent = Me

Accounts.Show()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

End Class

Accounts Form

Add a button to form named button1

Public Class Accounts

'Public myclass1 As Class1

Private Sub Accounts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Shared Sub test()

MsgBox("test")

Accounts.ts1.Text = "test"

Accounts.Show() '\\ If I add this line it opens up a new form with the status strip text changed

End Sub

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

Dim myclass1 As New Class1

Call myclass1.test()

End Sub

End Class

Class1

Public Class Class1

Public Sub test()

Call Accounts.test()

End Sub

End Class


 
 
nobugz





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

You are running into an unfortunate fluke in VB.NET; to stay compatible with lots of VB6 code, it allows you to create a new instance of a form simply by specifying the form's class name. This happens at the statement 'Accounts.ts1.Text = "test"'. This creates a NEW instance of the Accounts form, on top of the one you already created in Form1's ToolStripButton1_Click().

Rework your code to something like this:
Public Class Form1
Public WithEvents AccountsForm As Accounts

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
If AccountsForm Is Nothing Then
AccountsForm = New Accounts
AccountsForm.MdiParent = Me
AccountsForm.Show()
Else
AccountsForm.Focus()
End If
End Sub

Private Sub AccountsForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles AccountsForm.FormClosed
AccountsForm = Nothing
End Sub
End Class

Form1, the MDI parent, now has a reference to the one and only Accounts form. Note how you keep track whether or not it is available by handling the FormClosed event.

When you want to reference that single instance of the Accounts form, you can now use the MDI parent instance:
Public Class Class1
Public Sub Test()
If Not Form1.AccountsForm Is Nothing Then
Form1.AccountsForm.Test()
End If
End Sub
End Class

And finishing off with the Accounts form implementation:
Public Class Accounts
Public Sub Test()
ts1.Text = "test"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myclass1 As New Class1
myclass1.Test()
End Sub
End Class



 
 
ron nash





PostPosted: Visual Basic Express Edition, Making a call to a shared Shared Method Top

Hi Nobugz,

Thanks for the speedy reply,

I have used your code in a experimental app and it makes perfect sense. I will now code this into my main development, and I am sure it will cure the issues I have.

I can now see the issue with my code, if I had created the instance of the Accounts form within Form1 at a higher scope then I could have called the test method within the form1 instance of the Accounts form which would have been the same instance of the form that was displayed.

 

Many Thanks

Ron