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

where to put global code?

You could create a static class for the connection with
static properties for the connection object itself and
all of the associated properties. Just be aware that
only one object can use a connection at one time.

As far as the namespace, it depends on whether you will
want to reuse this class for other apps. The normal
naming convention is
CompanyName.MajorSystem.MajorClassification.Etc.
Although, I find it hard to create a new namespace for a
single class - unless the solution is big enough that it
would make less sense to stick it in some generic
namespace.

-----Original Message-----
hi,

i have a couple of classes which look something like this:
namespace BookStore
{
public class Customer
{
public Customer()
{
// initialise customer object here
}
}
}
namespace BookStore
{
public class Order
{
public Order()
{
// initialise order object here
}
}
}
i want to create a function takes in some parameters (ie: connection object,connection string, etc), and this function should make a connection to thedatabase. i want this function to be accessible by both classes. where wouldi put this code and what would my class look like (in terms of namespace,class name, etc). also, how would i call the function?

thanks.
.

Nov 13 '05 #1
3 1998
I would pass it by reference. It is generally cheaper,
as you pass a reference to the original object instead of
copying it. In some cases you could run into problems
because static values are shared across all instances, so
you just have to make sure that one piece of code won't
change something that will break something else.

With a connection object, you should be OK. Only one
object can use it at a time and you would want all
objects to be affected if you changed something at the
connection level. Hope this helps.
-----Original Message-----
Hi Mike,

I am new to c# so Im still not 100% clear about static etc.
I did the following and it seems to work...

public static void CreateConnection(ref SqlConnection oConn, stringstrConnectionString, .... etc).

But as you can see I had to put "ref" in front of the connection, so it ispassed by reference.

Is this the best/neatest way?

"mike.miller" <mi*********@bcbskc.com> wrote in message
news:0c****************************@phx.gbl...
You could create a static class for the connection with
static properties for the connection object itself and
all of the associated properties. Just be aware that
only one object can use a connection at one time.

As far as the namespace, it depends on whether you will
want to reuse this class for other apps. The normal
naming convention is
CompanyName.MajorSystem.MajorClassification.Etc.
Although, I find it hard to create a new namespace for a single class - unless the solution is big enough that it would make less sense to stick it in some generic
namespace.

>-----Original Message-----
>hi,
>
>i have a couple of classes which look something like

this:
>
>namespace BookStore
>{
> public class Customer
> {
> public Customer()
> {
> // initialise customer object here
> }
> }
>}
>
>
>namespace BookStore
>{
> public class Order
> {
> public Order()
> {
> // initialise order object here
> }
> }
>}
>
>
>i want to create a function takes in some parameters

(ie: connection object,
>connection string, etc), and this function should make a
connection to the
>database. i want this function to be accessible by
both classes. where would
>i put this code and what would my class look like (in

terms of namespace,
>class name, etc). also, how would i call the

function? >
>thanks.
>
>
>.
>

.

Nov 13 '05 #2
thanks,

what if i wanted common code to be used by other classes and didn't want it
shared across all instances of the class?

how would i do this?

thanks again.

"Mike M" <mi*********@bcbskc.com> wrote in message
news:0e****************************@phx.gbl...
I would pass it by reference. It is generally cheaper,
as you pass a reference to the original object instead of
copying it. In some cases you could run into problems
because static values are shared across all instances, so
you just have to make sure that one piece of code won't
change something that will break something else.

With a connection object, you should be OK. Only one
object can use it at a time and you would want all
objects to be affected if you changed something at the
connection level. Hope this helps.
-----Original Message-----
Hi Mike,

I am new to c# so Im still not 100% clear about static

etc.

I did the following and it seems to work...

public static void CreateConnection(ref SqlConnection

oConn, string
strConnectionString, .... etc).

But as you can see I had to put "ref" in front of the

connection, so it is
passed by reference.

Is this the best/neatest way?

"mike.miller" <mi*********@bcbskc.com> wrote in message
news:0c****************************@phx.gbl...
You could create a static class for the connection with
static properties for the connection object itself and
all of the associated properties. Just be aware that
only one object can use a connection at one time.

As far as the namespace, it depends on whether you will
want to reuse this class for other apps. The normal
naming convention is
CompanyName.MajorSystem.MajorClassification.Etc.
Although, I find it hard to create a new namespace for a single class - unless the solution is big enough that it would make less sense to stick it in some generic
namespace.
>-----Original Message-----
>hi,
>
>i have a couple of classes which look something like
this:
>
>namespace BookStore
>{
> public class Customer
> {
> public Customer()
> {
> // initialise customer object here
> }
> }
>}
>
>
>namespace BookStore
>{
> public class Order
> {
> public Order()
> {
> // initialise order object here
> }
> }
>}
>
>
>i want to create a function takes in some parameters
(ie: connection object,
>connection string, etc), and this function should make a connection to the
>database. i want this function to be accessible by both classes. where would
>i put this code and what would my class look like (in
terms of namespace,
>class name, etc). also, how would i call the function? >
>thanks.
>
>
>.
>

