Solved

retrieving the instance of the calling object?

Posted on 2006-06-20
5
199 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
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
Any time Mike ;)

Bob
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now