RE: Two Classes with the same Data Structure.. saving code?Inheriting a structure?

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

    #1

    RE: Two Classes with the same Data Structure.. saving code?Inheriting a structure?

    RE: Two Classes with the same Data Structure.. saving code? Inheriting
    a structure?

    I have two classes. One in Inherits System.Collecti ons.CollectionB ase,
    the other does not, but they both have the identical structure and
    properties. the only difference between them is there methods and that
    one is a collection class and the other is not.

    currently the code starts like this:

    class1

    Namespace WH
    Public Class colCollaborator
    Inherits System.Collecti ons.CollectionB ase
    Public Structure s_Collaborator
    Private m_Result As String
    Private m_ColId As Integer
    Private m_Lastname As String
    Public Property ColId() As Integer
    Get
    Return m_ColId
    End Get
    Set(ByVal value As Integer)
    m_ColId = value
    End Set
    End Property
    ... more code


    class2


    Namespace WH
    Public Class Collaborator
    Private m_ColId As Integer
    Private m_Lastname As String
    Private m_Firstname As String
    Public Property ColId() As Integer
    Get
    Return m_ColId
    End Get
    Set(ByVal value As Integer)
    m_ColId = value
    End Set
    End Property
    ....



    Dumb noob question that I can't figure out despite endless resources..
    is it possible to share or inherit that structure in both classes ..
    and consolidate class files?

    Thanks for any help or information!


  • Michael Leithold, WWK

    #2
    Re: Two Classes with the same Data Structure.. saving code?Inheriting a structure?

    On 12 Jun., 21:11, jc <wild...@noclie nt.netwrote:
    RE: Two Classes with the same Data Structure.. saving code? Inheriting
    a structure?
    >
    I have two classes. One in Inherits System.Collecti ons.CollectionB ase,
    the other does not, but they both have the identical structure and
    properties. the only difference between them is there methods and that
    one is a collection class and the other is not.
    Just two thoughts:

    You could use an abstract class to do this. Look here:
    http://www.devx.com/dotnet/Article/28086 You could also use
    inheritance although they say "use aggregation, not inheritance".
    At least it would be a good idea to create an interface and implement
    it in your two classes.

    Michael

    Comment

    • jc

      #3
      Re: Two Classes with the same Data Structure.. saving code?Inheriting a structure?

      But then wouldn't this be true?

      quote -

      All types that implement the AnInterface interface must implement
      every declared method in that interface. In this example we only need
      to implement the function WhoAmI. Suppose AClass implements
      AnInterface; we would need to implement WhoAmI. The result of
      implement AnInterface in AClass would yield the following code.

      Public Class AClass
      Implements AnInterface

      Public Function WhoAmI() As String Implements AnInterface.Who AmI
      Return "AClass"
      End Function

      End Class

      endquote -


      I'll try it out.. but if anybody has a simple example of how to reuse
      the structure code in two classes where one class is already
      inheriting another class (understanding that vb does not support
      multiple inheritances). Any help is appreciated.

      Thank you!

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: Two Classes with the same Data Structure.. saving code? Inheritinga structure?

        jc wrote:
        RE: Two Classes with the same Data Structure.. saving code? Inheriting
        a structure?
        >
        I have two classes. One in Inherits System.Collecti ons.CollectionB ase,
        the other does not, but they both have the identical structure and
        properties. the only difference between them is there methods and that
        one is a collection class and the other is not.
        >
        currently the code starts like this:
        >
        class1
        >
        Namespace WH
        Public Class colCollaborator
        Inherits System.Collecti ons.CollectionB ase
        Public Structure s_Collaborator
        Private m_Result As String
        Private m_ColId As Integer
        Private m_Lastname As String
        Public Property ColId() As Integer
        Get
        Return m_ColId
        End Get
        Set(ByVal value As Integer)
        m_ColId = value
        End Set
        End Property
        ... more code
        >
        >
        class2
        >
        >
        Namespace WH
        Public Class Collaborator
        Private m_ColId As Integer
        Private m_Lastname As String
        Private m_Firstname As String
        Public Property ColId() As Integer
        Get
        Return m_ColId
        End Get
        Set(ByVal value As Integer)
        m_ColId = value
        End Set
        End Property
        ....
        >
        >
        >
        Dumb noob question that I can't figure out despite endless resources..
        is it possible to share or inherit that structure in both classes ..
        and consolidate class files?
        >
        Thanks for any help or information!
        >
        Do you absolutely need the colCollaborator class to inherit the
        CollectionBase class? Otherwise you can just remove that inheritance,
        and make the colCollaborator class inherit the Collaborator class. You
        can quite easily implement things like IEnumerable if you need to
        iterate a collection in the class.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • jc

          #5
          Re: Two Classes with the same Data Structure.. saving code?Inheriting a structure?

          On Jun 13, 5:22 pm, Göran Andersson <gu...@guffa.co mwrote:
          jc wrote:
          RE: Two Classes with the same Data Structure.. saving code? Inheriting
          a structure?
          >
          I have two classes. One in Inherits System.Collecti ons.CollectionB ase,
          the other does not, but they both have the identical structure and
          properties. the only difference between them is there methods and that
          one is a collection class and the other is not.
          >
          currently the code starts like this:
          >
          class1
          >
          Namespace WH
          Public Class colCollaborator
          Inherits System.Collecti ons.CollectionB ase
          Public Structure s_Collaborator
          Private m_Result As String
          Private m_ColId As Integer
          Private m_Lastname As String
          Public Property ColId() As Integer
          Get
          Return m_ColId
          End Get
          Set(ByVal value As Integer)
          m_ColId = value
          End Set
          End Property
          ... more code
          >
          class2
          >
          Namespace WH
          Public Class Collaborator
          Private m_ColId As Integer
          Private m_Lastname As String
          Private m_Firstname As String
          Public Property ColId() As Integer
          Get
          Return m_ColId
          End Get
          Set(ByVal value As Integer)
          m_ColId = value
          End Set
          End Property
          ....
          >
          Dumb noob question that I can't figure out despite endless resources..
          is it possible to share or inherit that structure in both classes ..
          and consolidate class files?
          >
          Thanks for any help or information!
          >
          Do you absolutely need the colCollaborator class to inherit the
          CollectionBase class? Otherwise you can just remove that inheritance,
          and make the colCollaborator class inherit the Collaborator class. You
          can quite easily implement things like IEnumerable if you need to
          iterate a collection in the class.
          >
          --
          Göran Andersson
          _____http://www.guffa.com
          Göran,

          Can you show me a simple example ? I'm somewhat new to OOP.

          Very interested in see that.

          Thank you!

          Comment

          • Steve Gerrard

            #6
            Re: Two Classes with the same Data Structure.. saving code? Inheriting a structure?

            jc wrote:
            >
            >
            I'll try it out.. but if anybody has a simple example of how to reuse
            the structure code in two classes where one class is already
            inheriting another class (understanding that vb does not support
            multiple inheritances). Any help is appreciated.
            >
            I'm not sure what you are really after, but I would probably do it by embedding
            a Collaborator object inside of colCollaborator . This is sometimes referred to
            as "aggregatin g" a class within another class.

            Public Class Collaborator
            Private m_ColId As Integer
            ' ...
            Public Property ColId() As Integer
            ' ...
            End Property
            ' ...
            End Class

            Public Class colCollaborator
            Inherits System.Collecti ons.CollectionB ase
            Private m_Collab As Collaborator
            ' ...
            Public ReadOnly Property Collab As Collaborator
            Get
            Return m_Collab
            End Get
            End Property
            ' ...
            End Class

            The effect is that colCollaborator is a collection, but also has a .Collab
            property, which reveals all the properties of a Collaborator object as well.



            Comment

            Working...