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

How to detect the existance of a private member from inside the class itself

Bob
I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and
call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance
impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be that
there is no need to write the same Dispose code in every derived class that
has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived classes.
private SqlConnection _conn;
.......
}

Nov 18 '05 #1
4 1154
Bob,

With reflection, you *can* get the private fields. Simply pass the
"BindingFlags.NonPublic" flag to the "Type.GetFields" call.

On the other hand, this is pretty bad design IMHO. Private fields are
private for a reason. It should be up to the derived class to dispose of its
own fields, not the base class. I would override the Dispose() method in the
derived class and dispose of my variables there.

HTH,

Trev
" Bob" <bo*******@yahoo.com> wrote in message
news:ew*************@TK2MSFTNGP12.phx.gbl...
I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and
call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance
impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be that there is no need to write the same Dispose code in every derived class that has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived classes. private SqlConnection _conn;
.......
}

Nov 18 '05 #2
Bob,

I agree with Trev, accessing the private members this way is not an optimum
design. What is it you are trying to achieve by descending from MyBase?
Utility inheritance is generally considered a bad decision to make something
a child class simply to get utility functions. If you want to deal with the
fact classes may forget to close their connections, and thus you wish to
remove that burden, create a MySQLBase class:

public class MySQLBase
{
private SQLConnection _....

protected SQLConnection SQLConnection
{
get { return _connection; }
}
}

public class MyClass : MySQLBase
{
}

" Bob" <bo*******@yahoo.com> wrote in message
news:ew*************@TK2MSFTNGP12.phx.gbl...
I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and
call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance
impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be that there is no need to write the same Dispose code in every derived class that has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived classes. private SqlConnection _conn;
.......
}

Nov 18 '05 #3
A base class is there to "provide" features to the derived classes, and to
"offer" common code. Enforcement comes from someone taking you up on your
offer. A base class is not there to "enforce" good programming practice on
the part of the derived class.

Therefore, find a way to offer the feature that the derived class needs. Do
it in a way that YOU can enforce good practices. Then, you can get what you
want.

You've had some good suggestions here. Either derive an intermediate class
that already contains the SqlConnection member, or add the SqlConnection
member to your class. (I like the former more than the latter, but I do not
know what your class does).

Enforcement for the sake of a bit of utility code (not coding Dispose
multiple times) is an expensive proposition in the long run.

Besides, if a derived class is so poorly written that it does not properly
dispose of it's resources, it may have many other bugs as well... bugs that
need to be refactored out. Protecting bad code can only lead to later
problems.

My $0.02
--- Nick

" Bob" <bo*******@yahoo.com> wrote in message
news:ew*************@TK2MSFTNGP12.phx.gbl...
I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and
call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance
impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be that there is no need to write the same Dispose code in every derived class that has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived classes. private SqlConnection _conn;
.......
}

Nov 18 '05 #4
Bob
Thanks a lot for the replies Trev and Nick. I agree with Nick's comments on
the enforcement issue and decided even if reflection works the performance
hit is too big. I think to make the Dispose() an abstract method in my base
class and therefore force an implementation in the derived classes is
probably the best way to go in my base. This serves as a good way to remind
the derived class developer what needs to be done (e.g. closing the
underlying connection).

Thanks
Bob
"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:npgOb.86901$I06.386594@attbi_s01...
A base class is there to "provide" features to the derived classes, and to
"offer" common code. Enforcement comes from someone taking you up on your
offer. A base class is not there to "enforce" good programming practice on the part of the derived class.

Therefore, find a way to offer the feature that the derived class needs. Do it in a way that YOU can enforce good practices. Then, you can get what you want.

You've had some good suggestions here. Either derive an intermediate class that already contains the SqlConnection member, or add the SqlConnection
member to your class. (I like the former more than the latter, but I do not know what your class does).

Enforcement for the sake of a bit of utility code (not coding Dispose
multiple times) is an expensive proposition in the long run.

Besides, if a derived class is so poorly written that it does not properly
dispose of it's resources, it may have many other bugs as well... bugs that need to be refactored out. Protecting bad code can only lead to later
problems.

My $0.02
--- Nick

" Bob" <bo*******@yahoo.com> wrote in message
news:ew*************@TK2MSFTNGP12.phx.gbl...
I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be

that
there is no need to write the same Dispose code in every derived class

that
has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived

classes.
private SqlConnection _conn;
.......
}


Nov 18 '05 #5

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

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
6
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >>...
5
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
3
by: Richard Webb | last post by:
Hi all, I guess this is more of a design problem than a language problem, but I'm confused either way! I have a class and it has a private data member which is a struct. The size of the struct is...
5
by: pmatos | last post by:
Hi all, I have a style question. I've been for long programming in Lisp-like languages and C when I need a low-level language. Now, I'm programming for professional reasons in C++ (which I had...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
5
by: Bob | last post by:
I want to find a way to detect the existance of the private member of a particular type in the derived class from inside the base class itself and call its Dispose() method. Reflection GetFields()...
6
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.