473,729 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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!
Jun 27 '08 #1
5 1555
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

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.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!
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.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
Jun 27 '08 #4
jc
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!
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 "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.

Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
3609
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
2233
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 xml files arriving from a PC inventory scanner we use. I wish to deserialize them and then process them.
4
3553
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 can't declare the string in the abstract class because it would require initialization. Is there a way to require that any class inheriting from the base class contain this string without a specific declaration in the base class? I tried putting a...
6
4487
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 systems over the years. I have developed code in more than 30 distinct programming languages for a wide cariety of industries. But this issue of structures in C# is beginning to annoy me. I should also make it clear that I am not a big supporter...
6
1581
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 are returned. For example, username dlozzi, password fun. It returns David Lozzi as full name. If I login as someone else on another computer, say username dsmith and password fun2, the second computer displays the correct fullname. HOWEVER if I...
11
2350
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 policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of the boost dynamic bitset. Another would be the use of the std::vector. I obtained some code that...
7
2501
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
2783
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 overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
7
3129
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 "myList" as in the example below ? #include "Sector.h" using namespace boost;
0
8928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8766
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9291
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9217
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.