C# Design Help

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

Hi everyone, I have a general class design question. I come from a
VB5-6 background where I made applications that used an active x dll
to access the database. In the VB world each class was in a seperate
file and I was able to have a module that they all shared that had
global variables, like my ado connection object. In C sharp it looks
like all my class can be contained into one .cs file, so my question
is where would I put any variables that I want all my classes to have
access to. I have been experimenting a little and I looks like I
could have one top level class and then put all my sub classes inside
that class but is this the right way to design it?
 
For shared variables, there are a couple of methods to possibly use:

1. Use static variables and properties, which retain their values.
2. Create a singleton. This ensures that there is one instance alive that
can be shared.

Both are better than embedding classes to facilitate "global" vars.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Hi Frank,

You can decide if you want all the classes in the same file or in different
files, it's up to you to decide.

You are right regarding that all the variables must be inside a class, you
can declare a class with only static members representing your global
variables, so they are accesible from anywhere by using:
ClassName.VariableName

I suggest you that instead of using public variables you keep them private
and use properties instead.


hope this help,
 
Back
Top