Structural question regarding scope of variables - CSharp

This is a discussion on Structural question regarding scope of variables - CSharp ; Hi group, I've been developing a number of classes, many of which have a similar structure, as follows: namespace { public class classname { Private Fields private readonly static string connString = ConfigurationManager.ConnectionStrings["SCMConnectionString"].ConnectionString; Public Fields Public Properties Private Instance Methods ...

+ Reply to Thread
Results 1 to 2 of 2

Structural question regarding scope of variables

  1. Default Structural question regarding scope of variables

    Hi group,

    I've been developing a number of classes, many of which have a similar
    structure, as follows:

    namespace
    {
    public class classname
    {
    Private Fields
    private readonly static string connString =
    ConfigurationManager.ConnectionStrings["SCMConnectionString"].ConnectionString;

    Public Fields

    Public Properties

    Private Instance Methods

    Public Instance Methods

    Public Static Methods
    // these methods use the connString variable to establish database
    connections, using Microsoft.ApplicationBlocks.Data
    }
    }

    However, the static connString is declared in every class. I'm
    currently only using one connection, but I foresee using 2 or 3
    different connection strings, based on where the data will need to
    come from.
    Can anyone suggest a better way of doing this, so that I don't have to
    declare this variable in every class?
    Note that I had considered using inheritance by basing all the classes
    that need data on a base class that contains the static connection
    string. However, that doesn't seem the right approach because I only
    want to achieve the effect of a global variable. Speaking of global
    variables, note that I'm targeting a web environment.

    Any thoughts on this?


  2. Default Re: Structural question regarding scope of variables

    Hi,
    "Froefel" <hansdeschryver> wrote in message
    news:1185984049.246269.303950@q3g2000prf.googlegroups.com...
    > Hi group,
    >
    > I've been developing a number of classes, many of which have a similar
    > structure, as follows:
    >
    > namespace
    > {
    > public class classname
    > {
    > Private Fields
    > private readonly static string connString =
    > ConfigurationManager.ConnectionStrings["SCMConnectionString"].ConnectionString;
    >


    > However, the static connString is declared in every class. I'm
    > currently only using one connection, but I foresee using 2 or 3
    > different connection strings, based on where the data will need to
    > come from.
    > Can anyone suggest a better way of doing this, so that I don't have to
    > declare this variable in every class?
    >
    > Any thoughts on this?
    >


    A better approach would be to create a new class, static class, to just hold
    the connection string needed. In case that you have more than one connection
    then it will be up to the class to determine which one to use. That will
    depend of how you divide the data and how the classes how where the data
    they need is.



+ Reply to Thread

Similar Threads

  1. Compiler bug? Two variables of same name in one scope
    By Application Development in forum c++
    Replies: 15
    Last Post: 11-16-2007, 01:35 PM
  2. The scope of local variables -- where are they?
    By Application Development in forum RUBY
    Replies: 4
    Last Post: 09-09-2007, 06:17 AM
  3. Scope of variables within a FOR EACH ... NEXT LOOP
    By Application Development in forum DOTNET
    Replies: 2
    Last Post: 11-10-2005, 12:36 PM
  4. Scope And Persistance of Variables in a Method
    By Application Development in forum Java
    Replies: 6
    Last Post: 06-19-2004, 01:08 PM