Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

VB.NET: Can I programatically refer to the name of a class?

I would like to set the value of a string to the name of the class (found in the top line of the class in the Public Class 'name' line).

What I'm REALLY trying to do is allow a method I'm calling in another class to know which class called it. So, if I'm in Class1, and I'm calling SomeClass.SomeMethod, I'd like to let SomeMethod know that Class1 called it.

Is there a way to accomplish this in Visual Basic?

7 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You can get the full type name as string from any object in .NET:

    obj.GetType().ToString()

    You could also pass in the actual object, rather than a string, and use TypeOf to determine the type:

    Public Sub SomeMethod(ByVal sender As System.Object)

      If TypeOf (sender) Is Class1 Then

       MsgBox(sender.GetType().ToString())

      End If

    End Sub

    It's questionable if this is a good design though. You usually want classes to be as independent of each other as possible. If class A needs to know about class B and C to work, then you won't be able to use A from class D without changing A as well. Try to find a way to make your classes not so dependent on each other.

  • Anonymous
    4 years ago

    Vb.net Gettype

  • Anonymous
    1 decade ago

    I don't really know much about Vb net but you could create a new variable for each class where for example in the first class this variable will have value of 1, the second one 2, third 3 etc

    so basically by checking the value of the variable you can know which class called the method

  • 1 decade ago

    Yes.

    You can retrieve the name of the current class by using Me.GetType().Name. This will return the name of the class in a string that you can pass to your other method.

    Check the article below for info on what you're really trying to do.

  • How do you think about the answers? You can sign in to vote the answer.
  • Anonymous
    5 years ago

    Only Suker is worth mentioning. Yes, there is Eduardo of Arsenal but he hasn't proven himself completely internationally. Obviously other members used wikipedia for fuller answers!

  • 1 decade ago

    If you must know the name of the object that called the method then make that a parameter that gets passed in with the call to the method.

    My $0.02 worth.

    Source(s): VB.NET Programmer
  • Silent
    Lv 7
    1 decade ago

    Why do you want to do that? Doing stuff like this kind of defeats the purpose of object-oriented programming.

    No offense, but there's almost certainly a better way to accomplish this.

Still have questions? Get your answers by asking now.