.

Nov 13 '05 #3
Actually, I think I misspoke (wrote).

If the connection object itself is represented as a
static property, then there is no need to pass it at
all. Each class that needs it can just create an
instance of it. You would just need part of your code to
initialize the object first and set the connection
properties so that the individual objects don't have to.

You would pass by reference if you had an instance of a
regular class that you wanted to share. For example, if
you create an instance of the connection object and you
want several classes to use it, you can just create a
public connection property for the class and pass it in
by reference. That would accomplish what I wrote earler.

If you have code that you want to use across the
application, but each instance should own its own object,
you would just create a class with the appropriate public
properties and methods. Generally each class would
create its own instance. If the class had a bunch of
properties that need to be initialized a certain way
across the application, you might initialize it once and
then pass it by value to each class that needs it.
-----Original Message-----
thanks,

what if i wanted common code to be used by other classes and didn't want itshared across all instances of the class?

how would i do this?

thanks again.

"Mike M" <mi*********@bcbskc.com> wrote in message
news:0e****************************@phx.gbl...
I would pass it by reference. It is generally cheaper,
as you pass a reference to the original object instead of copying it. In some cases you could run into problems
because static values are shared across all instances, so you just have to make sure that one piece of code won't
change something that will break something else.

With a connection object, you should be OK. Only one
object can use it at a time and you would want all
objects to be affected if you changed something at the
connection level. Hope this helps.
>-----Original Message-----
>Hi Mike,
>
>I am new to c# so Im still not 100% clear about static

etc.
>
>I did the following and it seems to work...
>
>public static void CreateConnection(ref SqlConnection

oConn, string
>strConnectionString, .... etc).
>
>But as you can see I had to put "ref" in front of the

connection, so it is
>passed by reference.
>
>Is this the best/neatest way?
>
>
>
>"mike.miller" <mi*********@bcbskc.com> wrote in message >news:0c****************************@phx.gbl...
>> You could create a static class for the connection with >> static properties for the connection object itself and >> all of the associated properties. Just be aware that >> only one object can use a connection at one time.
>>
>> As far as the namespace, it depends on whether you will >> want to reuse this class for other apps. The normal
>> naming convention is
>> CompanyName.MajorSystem.MajorClassification.Etc.
>> Although, I find it hard to create a new namespace for
a
>> single class - unless the solution is big enough
that it
>> would make less sense to stick it in some generic
>> namespace.
>>
>>
>> >-----Original Message-----
>> >hi,
>> >
>> >i have a couple of classes which look something

like >> this:
>> >
>> >namespace BookStore
>> >{
>> > public class Customer
>> > {
>> > public Customer()
>> > {
>> > // initialise customer object here
>> > }
>> > }
>> >}
>> >
>> >
>> >namespace BookStore
>> >{
>> > public class Order
>> > {
>> > public Order()
>> > {
>> > // initialise order object here
>> > }
>> > }
>> >}
>> >
>> >
>> >i want to create a function takes in some parameters >> (ie: connection object,
>> >connection string, etc), and this function should

make a
>> connection to the
>> >database. i want this function to be accessible by

both
>> classes. where would
>> >i put this code and what would my class look like (in >> terms of namespace,
>> >class name, etc). also, how would i call the

function?
>> >
>> >thanks.
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 13 '05 #4

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

Similar topics

1
by: Andr? Roberge | last post by:
I have the following two files: #--testexec.py-- def exec_code(co): try: exec co except: print "error" #-- test.py--
2
by: urocrane | last post by:
i'm using visual c++ and it doesn't seem to have graphics header file. where can i get it? i've always had a wierd thought about prototypes, do you really need them? in my case, i think it looks...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
2
by: MLH | last post by:
I cut a mail function off the m'soft site. Has always worked. However, I would like to include error codes returned by the sendmail Fn and be able to understand what they mean. I had my first...
3
by: Zhigang Cui | last post by:
Hi, When I saw 'global static', the first response was there was no such term. But I realized it's an idiom later. When we learn C language, when we study C standard, when we study compiler, we...
3
by: Bob Rock | last post by:
Hello, this is something I've been asking myself for sometime ... and now I'd like to clarify this point. When should the try block start in a method??? What I mean is, does having all the code...
6
by: Andrea Williams | last post by:
Where is the best place to put global variables. In traditional ASP I used to put all of them into an include file and include it in every page. Will the Global.aspx.cs do that same thing? ...
4
by: John A Grandy | last post by:
I installed VS05 RC , created a new Web Site , but I do not see Global.asax , and I do not see Global.asax.cs in the App_Code dir ......
4
by: oliv | last post by:
I need some global app strong typed variable. So in my solution explorer, I did: Add New Item | Global Application class It add me a file Global.asax : <%@ Application Language="C#" %> ...
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...
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...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.