By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

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

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

I have two classes. One in Inherits System.Collections.CollectionBase,
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.Collections.CollectionBase
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!
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 12 Jun., 21:11, jc <wild...@noclient.netwrote:
RE: Two Classes with the same Data Structure.. saving code? Inheriting
a structure?

I have two classes. One in Inherits System.Collections.CollectionBase,
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

Jun 27 '08 #2

P: n/a
jc
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.WhoAmI
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!
Jun 27 '08 #3

P: n/a
jc wrote:
RE: Two Classes with the same Data Structure.. saving code? Inheriting
a structure?

I have two classes. One in Inherits System.Collections.CollectionBase,
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.Collections.CollectionBase
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
Jun 27 '08 #4

P: n/a
jc
On Jun 13, 5:22 pm, Göran Andersson <gu...@guffa.comwrote:
jc wrote:
RE: Two Classes with the same Data Structure.. saving code? Inheriting
a structure?
I have two classes. One in Inherits System.Collections.CollectionBase,
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.Collections.CollectionBase
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!
Jun 27 '08 #5

P: n/a
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 "aggregating" 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.Collections.CollectionBase
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.

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.