where to put global code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mike.miller

    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.Maj orSystem.MajorC lassification.E tc.
    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.

    [color=blue]
    >-----Original Message-----
    >hi,
    >
    >i have a couple of classes which look something like[/color]
    this:[color=blue]
    >
    >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[/color]
    (ie: connection object,[color=blue]
    >connection string, etc), and this function should make a[/color]
    connection to the[color=blue]
    >database. i want this function to be accessible by both[/color]
    classes. where would[color=blue]
    >i put this code and what would my class look like (in[/color]
    terms of namespace,[color=blue]
    >class name, etc). also, how would i call the function?
    >
    >thanks.
    >
    >
    >.
    >[/color]
  • Mike M

    #2
    Re: where to put global code?

    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.
    [color=blue]
    >-----Original Message-----
    >Hi Mike,
    >
    >I am new to c# so Im still not 100% clear about static[/color]
    etc.[color=blue]
    >
    >I did the following and it seems to work...
    >
    >public static void CreateConnectio n(ref SqlConnection[/color]
    oConn, string[color=blue]
    >strConnectionS tring, .... etc).
    >
    >But as you can see I had to put "ref" in front of the[/color]
    connection, so it is[color=blue]
    >passed by reference.
    >
    >Is this the best/neatest way?
    >
    >
    >
    >"mike.miller " <mike.miller@bc bskc.com> wrote in message
    >news:0ce101c34 d2b$2f04ebd0$a4 01280a@phx.gbl. ..[color=green]
    >> 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.Maj orSystem.MajorC lassification.E tc.
    >> Although, I find it hard to create a new namespace for[/color][/color]
    a[color=blue][color=green]
    >> single class - unless the solution is big enough that[/color][/color]
    it[color=blue][color=green]
    >> would make less sense to stick it in some generic
    >> namespace.
    >>
    >>[color=darkred]
    >> >-----Original Message-----
    >> >hi,
    >> >
    >> >i have a couple of classes which look something like[/color]
    >> this:[color=darkred]
    >> >
    >> >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[/color]
    >> (ie: connection object,[color=darkred]
    >> >connection string, etc), and this function should[/color][/color][/color]
    make a[color=blue][color=green]
    >> connection to the[color=darkred]
    >> >database. i want this function to be accessible by[/color][/color][/color]
    both[color=blue][color=green]
    >> classes. where would[color=darkred]
    >> >i put this code and what would my class look like (in[/color]
    >> terms of namespace,[color=darkred]
    >> >class name, etc). also, how would i call the[/color][/color][/color]
    function?[color=blue][color=green][color=darkred]
    >> >
    >> >thanks.
    >> >
    >> >
    >> >.
    >> >[/color][/color]
    >
    >
    >.
    >[/color]

    Comment

    • suzy

      #3
      Re: where to put global code?

      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" <mike.miller@bc bskc.com> wrote in message
      news:0e4401c34d 32$735268b0$a30 1280a@phx.gbl.. .[color=blue]
      > 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.
      >[color=green]
      > >-----Original Message-----
      > >Hi Mike,
      > >
      > >I am new to c# so Im still not 100% clear about static[/color]
      > etc.[color=green]
      > >
      > >I did the following and it seems to work...
      > >
      > >public static void CreateConnectio n(ref SqlConnection[/color]
      > oConn, string[color=green]
      > >strConnectionS tring, .... etc).
      > >
      > >But as you can see I had to put "ref" in front of the[/color]
      > connection, so it is[color=green]
      > >passed by reference.
      > >
      > >Is this the best/neatest way?
      > >
      > >
      > >
      > >"mike.miller " <mike.miller@bc bskc.com> wrote in message
      > >news:0ce101c34 d2b$2f04ebd0$a4 01280a@phx.gbl. ..[color=darkred]
      > >> 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.Maj orSystem.MajorC lassification.E tc.
      > >> Although, I find it hard to create a new namespace for[/color][/color]
      > a[color=green][color=darkred]
      > >> single class - unless the solution is big enough that[/color][/color]
      > it[color=green][color=darkred]
      > >> 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[/color][/color]
      > make a[color=green][color=darkred]
      > >> connection to the
      > >> >database. i want this function to be accessible by[/color][/color]
      > both[color=green][color=darkred]
      > >> 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[/color][/color]
      > function?[color=green][color=darkred]
      > >> >
      > >> >thanks.
      > >> >
      > >> >
      > >> >.
      > >> >[/color]
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      • Mike M

        #4
        Re: where to put global code?

        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.


        [color=blue]
        >-----Original Message-----
        >thanks,
        >
        >what if i wanted common code to be used by other classes[/color]
        and didn't want it[color=blue]
        >shared across all instances of the class?
        >
        >how would i do this?
        >
        >thanks again.
        >
        >"Mike M" <mike.miller@bc bskc.com> wrote in message
        >news:0e4401c34 d32$735268b0$a3 01280a@phx.gbl. ..[color=green]
        >> I would pass it by reference. It is generally cheaper,
        >> as you pass a reference to the original object instead[/color][/color]
        of[color=blue][color=green]
        >> copying it. In some cases you could run into problems
        >> because static values are shared across all instances,[/color][/color]
        so[color=blue][color=green]
        >> 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.
        >>[color=darkred]
        >> >-----Original Message-----
        >> >Hi Mike,
        >> >
        >> >I am new to c# so Im still not 100% clear about static[/color]
        >> etc.[color=darkred]
        >> >
        >> >I did the following and it seems to work...
        >> >
        >> >public static void CreateConnectio n(ref SqlConnection[/color]
        >> oConn, string[color=darkred]
        >> >strConnectionS tring, .... etc).
        >> >
        >> >But as you can see I had to put "ref" in front of the[/color]
        >> connection, so it is[color=darkred]
        >> >passed by reference.
        >> >
        >> >Is this the best/neatest way?
        >> >
        >> >
        >> >
        >> >"mike.miller " <mike.miller@bc bskc.com> wrote in[/color][/color][/color]
        message[color=blue][color=green][color=darkred]
        >> >news:0ce101c34 d2b$2f04ebd0$a4 01280a@phx.gbl. ..
        >> >> You could create a static class for the connection[/color][/color][/color]
        with[color=blue][color=green][color=darkred]
        >> >> static properties for the connection object itself[/color][/color][/color]
        and[color=blue][color=green][color=darkred]
        >> >> all of the associated properties. Just be aware[/color][/color][/color]
        that[color=blue][color=green][color=darkred]
        >> >> only one object can use a connection at one time.
        >> >>
        >> >> As far as the namespace, it depends on whether you[/color][/color][/color]
        will[color=blue][color=green][color=darkred]
        >> >> want to reuse this class for other apps. The normal
        >> >> naming convention is
        >> >> CompanyName.Maj orSystem.MajorC lassification.E tc.
        >> >> Although, I find it hard to create a new namespace[/color][/color][/color]
        for[color=blue][color=green]
        >> a[color=darkred]
        >> >> single class - unless the solution is big enough[/color][/color][/color]
        that[color=blue][color=green]
        >> it[color=darkred]
        >> >> would make less sense to stick it in some generic
        >> >> namespace.
        >> >>
        >> >>
        >> >> >-----Original Message-----
        >> >> >hi,
        >> >> >
        >> >> >i have a couple of classes which look something[/color][/color][/color]
        like[color=blue][color=green][color=darkred]
        >> >> 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[/color][/color][/color]
        parameters[color=blue][color=green][color=darkred]
        >> >> (ie: connection object,
        >> >> >connection string, etc), and this function should[/color]
        >> make a[color=darkred]
        >> >> connection to the
        >> >> >database. i want this function to be accessible by[/color]
        >> both[color=darkred]
        >> >> classes. where would
        >> >> >i put this code and what would my class look like[/color][/color][/color]
        (in[color=blue][color=green][color=darkred]
        >> >> terms of namespace,
        >> >> >class name, etc). also, how would i call the[/color]
        >> function?[color=darkred]
        >> >> >
        >> >> >thanks.
        >> >> >
        >> >> >
        >> >> >.
        >> >> >
        >> >
        >> >
        >> >.
        >> >[/color][/color]
        >
        >
        >.
        >[/color]

        Comment

        Working...