Delegate use between forms failing in Class Library project

G

Guest

Hi, I'm using delegates successfully in a Windows Application project. As
soon as I try the same code in a Class Library project, I get an error. Here
is the simple code:

Form1
Public Delegate Sub LabelWriter(ByVal txt As String)

Public Sub WriteLabel(ByVal txt As String)
Label1.Text = txt
End Sub

Form2
Private Sub WriteToForm1(ByVal op As Form1.LabelWriter, ByVal txt As
String)
op.Invoke(txt)
End Sub

Private Sub SendMessage()
WriteToForm1(AddressOf Form1.WriteLabel, "test")
End Sub

In the Class Library project, I get a "reference to a non-shared member
requires an object reference" message at the line "AddressOf
Form1.WriteLabel". It should be noted that Intellisense didn't pick up my
Form1.WriteLabel method even though it is Public scope (I typed it manually).
Again, this code works as is in a Windows Application project.

Could you provide an explanation of why this is occurring and more
importantly, a strategy to address the expected behavior? Thanks in advance!
 
S

Stoitcho Goutsev \(100\)

John,

I'm by far not an expert in VB, but that is what I see.In Form2 WriteToForm1
method one of the parameters is of type Form1.LabelWriter - this is the
delegate. From here Form1 is a name of a class. In AddressOf you are trying
to create delegate for Form1.WriteLabel, method. Because Form1 is name of a
class WriteLabel has to be a static (shared) member. Intellisense correctly
doesn't show this method because it IS NOT static.

Why it worked in the Windows Forms application, I believe, is because you
had declared a member variable named Form1. It looks like VB picked the
variable rather then the class name in AddressOf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top