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

private field v.s. parameter

I have a class I'm writing that has aprox 20 methods, a mixture of both
public and private. Of the private methods, 3 use a database command object
(OleDbCommand) for either inserting data or selecting data. AM I better off
making the OleDbCommand object a private field to the class, or passing the
actual object to the three methods that'll be using it? Advantages?
Disadvantages?
Nov 17 '05 #1
5 1943
I guess the question that needs to be asked is .. what functionality are you
trying to achieve by passing as a parameter? If the same command object is
passed every time then I really don't see the benefit of doing this.

Br,

Mark.
"JSheble" <js************@logicor.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
I have a class I'm writing that has aprox 20 methods, a mixture of both
public and private. Of the private methods, 3 use a database command
object (OleDbCommand) for either inserting data or selecting data. AM I
better off making the OleDbCommand object a private field to the class, or
passing the actual object to the three methods that'll be using it?
Advantages? Disadvantages?

Nov 17 '05 #2
JSheble <js************@logicor.com> wrote:
I have a class I'm writing that has aprox 20 methods, a mixture of both
public and private. Of the private methods, 3 use a database command object
(OleDbCommand) for either inserting data or selecting data. AM I better off
making the OleDbCommand object a private field to the class, or passing the
actual object to the three methods that'll be using it? Advantages?
Disadvantages?


The fewer classes you can have which have member variables which
implement IDisposable, the better. If you have them, you really ought
to implement IDisposable yourself. If you use a parameter, you can just
do (from the calling code):

using (OleDbCommand foo = ...)
{
CallMethod (foo, ...);
}

and everything will be taken care of without you having to write your
own IDisposable implementation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Hi,

I'm of the opinion its better to pass the object through method
parameters. Most of the other methods don't care about it at all,
and you'll run less chance of 'accidently' using it when you shouldn't
if you keep things as local as possible.

I tend to keep fields only to hold the values of the classes
properties, since they represent that state of your object. The
command object doesn't represent any kind of state, you're just using
it to get work done.

I'm sure there are many other opinions, but I don't think you'll go
wrong keeping variables scoped as locally as possible.

HTH
Andy

Nov 17 '05 #4
Hi,

Why don;t you create the object in each method ?

Or if more than one class interact with the DB you could create an object
that encapsulate the DB operations. then you export methods like this:

public static void ExecuteNonScalar( OleDbCommand)

The good thing about such a design is that in one class you concentrate all
related to connections and the others DB access related issues, the classes
only need to build a Command that they pass as a parameter.

MS also has a similar architecture, take a look at it too.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"JSheble" <js************@logicor.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
I have a class I'm writing that has aprox 20 methods, a mixture of both
public and private. Of the private methods, 3 use a database command
object (OleDbCommand) for either inserting data or selecting data. AM I
better off making the OleDbCommand object a private field to the class, or
passing the actual object to the three methods that'll be using it?
Advantages? Disadvantages?

Nov 17 '05 #5
I would destroy the OleDbCommand as soon as possible. The fact that you are
considering assigning it to a field suggests you are holding onto it too
long.

--
Jonathan Allen
"JSheble" <js************@logicor.com> wrote in message
news:uH**************@TK2MSFTNGP12.phx.gbl...
I have a class I'm writing that has aprox 20 methods, a mixture of both
public and private. Of the private methods, 3 use a database command
object (OleDbCommand) for either inserting data or selecting data. AM I
better off making the OleDbCommand object a private field to the class, or
passing the actual object to the three methods that'll be using it?
Advantages? Disadvantages?

Nov 17 '05 #6

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

Similar topics

5
by: Li Pang | last post by:
Hi, I made a crystal report which contains a text field of empty value. I want to know how to fill this field at the run time by a variable. Andbody give a hand? Thank in advance
0
by: blinky44 | last post by:
Hey, OK, I have 2 questions about parameters fields in crystal. 1)Is it possible to have a dynamically building parameter field? So a parameter is based on a field in a table and when...
10
by: Gandalf | last post by:
Hi, I created a C# application which is using many private variables. I'm working on VS .NET 2003 and have the following error (whereas the variable is used in the method of the class !) : ...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
3
by: Daron | last post by:
Is it possible to use a parameter as the field name, not just a criteria? Snippet of my SQL: SELECT Eval("!!.(0)") AS TestField, AS from qry_main The parameter will hold the name of the...
2
by: K B | last post by:
Hi, Is there ANY WAY to hide a row/field in a DetailsView based on meeting a condition at the ItemCreated or ModeChanging event -- or any other way? There appears not to be, but I was hoping for...
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
0
by: siddu | last post by:
Hi All, I know how to create a parameter field and Formulafield. Please tell me How to use parameter field in my Report. I Created one parameter field with name.A window opened which taking...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.