Results 1 to 8 of 8

Thread: Can one Class know the object of another Class?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Can one Class know the object of another Class?

    Let's say I have two class modules one called Apple and the other called Orange.

    Now in a .bas file I have the following declarations:

    Public appl As Apple
    Public oran As Oranges

    Now in the main Form I have some code like this:

    '
    '
    '
    Set appl = New Apple
    Set oran = New Orange
    '
    '
    '
    So my question is; is there a way in class Apple, for example, it has a way of knowing that oran was set as the object of class Orange?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can one Class know the object of another Class?

    Yes....
    If TypeOf Oran Is Orange Then ' oran is an orange class

    Additionally. You can use TypeName(object) to check its name as a string.

    Edited: And if you really want to prevent any potential of error
    Code:
      
       If Not Oran Is Nothing ' will error if Oran not set to new Orange
          If TypeOf Oran Is Orange Then ' retun true?
       End If
    FYI: If Oran hasn't been set to New Orange, then TypeName(Oran)="Nothing" & (Oran Is Nothing)=True
    Last edited by LaVolpe; Mar 7th, 2008 at 04:29 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Can one Class know the object of another Class?

    Not sure I understand.

    In your first statement you say If TypeOf oran Is Orange Then but here the name oran has to be already known where my question is how can class Apple know what the object name is so I couldn't very well use that since it is asking if typeof oran is orange and class Apple doesn't know that name.

    Now in your second statement you say Additionally. You can use TypeName(object) to check its name as a string so what exactly is (object) in this statement and exactly where and how would I use that?

    To make it more clear as to what I am trying to do I will try to put it this way.

    In the main Form I have

    Set oran = New Orange

    now later in class Apple I need to do this:

    If oran.OrangeType = "Naval" Then

    where OrangeType is a Public variable in class Orange

    but class Apple does not know that it needs to say oran.OrangeType = "Naval" because class Apple doesn't know the object name is oran. So I am asking how can class Apple find out what object name was set to class Orange.

    Does that make any sense?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can one Class know the object of another Class?

    How can your class possibly know all the variables in your project? You may want to give more details of why this is needed. If you are going to pass to Apple through one of Apple's functions, an object and you want Apple to know what type of object that is, here is one way.
    Code:
    ' apple class
    Public Sub DoSomething(theObject As Object)
       Select Case TypeName(theObject)
       Case "Nothing"  ' whatever was passed is not instantiated
       Case "Orange" ' this is an orange object/class
    
           Debug.Print "Orange type is "; theObject.OrangeType
    
       Case "Apple" ' this is an apple object/class
       Case "PictureBox" ' it is a picture box
       Case "Image" ' it is an image control
       etc etc etc
       End Select
      
       ' do whatever to theObject
       
    End Sub
    Edited: Gave a more specific example. Note that when passing classes/controls/etc to a routine as Object, only public properties, functions, subs are available. So in the above example, only if Orange's OrangeType is a public property/function can you call it; otherwise, error.
    Last edited by LaVolpe; Mar 7th, 2008 at 05:06 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can one Class know the object of another Class?

    And once you master the above, should you really want to get froggy, you can do a little research on Implementations. That is fun and is kind of what I think you are attempting to do. However, it can be very confusing to most & is a bit more difficult, but at the same time, more flexible than the method shown above.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Can one Class know the object of another Class?

    How can your class possibly know all the variables in your project?

    Well I think this because when you write a program and your program contains a Form module and a .bas module and in the .bas module you put a Public variable then you know that the Form module knows that this variable is in the .bas module, right?

    So, Class Apple knows there is a Public variable in Class Orange called OrangeType just in the same sense that the main Form knows that there is a Public variable in the .bas module

    I could, since I am making the program, just hard code the object name because I already know that oran was set to class Orange so in class Apple I could do just that: If oran.OrangeType = "Naval" Then which would definitely satisfy the results of the question. However, what if I gave class Apple to another programmer to use in his code and class Apple had it hard coded then I would have to tell the other programmer that he must use oran as the object name if he was to instantiate class Orange.

    Maybe what I am asking is not possible at all. I was just wanting to see if there was a way to do this without having to hard code the oran in class Apple and use whatever object name was set to class Orange.

    I'm trying to do something in VB that is already done in Java. I have a Java program that has two classes and if I write the main code I can instantiate one of the classes by any name I want and if I instantiate the other class then the other class knows the name I used for the first class. I don't know how it knows that but I know it does. For all I know it works by maybe one class is a subclass of the other but I am not sure of this.

    Just out of curriosity is it possible to subclass in VB in the same sense that you subclass in Java?

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Can one Class know the object of another Class?

    Quote Originally Posted by LaVolpe
    And once you master the above, should you really want to get froggy, you can do a little research on Implementations. That is fun and is kind of what I think you are attempting to do. However, it can be very confusing to most & is a bit more difficult, but at the same time, more flexible than the method shown above.
    Now I'm beggining to think that's how it's done in Java. I instaniate one class but implement the other. Although I don't see that in the Java program that does kind of make sense.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can one Class know the object of another Class?

    What you are describing is inheritance if I am not mistaken. VB doesn't do that; though .Net does. The closest we have with VB is known as Implementation. And implementation kinda works like this.

    ICommonObject is a class. And it has these functions/properites
    Shape, Width, Height, Draw, etc

    cCircle is a class, cRectangle is a class, cTriangle is a class. Those classes implement ICommonObject. And because they implement ICommonObject, they must also implement ICommonObject's functions/properties. Example follows. For sake of brevity, only Shape is coded.

    Code:
    ' class name: ICommonObject
    Option Explicit
    ' impelmentations contain no code (generally), their functions are placeholders
    ' though they may contain public enumerations, declarations
    Public Property Get Shape() As String
    
    End Property
    Code:
    ' class name: cCircle
    Option Explicit
    Implements ICommonObject
    Private Property Get ICommonObject_Shape() As String
        ICommonObject_Shape = "Circle"
    End Property
    Code:
    ' Class name: cRectangle
    Option Explicit
    Implements ICommonObject
    Private Property Get ICommonObject_Shape() As String
        ICommonObject_Shape = "Rectangle"
    End Property
    Code:
    ' on a form....
    Private Sub Command1_Click()
        Dim myShape As ICommonObject
        
        Set myShape = New cCircle
        Debug.Print myShape.Shape
        
        Set myShape = New cRectangle
        Debug.Print myShape.Shape
    End Sub
    Last edited by LaVolpe; Mar 7th, 2008 at 06:03 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width