473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 1527
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
2
by: Peter Bates | last post by:
Hi, I'm just getting used to XSDObjectGen and i have the following question. Can i use a class inherited from a class generated by XSDObjectGen with XmlSerialize? Specifically, I have many...
4
by: Chuck Bowling | last post by:
I am using CodeDOM to generate source files. The classes being generated have a const string member. This member is referenced in an abstract base class but declared in the inheriting class. I...
6
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
6
by: David Lozzi | last post by:
Howdy, I'm new to classes. Below is my class User. (is this a reserved namespace or class?) It works great, kind of. If I specify the username and password, the correct firstname and lastname...
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.