To Delegate or not to ..... ??

tocos

Newcomer
Joined
Mar 16, 2006
Messages
4
Location
Alexandria Egypt
Hello all,

I need your help in one small problem:
I have a Class Called clsCats

Code:
Class clsCats
       Public Function ShowList(OtherForm as Form) as Boolean
              'Need to pass OtherForm to Form1
              Form1.Show
       End Function
End Class

On the Form1_Load event I need to access the OtherForm properties

Code:
Class Form1
       Private mOtherForm as form

       Sub Form1_Load or Button1_Click
              mOtherForm.[Any property goes here]
              mOtherForm.ShowDialogue
       End Sub
End Class

I tried Delegates but I failed because the OtherForm will be unknown in the design time .. I'm not good in the delegate thing so .. please help

Hesham Abdou
 
I don't think you need a Delegate to achieve what you wish. Assuming I'm understanding you right you could do it with a property.

Visual Basic:
Class Form1
       Private mOtherForm as form
    Public Property OtherForm()
        Get
            Return mOtherForm
        End Get
        Set(ByVal Value)
            mOtherForm = Value
        End Set
    End Property

       Sub Form1_Load or Button1_Click
              mOtherForm.[Any property goes here]
              mOtherForm.ShowDialogue
       End Sub
End Class

       Public Function ShowList(OtherForm as Form) as Boolean
             Form1.OtherForm = OtherForm
              Form1.Show
       End Function
 
Cags said:
Visual Basic:
Class Form1

       .............

       Sub Form1_Load or Button1_Click
              mOtherForm.[Any property goes here]   <<<<<<<<< Here
              mOtherForm.ShowDialogue
       End Sub
End Class

My problem is in the following line:
Visual Basic:
        mOtherForm.[Any property goes here]

I can not access any "Custom Properties or Procedures" of mOtherForm as the compiler will mention that the custom Property is not a member of System.Windows.Forms.Form

Thank you any way ..
 
I don't quite get it. It looks like mOtherForm is a form...so why would it have any additional properties? Perhaps some additional abstraction is in order? Is mOtherForm actually another class that inherits Form? Can you just cast it back to whatever it needs to be and call the method...
 
I'm not sure if I understand the problem, but I think it might be that you don't realize that a delegate can be tied to a particular instance.
Visual Basic:
Class SampleClass
    Dim Value As String
    Delegate Function BlankFunction() As String
 
    Public Function GetValue() As String
        Return Value
    End Function
 
    Public Sub New(ByVal Val As String)
        Value = Val
    End Sub
 
    Shared Sub ForExample()
        Dim MySample1 As New SampleClass("1")
        Dim MySample2 As New SampleClass("2")
 
        Dim MyDelegate1 As BlankFunction = AddressOf MySample1.GetValue
        Dim MyDelegate2 As BlankFunction = AddressOf MySample2.GetValue
 
        'Note that when we call the delegate, we do not have to specify which object
        'to call the function on. That information is stored in the delegate.
        MessageBox.Show(MyDelegate1)
        MessageBox.Show(MyDelegate2)
    End Sub
End Class
 
Let's back up, what is ClsCats...is this the 'other form' or are we all incorrectly assuming?

If it is indeed suppose to be an instance of ClsCats then just make your member field a type of ClsCats instead of Form, then you won't get your compile error.

Also I notice that you never actually intialize ClsCats...or mention to us that it's being initialized in another procedure, I think we're all assuming this, but to clarify, you are intializing mOtherForm with a new instance of ClsCats somewhere right?
 
Hello everybody,
Thank you very much for your fast response ...

I will start from the begining to clarify any missunderstanding

1- clsCats is a class
2- Form1 is a form
3- OtherForm is a form which will be different every time I use the class clsCats, but all of the OtherForm(s) will have the same custom properties for Example "ProductID"

When I instanciate clsCats it will
-Show Form1
-Store a reference to the OtherForm in the Form1
-Store the Value of the Property "ProductID" which will be used later by the OtherForm

Form1 has a Button1 when clicked will show the OtherForm Loaded with the Product information Where ProductID = whatever ..
And this step is the problem because I can't use:
Visual Basic:
mOtherForm.ProductID
as this is considered as a late binding .. in the design time the debugger does not know that mOtherForm will have a property called ProductID and fire an exception >> "ProductID is not a member of system.windows.forms.form"


Thats all .. for more information please let me
Hesham
 
Okay, if all the other forms are going to implement that property you need to use an interface who has that property. On each form you use that interface. Then your mOtherForm is of that interface type, not the form:

Interface:
Visual Basic:
Interface ICatsAccessible
     Property ProductID as Integer   'Prepend ReadOnly if needed
End Interface

Then on each FORM underneath the line 'Inherits ....' write:
Visual Basic:
Implements ICatsAccessible
and hit return, it will (should) implement the stubs for you.

Then simple as I said before declare your member variable as:
Visual Basic:
Dim mOtherForm as ICatsAccessible

And then open your form as you normally do:
Visual Basic:
  mOtherForm = New MyForm1   'or whatever you're doing (as long as that objects implements the ICatsAccessible interface

And finally you'll be able to compile with:

Visual Basic:
mOtherForm.ProductId

as the compiler will now that this variable is of a type that implements this interface.
 
Back
Top