Evaluate Enum

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi, im getting the powerstate of the battery
usingSystemInformation.PowerState like so:

PowerStatus power = SystemInformation.PowerStatus;

if (power.BatteryChargeStatus == BatteryChargeStatus.Charging)
//code here

but the problem is, the enum is BatteryChargeState shows this during debug

power.BatteryChargeStatus High | Charging

how can i write an if statement for that? ive tried

if (power.BatteryChargeStatus == BatteryChargeStatus.High |
BatteryChargeStatus.Charging)

but that didnt work....any help would be great! thanks
 
iwdu15 said:
hi, im getting the powerstate of the battery
usingSystemInformation.PowerState like so:

PowerStatus power = SystemInformation.PowerStatus;

if (power.BatteryChargeStatus == BatteryChargeStatus.Charging)
//code here

but the problem is, the enum is BatteryChargeState shows this during debug

power.BatteryChargeStatus High | Charging

how can i write an if statement for that? ive tried

if (power.BatteryChargeStatus == BatteryChargeStatus.High |
BatteryChargeStatus.Charging)

but that didnt work....any help would be great! thanks

if ((power.BatteryChargeStatus & Charging) != 0)
 
Göran Andersson said:
if ((power.BatteryChargeStatus & Charging) != 0)

maybe even better:

if ((power.BatteryChargeStatus & BatteryChargeStatus.Charging) ==
BatteryChargeStatus.Charging)
 
Ben said:
maybe even better:

if ((power.BatteryChargeStatus & BatteryChargeStatus.Charging) ==
BatteryChargeStatus.Charging)

Yes, that's useful if you want to match a composite enum value exactly.
For example:

[Flags]
enum Value { Some = 1, Other = 2, Any = 3}

if ((x & Value.Any) != ) {
MessageBox.Show("some, other or both");
}
if ((x & Value.Any) == Value.Any) {
MesssageBox.Show("both some and other");
}

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top