2
public struct Speakers 
{
    //...

    public bool BackCenter { get; set; }
    public bool BackLeft { get; set; }
    public bool BackRight { get; set; }
    public bool FrontCenter { get; set; }
    public bool FrontLeft { get; set; }
    public bool FrontLeftOfCenter { get; set; }
    public bool FrontRight { get; set; }
    public bool FrontRightOfCenter { get; set; }
    public bool SideLeft { get; set; }
    public bool SideRight { get; set; }
    public bool Subwoofer { get; set; }
    public bool TopBackCenter { get; set; }
    public bool TopBackLeft { get; set; }
    public bool TopBackRight { get; set; }
    public bool TopCenter { get; set; }
    public bool TopFrontCenter { get; set; }
    public bool TopFrontLeft { get; set; }
    public bool TopFrontRight { get; set; }

}

How can I easily loop through all of these and set them to false?

bulltorious
  • 7,769
  • 4
  • 49
  • 78
  • 6
    Don't know it that helps you, but by default they are false. – Piotr Perak Nov 21 '11 at 21:26
  • This is from a 3rd party library, so unfortunately I am forced to used this implementation :/ – bulltorious Nov 21 '11 at 21:34
  • 1
    Instead of using reflection as some will suggest, just make a new method to set them all to false and bite the bullet. You specifically ask for a loop, so this isn't an answer... just a suggestion. – SQLMason Nov 21 '11 at 21:37
  • 4
    **Do not do this in the first place**. Structs should be *small* and *logically immutable values.* The data structure you are describing is large and mutable; it should be a class. Why on earth are you making this thing a struct? – Eric Lippert Nov 21 '11 at 23:36
  • Again. This is a 3rd party library I am using, so some of these design decisions are made without me. – bulltorious Nov 27 '11 at 19:53

6 Answers6

5

Setting all to false is relatively easy, since default(T) or new T() has all of them as false. So you just need to assign the default value to the variable you're interested in.

speakers=default(Speakers);

If you need a non default value, you can use reflection, but it's a bit ugly, since boxing will implicitly copy your value. To set all values to true you can:

Speakers speakers = new Speakers();
object boxedSpeakers = speakers;
foreach(PropertyInfo p in sp.GetType().GetProperties())
    p.SetValue(boxedSpeakers, true, null);
speakers = (Speakers)boxedSpeakers;

You could also consider making a cleaner wrapper over that third party library thus isolating your code from this rather ugly stuct.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
3

Are you sure the modeling / representation is correct? What you have doesn't look quite right.

Consider this:

class Speaker
{
    // not really relevant here
    public SpeakerPosition Position {get; set;}  //enum        

    public void PowerOff() { ... }
}

Then you use it like so:

class SpeakerSystem
{
    Speaker[] _speakers = ...;

    public void PowerOff()
    {
        foreach(var speaker in _speakers)
           speaker.PowerOff();
    }
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
2

By default booleans are false.

You could iterate over properties with reflection but that wouldn't be very fast, but maybe acceptable for you.

But maybe think about that. When you have object with some of the properties set to true and want to reset them to false just create new object.

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
1

You can use Type.GetFields() or Type.GetProperties(), using Reflection, to get the values, and SetValue to set the values.

But, why aren't you using a class for this? Are you absolutely sure you need a struct?

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
0

Your design will not work.

  • When the struct is created all values are set by default they are false.
  • After the struct is created the values cannot be changed

You have 2 options:

  • Change to a class
  • Create a new struct when you need to reset the values
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
0

With reflection:

object a = new Speakers(); // required boxing, otherwise you cannot change a struct
PropertyInfo[] info = a.GetType().GetProperties();
for (int i = 0; i < info.Length; i++)
{                
    info[i].SetValue(a, false, null);
}
Tudor
  • 61,523
  • 12
  • 102
  • 142