Solved

retrieving the instance of the calling object?

Posted on 2006-06-20
5
204 Views
Last Modified: 2010-04-23
is it possible to get the calling class and use it in the called module?
for example
class A is calling method modifyC in class B
modifyC takes a param from the calling class

basically I need to clean up some code that has method repeated in multiple classes and want to pull those out but need to use a variable from the calling member

is the easiest way to create an enum with the classes that could be calling and go that route?

thanks for the help
0
Comment
Question by:Orlando15767
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 16942590
You could make a "sender" parameter like .Net does...

    Private Sub Foo(ByVal sender As Object, ByVal someParam As Integer)
        If TypeOf sender Is ClassA Then
            Dim clsA As ClassA = CType(sender, ClassA)
            ' do something with "clsA"
        ElseIf TypeOf sender is ClassB Then
            ...
        Else ...
            ...
        End If
    End Sub

Or if you want to be able to use the exact same calling code for all forms then create an Interface and make all your forms implement it.  Then the data type of your parameter should be the Interface type so you can access the common property/sub/function of them all.
0
 
LVL 1

Author Comment

by:Orlando15767
ID: 16942763
How could I go about using an interface to accomplish this? I've never used interface that much, and never in vb.net

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 125 total points
ID: 16945489
Something like this:

    Public Interface IFooBar
         Sub MethodA
    End Interface

Implementation:

   Public Class FooBarTest
      Implements IFooBar

      Private _test As String = ""
      Public Property StringValue() As String
          Get
             Return _test
          End Get
          Set(ByVal value As String)
             _test = value
          End Set
      End Property

      Private Sub MethodA() Implements IFooBar.MethodA
          MessageBox.Show(Me.StringValue)
      End Sub

   End Class

Usage:

   Dim instance As New FooBarTest
   instance.StringValue = "Bob"

   Dim test As IFooBar = CType(instance, IFooBar)
   test.MethodA()
   
Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16946021
Thanx Bob.  =)

So once you have Implemented the Interface, that class can take the place of a parameter specifed as that Interface type:

    Private Sub Foo(ByVal sender As IFooBar)
        sender.MethodA()
    End Sub

You can create an instance of FooBarTest and pass that into Foo():

    Dim instance As New FooBarTest
    instance.StringValue = "Bob"
    Foo(instance)

This will work with ANY class that Implements IFooBar...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16946036
Any time Mike ;)

Bob
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question