By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,327 Members | 1,096 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,327 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 20 '05 #2

P: n/a
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 20 '05 #3

P: n/a
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 20 '05 #4

P: n/a
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 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.