473,418 Members | 2,052 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,418 software developers and data experts.

Building class

In the system I am working on, there are 3 credit cards: Visa, MasterCard and
AmericanExpress. On the database side I have one table: credit_card
CREATE TABLE credit_card
( credit_card_id int not null PRIMARY KEY,
credit_card_code varchar(30) not null,
description varchar(255) null )

Now I am building classes and i can do it in 2 ways:

* Build a class "CreditCard".
public class CreditCard
{
private int _creditCardId;
private string _creditCardCode;
private string _description;
}

* Build a class "CreditCard" and then build 3 classes "Visa", "MasterCard"
and "AmericanExpress" all inherited from "CreditCard".
public class CreditCard
{
private int _creditCardId;
private string _creditCardCode;
private string _description;
}
public class Visa : CreditCard
{
}
public class MasterCard : CreditCard
{
}
public class AmericanExpress : CreditCard
{
}
Which way is better and why?

Thanks

Apr 30 '07 #1
4 1560
Hello Rizwan,

The second case is preferable, because u have the common data/logic for all
you classes which can be moved to the base class

Read about inheritance there http://en.wikipedia.org/wiki/Inherit...ter_science%29

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

RIn the system I am working on, there are 3 credit cards: Visa,
RMasterCard and
RAmericanExpress. On the database side I have one table: credit_card
RCREATE TABLE credit_card
R( credit_card_id int not null PRIMARY KEY,
Rcredit_card_code varchar(30) not null,
Rdescription varchar(255) null )
RNow I am building classes and i can do it in 2 ways:
R>
R* Build a class "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
R* Build a class "CreditCard" and then build 3 classes "Visa",
R"MasterCard"
Rand "AmericanExpress" all inherited from "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
Rpublic class Visa : CreditCard
R{
R}
Rpublic class MasterCard : CreditCard
R{
R}
Rpublic class AmericanExpress : CreditCard
R{
R}
RWhich way is better and why?
R>
RThanks
R>
Apr 30 '07 #2
Thanks Michael.

One other person I consult with, thinks the first way is better. He is of
the view that "your classes should match one-to-one with your tables". What
do you think about that?

Hello Rizwan,

The second case is preferable, because u have the common data/logic for all
you classes which can be moved to the base class

Read about inheritance there http://en.wikipedia.org/wiki/Inherit...ter_science%29

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

RIn the system I am working on, there are 3 credit cards: Visa,
RMasterCard and
RAmericanExpress. On the database side I have one table: credit_card
RCREATE TABLE credit_card
R( credit_card_id int not null PRIMARY KEY,
Rcredit_card_code varchar(30) not null,
Rdescription varchar(255) null )
RNow I am building classes and i can do it in 2 ways:
R>
R* Build a class "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
R* Build a class "CreditCard" and then build 3 classes "Visa",
R"MasterCard"
Rand "AmericanExpress" all inherited from "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
Rpublic class Visa : CreditCard
R{
R}
Rpublic class MasterCard : CreditCard
R{
R}
Rpublic class AmericanExpress : CreditCard
R{
R}
RWhich way is better and why?
R>
RThanks
R>
Apr 30 '07 #3
Rizwan <Ri****@discussions.microsoft.comwrote:
One other person I consult with, thinks the first way is better. He is of
the view that "your classes should match one-to-one with your tables". What
do you think about that?
What's important isn't the view, so much as the reasons behind it.
There are times when a sort of inheritance structure makes sense, and
other times when it would make life harder.

*Why* does he think that classes should match one-to-one with tables?
What advantages does he see to that approach? How applicable are they
to your situation? What advantages are there in the alternative
approach? How much difference is there (in code) between the three
different kinds of card? Could those differences be encapsulated in a
way other than straight inheritance?

In short, we haven't got enough information to say what the best course
is - you need to be looking at the pros and cons of each approach. If
there's "received wisdom" you should ask for the reasons behind it -
otherwise it's superstition rather than wisdom.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 30 '07 #4
Hi Rizwan,
I believe the person you consulted is thinking of using Reflection,
that's why he advised you to create a business Object class that match
one to one to your Database Table.
If you're going to build the system in the 3-tier architecture, that
person idea is best (i.e. creating each class for Visa, Mastercard and
Amex respectively).

But from language point of view, the second approach is more elegant
(^_^), since it's using inheritance.

Regards,
Hardono Arifanto
-----------------------
visit my blog at http://sodeve.net
On May 1, 7:06 am, Rizwan <Riz...@discussions.microsoft.comwrote:
Thanks Michael.

One other person I consult with, thinks the first way is better. He is of
the view that "your classes should match one-to-one with your tables". What
do you think about that?
Hello Rizwan,
The second case is preferable, because u have the common data/logic for all
you classes which can be moved to the base class
Read about inheritance therehttp://en.wikipedia.org/wiki/Inheritance_%28computer_science%29
---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog:http://spaces.live.com/laflour
Team blog:http://devkids.blogspot.com/
"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
RIn the system I am working on, there are 3 credit cards: Visa,
RMasterCard and
RAmericanExpress. On the database side I have one table: credit_card
RCREATE TABLE credit_card
R( credit_card_id int not null PRIMARY KEY,
Rcredit_card_code varchar(30) not null,
Rdescription varchar(255) null )
RNow I am building classes and i can do it in 2 ways:
R>
R* Build a class "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
R* Build a class "CreditCard" and then build 3 classes "Visa",
R"MasterCard"
Rand "AmericanExpress" all inherited from "CreditCard".
Rpublic class CreditCard
R{
Rprivate int _creditCardId;
Rprivate string _creditCardCode;
Rprivate string _description;
R}
Rpublic class Visa : CreditCard
R{
R}
Rpublic class MasterCard : CreditCard
R{
R}
Rpublic class AmericanExpress : CreditCard
R{
R}
RWhich way is better and why?
R>
RThanks
R>

May 1 '07 #5

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
9
by: Jenta | last post by:
A World Beyond Capitalism 2005, An Annual International Multiracial Alliance Building Peace Conference Is Accepting Proposals... ...and Online Registration is now available if you plan to table...
1
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am using VS.NET 2003. What I am doing apart from teaching myself C# is learning my way round V.NET2003 also.
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
2
by: tony | last post by:
Hello!! I use VS 2003 and C# for all class library except MeltPracCommon.dll which is C++.NET The problem is that I get these warnings when building the exe file and use my class libraries....
1
by: Diffident | last post by:
Hello All, I have a question as to why my users are noticing error when I am building the project on the production system. Here is the problem's background. In order to build the project on...
3
by: tony | last post by:
Hello!! I use VS 2003 and C# for all class library except MeltPracCommon.dll which is C++.NET The problem is that I get these warnings when building the exe file and use my class libraries....
12
by: Ludwig | last post by:
Hi all, we are building our own class library framework, with stuff in it that can be used in various projects at various clients. Initially we had 4 assemblies with everything in it. Of...
1
by: alqui | last post by:
Hi, I'm actually trying to resolve a simple problem. I'm trying to build my Web site. The structure of the Web site looks like that : /Element /Element/App_Code /Element/Admin...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.