If you access the field directly, then it works because you're not
"returning" anything, so no copy of the object is made - but a public
field automatically breaks the laws of encapsulation because there's
nothing stopping external code from completely overwriting the entire
object.  It works, but it's not necessarily good practice.  If you
make it a class instead of a struct, you can maintain encapsulation by
only passing out a reference on the "getter".

If you have a lot of booleans, then it may be better to use an array
or other collection rather than a bunch of individually named boolean
variables.  That way you can access individual members of your
collection without having public access to the actual object.  Since
your code isn't "real", it's hard for me to give a sample of what this
might look like, but I'm thinking something along the lines of:

public struct Seleccion {
  private bool[] _boolValues;
}
public void SetBoolValue(int idx, bool value) {
  sel_activa._boolValues[idx] = value;
}

Or if your boolean values are tied directly to the checkbox values,
you may be able to define a Dictionary that accesses a boolean based
on an element of that checkbox, such as its ID or other unique
property.  For example:
public Dictionary<string, bool> BoolsByCheckBox { get { /* get field
*/ } }

foreach (CheckBox cbox in /*all checkboxes*/) {
  BoolsByCheckBox[cbox.ID] = cbox.Checked;
}

To get the value of a specific checkbox-check from your class, just
call it from the Dictionary:
bool isChk3Checked = (BoolsByCheckBox.ContainsKey("chk3") &&
BoolsByCheckBox["chk3"]);

Don't know what your specific situation really looks like, so I don't
know if either of these works for you.  But any time you have that
many variables of the same type, you really need to use some kind of
collection to keep them organized, rather than a bunch of free-
floating variables.  FYI: There is a BitArray type out there that is
designed to handle a bunch of booleans faster than a bool[], so if you
decide to use an array, you may want to take a quick look at that.


By the way - I do agree with Nikhil that you shouldn't have a private
class inside Program - in my opinion, Program should only be used to
start the application, and not have anything else in it.  It doesn't
hurt anything to do it this way, but I believe it's just cleaner to
leave Program alone and have some other static class out there to
actually "do" stuff.

On Feb 19, 3:59 am, Nacho108 <[email protected]> wrote:
> Thanks everybody for the answers.
>
> Joe:
> The code I post is a "simplification" of what I have really. Really I
> have like more than 50 boolean variables that I would like to share
> between forms. They are all states of checkboxes in a form.
> So the real code would be:
>
> public struct Seleccion { public bool x; public bool y;  public bool
> z; etc } (like this 50 variables more)
>
> So defining more than 50 class properties would be very long. That's
> why I decided (maybe wrongly) to encapsulate all of them in an
> structure.
> Also defining a method for each variable would be very long.
>
> Trying to find a solution, I tried to access the structure directly
> from another forms, forgetting about the properties I defined and it
> WORKED. I did it like this:
>
> Program.sel_activa.x = B02.Checked;
>
> So I could even define separate 50 boolean variables and access them
> directly instead of an structure.
>
> Is there something wrong with this approach?
> I want to put clear that I'm just starting to learn object oriented
> programming (I program in VB in a structured programming fashion only)
> and that's why I'm asking this basic questions. Is there any advantage
> in making them visible through properties?
> I want to make this in the more suited "Object Oriented" way even if
> it's long, but I just would like to know which advantages gives me all
> the code I would have to type.
>
> Nikhil:
>
> What advantages would I have if I define it in a separate class? and
> is there any consideration I have to take into account to make those
> new class variables visible to all forms and classes?
>
> Thanks in advance,
> Regards!
> Nacho
>
> On Feb 18, 6:42 pm, nikhil gaitonde <[email protected]> wrote:
>
> > Hi Nacho,
>
> > Better don't use Program.cs to declare the property.
> > Instead declare another static class just to declare variables across the
> > application.
> > Something like a Statemanager.cs
> > Just let me know if this helps.
>
> > Regards,
> > Nikhil
>
> > On Wed, Feb 18, 2009 at 9:36 PM, Nacho108 <[email protected]> wrote:
>
> > > Hi everyone,
> > > I'm trying to create an structure so it can be seen from all forms. So
> > > doing a bit of research I decided to create an static property of the
> > > main class of the program.
>
> > > For this I've made this code:
>
> > > namespace WindowsApplication1
> > > {
> > >    static class Program
> > >    {
> > >        public struct Seleccion
> > >        {       public bool x;
> > >                public bool y;         }
>
> > >        private static Seleccion sel_activa ;
>
> > >        public static Seleccion _sel_activa
> > >        {        get { return Program.sel_activa;}
> > >                 set { Program.sel_activa =value; }        }
>
> > >        static void Main()
> > >        {  Program.sel_activa = new Seleccion();
> > >            Application.EnableVisualStyles();
> > >            Application.SetCompatibleTextRenderingDefault(false);
> > >            Application.Run(new Form1());        }
> > > } }
>
> > > and after doing this I try to access the property from one form like
> > > this:
>
> > > Program._sel_activa.x = B02.Checked;    (B02 is a check box)
>
> > > and VS throw me an exception:
> > > "Cannot modify the return value of
> > > 'WindowsApplication1.Program._sel_activa' because it is not a
> > > variable"
>
> > > Can somebody help with this?
>
> > > By the way this is the link of the article I've taken from the
> > > information.
>
> > >http://bytes.com/groups/net-c/263680-accessing-same-data-multiple-for...
>
> > > Thanks in advance!

Reply via email to