/[OhmsLawCalc]/trunk/libOhmsLawCalc/OhmsLawValue.cs
ViewVC logotype

Contents of /trunk/libOhmsLawCalc/OhmsLawValue.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations) (download)
Wed Jan 29 04:16:58 2014 UTC (5 years, 11 months ago) by william
File size: 5284 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace libOhmsLawCalc
7 {
8 public interface IOhmsLawValue
9 {
10 OhmsLawValueType ValueType { get; }
11 OhmsLawUnitType UnitType { get; }
12 float Value { get; }
13 OhmsLawValue DefaultValue { get; }
14 string ToString();
15 }
16 public class OhmsLawValue : IOhmsLawValue
17 {
18 //public OhmsLawValue() : this(OhmsLawValueType.None,OhmsLawUnitType.Default,1.0f) { }
19 public OhmsLawValue(OhmsLawValueType valuetype, OhmsLawUnitType unittype, float value)
20 {
21 this.ValueType = valuetype;
22 this.UnitType = unittype;
23 this.Value = value;
24 }
25 #region IOhmsLawValue members
26 public OhmsLawValueType ValueType { get; private set; }
27 public OhmsLawUnitType UnitType { get; private set; }
28 public float Value { get; private set; }
29 public OhmsLawValue DefaultValue { get { return ConvertToDefaultValue(); } }
30
31 private OhmsLawValue ConvertToDefaultValue()
32 {
33 OhmsLawValue olv = new OhmsLawValue(this.ValueType, this.UnitType, this.Value);
34 float v = 0f;
35 switch (this.UnitType)
36 {
37 case OhmsLawUnitType.Pico: // 1 000 000 000 000 Watts
38 v = this.Value / 1000000000000f;
39 break;
40 case OhmsLawUnitType.Nano: // 1 000 000 000 Watts
41 v = this.Value / 1000000000f;
42 break;
43 case OhmsLawUnitType.Micro: // 1 000 000 Watts
44 v = this.Value / 1000000f;
45 break;
46 case OhmsLawUnitType.Milli: // 1 000 milliWatts
47 v = this.Value / 1000f;
48 break;
49 case OhmsLawUnitType.Default: // 1 Watts
50 // we're already in the default unit
51 v = this.Value;
52 break;
53 case OhmsLawUnitType.Kilo: // 0.001 kilo Watts
54 v = this.Value * 1000f;
55 break;
56 case OhmsLawUnitType.Mega: // 0.000001 megaWatts
57 v = this.Value * 1000000f;
58 break;
59 case OhmsLawUnitType.Giga: // 0.000 000 001 gigaWatts
60 v = this.Value * 1000000000f;
61 break;
62 case OhmsLawUnitType.Tera: // 0.000 000 000 001 gigaWatts
63 v = this.Value * 1000000000000f;
64 break;
65 case OhmsLawUnitType.None:
66 throw new InvalidCastException("A UnitType of None is invalid for conversion");
67 default:
68 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawUnitType", (uint)this.UnitType));
69 }
70 olv = new OhmsLawValue(olv.ValueType, OhmsLawUnitType.Default, v);
71 return olv;
72 }
73
74 private string CreateFormattedValue()
75 {
76 string unit_char = "";
77 string value_char = "";
78 switch (this.ValueType)
79 {
80 case OhmsLawValueType.Power: value_char = "Watts"; break;
81 case OhmsLawValueType.Current: value_char = "Amps"; break;
82 case OhmsLawValueType.Resistance: value_char = "Ohms"; break;
83 case OhmsLawValueType.Voltage: value_char = "Volts"; break;
84 case OhmsLawValueType.None: throw new InvalidCastException("A ValueType of None is invalid for conversion");
85 default:
86 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawValueType", (uint)this.ValueType));
87 }
88
89 switch (this.UnitType)
90 {
91 case OhmsLawUnitType.Default: unit_char = ""; break;
92 case OhmsLawUnitType.Giga: unit_char = "Giga"; break;
93 case OhmsLawUnitType.Mega: unit_char = "Mega"; break;
94 case OhmsLawUnitType.Micro: unit_char = "Micro"; break;
95 case OhmsLawUnitType.Milli: unit_char = "Milli"; break;
96 case OhmsLawUnitType.Nano: unit_char = "Nano"; break;
97 case OhmsLawUnitType.Pico: unit_char = "Pico"; break;
98 case OhmsLawUnitType.Kilo: unit_char = "Kilo"; break;
99 case OhmsLawUnitType.Tera: unit_char = "Tera"; break;
100 case OhmsLawUnitType.None: throw new InvalidCastException("A UnitType of None is invalid for conversion");
101 default:
102 throw new InvalidOperationException(string.Format("Value: 0x{0:x8} is not defined in OhmsLawUnitType", (uint)this.UnitType));
103 }
104
105 string f_value = string.Format("{0} {1}{2}", this.Value, unit_char == "" ? "":string.Format("{0} ",unit_char), value_char);
106 return f_value;
107 }
108
109 public override string ToString()
110 {
111 string value = this.DefaultValue.CreateFormattedValue();
112 return value;
113 }
114 #endregion
115 }
116 }

  ViewVC Help
Powered by ViewVC 1.1.22