VB Classes: Bit more info...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Turner

    VB Classes: Bit more info...

    Hi all,
    I have 3 questions regarding classes.
    1:- If I have a class, Class1, how do I get Class2 to inherit class1?
    2:- Assuming that the above is possible, if Class1 contains a function,
    Funct1, what scope keyword do I need so that Funct1 is only visible
    to derived classes?
    3:- Can the same be done with constants and varibles?

    I'm using VB6 by the way...

    Thanks for any input.

    GST


  • Jeffrey R. Bailey

    #2
    Re: VB Classes: Bit more info...

    1. Inheritance isn't possible with VB6, aggregation is. To achieve it at
    the top of the Class2 module include a declaration similar to this:
    Private m_Class1 as New Class1

    Then any of the public/friend methods (functions/subs) are available for
    use in Class2. This achieves code re-use and allows for information hiding
    within class1, which is the point of inheritance. VB6 is not a true object
    oriented language.

    2. From within the project I do not think it is not possible to scope a
    function so that it is only visible to derived classes. I think I am being
    overly technical here, because it is possible to set the instancing property
    of Class1 to public (making Class1's publicly declared properties and
    methods visible outside of your project or component) and to have properties
    and methods in Class1 that are available to other objects within your
    project or component, but not visible from outside. To do this declare
    Funct1 with the Friend keyword:
    Friend Funct1(MyArg as Whatever) as Whatever

    3. Yes, you can declare constants and variables public in standard module
    and then wrap them for use as Friend Properties in a class module. This
    hides them from use outside of your project.

    Visual Basic Documentation contains an article called "General Principles of
    Component Design" that covers "Friend" nicely in a sub-topic named "Private
    Communications Between Your Objects" as well as "Friend Properties and
    Methods"

    HTH

    -- Jeffrey R. Bailey
    "Geoff Turner" <g.s.turner@rl. ac.uk> wrote in message
    news:bvd4vm$pgm @newton.cc.rl.a c.uk...[color=blue]
    > Hi all,
    > I have 3 questions regarding classes.
    > 1:- If I have a class, Class1, how do I get Class2 to inherit class1?
    > 2:- Assuming that the above is possible, if Class1 contains a function,
    > Funct1, what scope keyword do I need so that Funct1 is only visible
    > to derived classes?
    > 3:- Can the same be done with constants and varibles?
    >
    > I'm using VB6 by the way...
    >
    > Thanks for any input.
    >
    > GST
    >
    >
    >[/color]


    Comment

    • Jeffrey R. Bailey

      #3
      Re: VB Classes: Bit more info...

      My bad. Friend declarations are not callable from standard modules and it
      even says so in the documentation. Friend declarations should do exactly
      what you want in part 2 of your post.

      --
      Jeffrey R. Bailey
      "Jeffrey R. Bailey" <MrWizard2903Re moveMe@yahoo.co m> wrote in message
      news:KlDSb.1721 74$I05.2814304@ twister.tampaba y.rr.com...[color=blue]
      > 1. Inheritance isn't possible with VB6, aggregation is. To achieve it at
      > the top of the Class2 module include a declaration similar to this:
      > Private m_Class1 as New Class1
      >
      > Then any of the public/friend methods (functions/subs) are available[/color]
      for[color=blue]
      > use in Class2. This achieves code re-use and allows for information[/color]
      hiding[color=blue]
      > within class1, which is the point of inheritance. VB6 is not a true[/color]
      object[color=blue]
      > oriented language.
      >
      > 2. From within the project I do not think it is not possible to scope a
      > function so that it is only visible to derived classes. I think I am[/color]
      being[color=blue]
      > overly technical here, because it is possible to set the instancing[/color]
      property[color=blue]
      > of Class1 to public (making Class1's publicly declared properties and
      > methods visible outside of your project or component) and to have[/color]
      properties[color=blue]
      > and methods in Class1 that are available to other objects within your
      > project or component, but not visible from outside. To do this declare
      > Funct1 with the Friend keyword:
      > Friend Funct1(MyArg as Whatever) as Whatever
      >
      > 3. Yes, you can declare constants and variables public in standard module
      > and then wrap them for use as Friend Properties in a class module. This
      > hides them from use outside of your project.
      >
      > Visual Basic Documentation contains an article called "General Principles[/color]
      of[color=blue]
      > Component Design" that covers "Friend" nicely in a sub-topic named[/color]
      "Private[color=blue]
      > Communications Between Your Objects" as well as "Friend Properties and
      > Methods"
      >
      > HTH
      >
      > -- Jeffrey R. Bailey
      > "Geoff Turner" <g.s.turner@rl. ac.uk> wrote in message
      > news:bvd4vm$pgm @newton.cc.rl.a c.uk...[color=green]
      > > Hi all,
      > > I have 3 questions regarding classes.
      > > 1:- If I have a class, Class1, how do I get Class2 to inherit class1?
      > > 2:- Assuming that the above is possible, if Class1 contains a function,
      > > Funct1, what scope keyword do I need so that Funct1 is only visible
      > > to derived classes?
      > > 3:- Can the same be done with constants and varibles?
      > >
      > > I'm using VB6 by the way...
      > >
      > > Thanks for any input.
      > >
      > > GST
      > >
      > >
      > >[/color]
      >
      >
      >[/color]


      Comment

      • Steve Gerrard

        #4
        Re: VB Classes: Bit more info...


        "Jeffrey R. Bailey" <MrWizard2903Re moveMe@yahoo.co m> wrote in message
        news:9sDSb.1723 63$I05.2814924@ twister.tampaba y.rr.com...[color=blue]
        > My bad. Friend declarations are not callable from standard modules[/color]
        and it[color=blue]
        > even says so in the documentation. Friend declarations should do[/color]
        exactly[color=blue]
        > what you want in part 2 of your post.
        >
        > --[/color]

        Nit picking: Friend procedures can't be declared in a standard module.
        Friend methods of a class in the same project can be called from code in
        a standard module or a form module.

        The help file wording is unhelpfully vague: "Friend procedures cannot be
        used in standard modules."



        Comment

        • Jeffrey R. Bailey

          #5
          Re: VB Classes: Bit more info...

          Which is what I thought in the first post. I should have stuck to my guns
          or done a test in code before posting.

          --
          Jeffrey R. Bailey
          "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
          news:V-idnZr3wtafh4bdR Vn-vg@comcast.com. ..[color=blue]
          >
          > "Jeffrey R. Bailey" <MrWizard2903Re moveMe@yahoo.co m> wrote in message
          > news:9sDSb.1723 63$I05.2814924@ twister.tampaba y.rr.com...[color=green]
          > > My bad. Friend declarations are not callable from standard modules[/color]
          > and it[color=green]
          > > even says so in the documentation. Friend declarations should do[/color]
          > exactly[color=green]
          > > what you want in part 2 of your post.
          > >
          > > --[/color]
          >
          > Nit picking: Friend procedures can't be declared in a standard module.
          > Friend methods of a class in the same project can be called from code in
          > a standard module or a form module.
          >
          > The help file wording is unhelpfully vague: "Friend procedures cannot be
          > used in standard modules."
          >
          >
          >
          >[/color]


          Comment

          • Steve Gerrard

            #6
            Re: VB Classes: Bit more info...


            "Geoff Turner" <g.s.turner@rl. ac.uk> wrote in message
            news:bvd4vm$pgm @newton.cc.rl.a c.uk...[color=blue]
            > Hi all,
            > I have 3 questions regarding classes.
            > 1:- If I have a class, Class1, how do I get Class2 to inherit class1?
            > 2:- Assuming that the above is possible, if Class1 contains a[/color]
            function,[color=blue]
            > Funct1, what scope keyword do I need so that Funct1 is only visible
            > to derived classes?
            > 3:- Can the same be done with constants and varibles?
            >
            > I'm using VB6 by the way...
            >
            > Thanks for any input.
            >
            > GST
            >
            >[/color]

            In addition to aggregation, as described in Jeffrey's post, you might
            look at Interface implementation. Its like inheriting the structure of a
            class, without any code.

            You might define an interface by making a class called IBaseClass, which
            contains empty public procedure declarations. Say for now you declare:
            Public Sub Identify()
            End Sub

            You could then define Class1, and among other things, make it implement
            the IBaseClass interface by putting Implements IBaseClass at the top, in
            the general declarations. You would then be required to include an
            implentation of each of the IBaseClass public methods, i.e.
            Private Sub IBaseClass_Iden tify()
            MsgBox "I am a class 1 object"
            End Sub

            Repeat for Class2, also implementing IBaseClass:
            Private Sub IBaseClass_Iden tify()
            MsgBox "I am a class 2 object"
            End Sub

            Although you do not inherit any code, both Class1 and Class2 can now be
            worked with as IBaseClass objects:

            Private Sub Command1_Click( )
            Dim oClass1 As New Class1
            Dim oClass2 As New Class2
            Dim oBase As IBaseClass

            Set oBase = oClass1
            Call oBase.Identify

            Set oBase = oClass2
            Call oBase.Identify

            End Sub

            If you need to inherit code, you can combine aggregation and interface
            implementation, and actually do quite a lot of object orientation.



            Comment

            Working...