473,698 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing variable types of a function of a derived class

Hello,

I have a class that uses some variables to access a sql database, and I
want to make a derived class that inherits all the methods of this
class, because the derived one will do exactly the same process that
the parent one, but with a postgres database, thus I've got to use
diferent type variables to acces the database.

There's one big function in the class that does all the DB stuff, and I
want to know if there's any way to inherit the function but with only
changing the type of some vars inside this function.

Thanks!

Sergi

Sep 20 '06 #1
2 1480
Sergi

Is this what your trying to achieve...

abstract class MyBaseClass
{
private IDbConnection _connection;
public virtual IDbConnection Connection
{
get { return _connection; }
set { _connection = value; }
}

public MyBaseClass()
{
Connection = CreateConnectio n();
}

protected abstract IDbConnection CreateConnectio n();

public virtual void DoSomeSql()
{
if ( Connection.Stat e != ConnectionState .Open )
{
Connection.Open ();
}
using ( IDbCommand command = Connection.Crea teCommand() )
{
//If it's parameterised sql you'll need to check what the placeholders
should be for the DB type
//@=sql server, ?=odbc, :=oracle
command.Command Text = "SELECT * FROM T_SOME_DATA";

using ( IDataReader reader = command.Execute Reader() )
{
while ( reader.Read() )
{
//DoSomething
}
}
}
}
}
class MyOracleVersion : MyBaseClass
{
protected override IDbConnection CreateConnectio n()
{
return new OracleConnectio n( "Data Source=emp;user
id=scott;passwo rd=tiger" );
}
}
class MySqlServerVers ion : MyBaseClass
{
protected override IDbConnection CreateConnectio n()
{
return new SqlConnection( "How should I know this, I use Oracle" );
}
}

HTH

Glenn
"mrclash" <se************ @gmail.comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Hello,

I have a class that uses some variables to access a sql database, and I
want to make a derived class that inherits all the methods of this
class, because the derived one will do exactly the same process that
the parent one, but with a postgres database, thus I've got to use
diferent type variables to acces the database.

There's one big function in the class that does all the DB stuff, and I
want to know if there's any way to inherit the function but with only
changing the type of some vars inside this function.

Thanks!

Sergi

Sep 20 '06 #2
Yeah, this is it! I didn't knew of the IDbConnection interface.

Thanks a lot, and thanks to answer such a newbie question.

Sergi

Glenn wrote:
Sergi

Is this what your trying to achieve...

abstract class MyBaseClass
{
private IDbConnection _connection;
public virtual IDbConnection Connection
{
get { return _connection; }
set { _connection = value; }
}

public MyBaseClass()
{
Connection = CreateConnectio n();
}

protected abstract IDbConnection CreateConnectio n();

public virtual void DoSomeSql()
{
if ( Connection.Stat e != ConnectionState .Open )
{
Connection.Open ();
}
using ( IDbCommand command = Connection.Crea teCommand() )
{
//If it's parameterised sql you'll need to check what the placeholders
should be for the DB type
//@=sql server, ?=odbc, :=oracle
command.Command Text = "SELECT * FROM T_SOME_DATA";

using ( IDataReader reader = command.Execute Reader() )
{
while ( reader.Read() )
{
//DoSomething
}
}
}
}
}
class MyOracleVersion : MyBaseClass
{
protected override IDbConnection CreateConnectio n()
{
return new OracleConnectio n( "Data Source=emp;user
id=scott;passwo rd=tiger" );
}
}
class MySqlServerVers ion : MyBaseClass
{
protected override IDbConnection CreateConnectio n()
{
return new SqlConnection( "How should I know this, I use Oracle" );
}
}

HTH

Glenn
"mrclash" <se************ @gmail.comwrote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Hello,

I have a class that uses some variables to access a sql database, and I
want to make a derived class that inherits all the methods of this
class, because the derived one will do exactly the same process that
the parent one, but with a postgres database, thus I've got to use
diferent type variables to acces the database.

There's one big function in the class that does all the DB stuff, and I
want to know if there's any way to inherit the function but with only
changing the type of some vars inside this function.

Thanks!

Sergi
Sep 20 '06 #3

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

Similar topics

9
1435
by: Rob. | last post by:
I originally thought this was a compiler error but it seems the standard prohibits the code below. Has anyone got any good ideas about how to code around it? class A; class B; class X; class Y; class X
6
5873
by: eselk | last post by:
If I have: class A { public: class some_base_class **Obj; }; And I would like to redefine "Obj" in a class derived from class A, something like this maybe:
13
4264
by: Stephen Walch | last post by:
Error C2392 is hitting me hard! I have a managed C++ library that implements a bunch of fixed interfaces. For example, one interface is: public abstract interface IDbCommand { public abstract new System.Data.IDbConnection Connection }
14
7089
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to define some derived classes wich inherit the function and override the variable, but it doesn't work. Testcase:
11
3558
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object but x.gettype returns 'string', so it knows it contains a string. If I want to create a variable "y" as the same type that variable x contains how would I do that without having to iterate thru every possible
13
2839
by: dragoncoder | last post by:
Consider the following code #include <iostream> class Base { public: virtual void say() { std::cout << "Base" << std::endl; } }; class Derived: public base {
3
4549
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
9
2062
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
15
3526
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
0
8685
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
8612
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
9032
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
8905
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
7743
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...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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.