473,769 Members | 6,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15 '05 #1
5 2040
Bob,

With reflection, you *can* get the private fields. Simply pass the
"BindingFlags.N onPublic" 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*******@yaho o.com> wrote in message
news:ew******** *****@TK2MSFTNG P12.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 15 '05 #2
Well, you can have a MySqlBase class between MyBase and the derived
classes that have SqlConnection, and make _conn a protected member
there. Put the Dispose logic in MySqlBase.

Cheers
Elder

Bob wrote:
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 15 '05 #3
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*******@yaho o.com> wrote in message
news:ew******** *****@TK2MSFTNG P12.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 15 '05 #4
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*******@yaho o.com> wrote in message
news:ew******** *****@TK2MSFTNG P12.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 15 '05 #5
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*******@hotm ail.nospam.com> wrote in message
news:npgOb.8690 1$I06.386594@at tbi_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*******@yaho o.com> wrote in message
news:ew******** *****@TK2MSFTNG P12.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 15 '05 #6

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

Similar topics

6
2177
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 >> (istream &, abc &); private:
5
2416
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
2068
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 what I would call relatively large (about 1Mb). I have written methods for this class so that the struct can be correctly filled with the corerct data and certain parts of the struct can be extracted. But the problem I face is that I now have...
5
2185
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 programmed in before but only in college) and I'm getting to like it, however I'm having some issues with style. For example, If I have an object which I define as a private member of a class, for
8
7830
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 scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
6
3590
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 myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
4
1171
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() 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...
1
5400
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 completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried, and all examples I have found that other people says works - doesn't work for me :-(
14
4211
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
0
9589
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
9423
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,...
1
9999
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
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
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...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
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...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2815
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.