473,321 Members | 1,778 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,321 software developers and data experts.

Pass an instantiated base class to a new class that inherits the same class -- Possible??

OK, I may be asking for the impossible, but I'll give it a shot: For a case
like this:

using System.Data.SqlClient;
abstract class Base
{
private SqlConnection DBConnect;
Base(string SqlConnectStr)
{
DBConnect = new SqlConnection(SqlConnectStr);
}

// Some other base code here that relys on the DBConnect object
}

sealed class Derived1 : Base
{
Derived1(string ConnectStr):base(ConnectStr)
// etc
}

sealed class Derived2 : Base
{
Derived2(string ConnectStr):base(ConnectStr)

void func()
{
Derived1 DerivedObject;
// here I would like DerivedObject to inherit the Base class already
// instantiated by Derived2 (so that it uses the same SQL Server
connection), rather than creating it's own instance
DerivedObject = new Derived1();
}
}

I would like for Derived2 to pass the SqlConnection that Base has created
for it along to Derived1 when it creates an object for it; from my thinking,
it would be nice if there was some way to force Derived1:Base to accept
Derived2:Base's object rather than instantiating its own.

I can think of two potential solutions, neither of which is very appealing
to me. The first is to create a new Base constructor that takes a
SqlConnection, and make it's internal SqlConnection protected.
Unfortunately, this is code in a DLL, and because C# has no concept of
friend classes, this creates a sort-of "backdoor" situation whereby a) any
derived class from Base can mess with the SQL Server Connection, and b) it
creates an unwanted API interface that allows "unauthorized" code to inject
a SQL Server connection. The other solution would be to simply pass the
ConnectStr string and create a new SQL Connection, but I would also like to
limit the number of connections that I make if possible because there exists
the potental for a lot of database connections already within the scope of
the overall app.

Any other solutions to my dilemma?
Nov 15 '05 #1
3 1716
SQL Server connections are already pooled. If you use a consistent
connection string across your connection requests, then aquire late and
release early the connections will be pooled automatically by the runtime.
You're looking at quite a lot of work if you want to re-invent the wheel and
implement a homegrown pooling mechanism.

And you can't share superclasses instance identity among multiple subclass
instances.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
Nov 15 '05 #2
Brad:

Before I go on, note that I also agree with Mickey on the connection
pooling - why reinvent your own?You're normally better off creating (new)
and destroying (dispose) your connections in as short a scope as possible.

1) C# does have a concept like "friend" - it's "internal". This limits
accessibility to callers in the same assembly. (note that it is like
private - you can get around it with reflection)

2) You could use somthing like this static hashtable to store connections.
I don't recommend this particular solution, because it will keep the
connections around for the life of the appdomain - probably the life of your
application. However, you could 'tweak' it to remove the instances of the
connections, but then you're reference counting. Again, go with .net's
connection pooling.

abstract class Base
{
...
private static Hashtable connections = new Hashtable();
Base(string SqlConnectStr)
{
if( connections.Contains(SqlConnectStr))
{
DBConnect = (SqlConnection)connections[SqlConnectStr];
}
else
{
DBConnect = new SqlConnection(SqlConnectStr);
connections.Add(SqlConnectStr, DBConnect);
}
....
}
"Brad Navarro" <a@a.com> wrote in message
news:jS******************@twister.socal.rr.com...
OK, I may be asking for the impossible, but I'll give it a shot: For a case like this:

using System.Data.SqlClient;
abstract class Base
{
private SqlConnection DBConnect;
Base(string SqlConnectStr)
{
DBConnect = new SqlConnection(SqlConnectStr);
}

// Some other base code here that relys on the DBConnect object
}

sealed class Derived1 : Base
{
Derived1(string ConnectStr):base(ConnectStr)
// etc
}

sealed class Derived2 : Base
{
Derived2(string ConnectStr):base(ConnectStr)

void func()
{
Derived1 DerivedObject;
// here I would like DerivedObject to inherit the Base class already // instantiated by Derived2 (so that it uses the same SQL Server
connection), rather than creating it's own instance
DerivedObject = new Derived1();
}
}

I would like for Derived2 to pass the SqlConnection that Base has created
for it along to Derived1 when it creates an object for it; from my thinking, it would be nice if there was some way to force Derived1:Base to accept
Derived2:Base's object rather than instantiating its own.

I can think of two potential solutions, neither of which is very appealing to me. The first is to create a new Base constructor that takes a
SqlConnection, and make it's internal SqlConnection protected.
Unfortunately, this is code in a DLL, and because C# has no concept of
friend classes, this creates a sort-of "backdoor" situation whereby a) any
derived class from Base can mess with the SQL Server Connection, and b) it
creates an unwanted API interface that allows "unauthorized" code to inject a SQL Server connection. The other solution would be to simply pass the
ConnectStr string and create a new SQL Connection, but I would also like to limit the number of connections that I make if possible because there exists the potental for a lot of database connections already within the scope of
the overall app.

Any other solutions to my dilemma?

Nov 15 '05 #3

Hi Brad,

Do you still have any concern?
Please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

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

Similar topics

3
by: thechaosengine | last post by:
Hi all, I wanted to put some common security functions into a class that inherits from the Page class and then use the new class as the basis for all my pages. Unfortunately, if I try and...
3
by: crichmon | last post by:
Any general advice for dealing with circular dependencies? For example, I have a situation which, when simplified, is similar to: ///////////// // A.h class A { public: int x;
8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
12
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
1
by: miben | last post by:
I want to create a new inherited class givin its base. For example: class Base { public: void operator =(const Base &base) { // copy base items } }; class Inherits: public Base {
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.