11namespace GenCode128
22{
3+ using System . Collections ;
34 using System . Text ;
45
56 /// <summary>
67 /// Represent the set of code values to be output into barcode form
78 /// </summary>
89 public class Code128Content
910 {
10- private readonly int [ ] codeList ;
11-
1211 /// <summary>
1312 /// Create content based on a string of ASCII data
1413 /// </summary>
1514 /// <param name="asciiData">the string that should be represented</param>
1615 public Code128Content ( string asciiData )
1716 {
18- this . codeList = this . StringToCode128 ( asciiData ) ;
17+ this . Codes = this . StringToCode128 ( asciiData ) ;
1918 }
2019
2120 /// <summary>
2221 /// Provides the Code128 code values representing the object's string
2322 /// </summary>
24- public int [ ] Codes
25- {
26- get
27- {
28- return this . codeList ;
29- }
30- }
23+ public int [ ] Codes { get ; }
3124
3225 /// <summary>
3326 /// Transform the string into integers representing the Code128 codes
@@ -38,35 +31,33 @@ public int[] Codes
3831 private int [ ] StringToCode128 ( string asciiData )
3932 {
4033 // turn the string into ascii byte data
41- byte [ ] asciiBytes = Encoding . ASCII . GetBytes ( asciiData ) ;
34+ var asciiBytes = Encoding . ASCII . GetBytes ( asciiData ) ;
4235
4336 // decide which codeset to start with
44- Code128Code . CodeSetAllowed csa1 = asciiBytes . Length > 0
45- ? Code128Code . CodesetAllowedForChar ( asciiBytes [ 0 ] )
46- : Code128Code . CodeSetAllowed . CodeAorB ;
47- Code128Code . CodeSetAllowed csa2 = asciiBytes . Length > 0
48- ? Code128Code . CodesetAllowedForChar ( asciiBytes [ 1 ] )
49- : Code128Code . CodeSetAllowed . CodeAorB ;
50- CodeSet currcs = this . GetBestStartSet ( csa1 , csa2 ) ;
37+ var csa1 = asciiBytes . Length > 0
38+ ? Code128Code . CodesetAllowedForChar ( asciiBytes [ 0 ] )
39+ : Code128Code . CodeSetAllowed . CodeAorB ;
40+ var csa2 = asciiBytes . Length > 0
41+ ? Code128Code . CodesetAllowedForChar ( asciiBytes [ 1 ] )
42+ : Code128Code . CodeSetAllowed . CodeAorB ;
43+ var currentCodeSet = this . GetBestStartSet ( csa1 , csa2 ) ;
5144
5245 // set up the beginning of the barcode
53- System . Collections . ArrayList codes = new System . Collections . ArrayList ( asciiBytes . Length + 3 ) ;
54-
5546 // assume no codeset changes, account for start, checksum, and stop
56- codes . Add ( Code128Code . StartCodeForCodeSet ( currcs ) ) ;
57-
47+ var codes = new ArrayList ( asciiBytes . Length + 3 ) { Code128Code . StartCodeForCodeSet ( currentCodeSet ) } ;
48+
5849 // add the codes for each character in the string
59- for ( int i = 0 ; i < asciiBytes . Length ; i ++ )
50+ for ( var i = 0 ; i < asciiBytes . Length ; i ++ )
6051 {
6152 int thischar = asciiBytes [ i ] ;
62- int nextchar = asciiBytes . Length > ( i + 1 ) ? asciiBytes [ i + 1 ] : - 1 ;
53+ var nextchar = asciiBytes . Length > i + 1 ? asciiBytes [ i + 1 ] : - 1 ;
6354
64- codes . AddRange ( Code128Code . CodesForChar ( thischar , nextchar , ref currcs ) ) ;
55+ codes . AddRange ( Code128Code . CodesForChar ( thischar , nextchar , ref currentCodeSet ) ) ;
6556 }
6657
6758 // calculate the check digit
68- int checksum = ( int ) codes [ 0 ] ;
69- for ( int i = 1 ; i < codes . Count ; i ++ )
59+ var checksum = ( int ) codes [ 0 ] ;
60+ for ( var i = 1 ; i < codes . Count ; i ++ )
7061 {
7162 checksum += i * ( int ) codes [ i ] ;
7263 }
@@ -75,7 +66,7 @@ private int[] StringToCode128(string asciiData)
7566
7667 codes . Add ( Code128Code . StopCode ( ) ) ;
7768
78- int [ ] result = codes . ToArray ( typeof ( int ) ) as int [ ] ;
69+ var result = codes . ToArray ( typeof ( int ) ) as int [ ] ;
7970 return result ;
8071 }
8172
@@ -88,14 +79,14 @@ private int[] StringToCode128(string asciiData)
8879 /// <returns>The codeset determined to be best to start with</returns>
8980 private CodeSet GetBestStartSet ( Code128Code . CodeSetAllowed csa1 , Code128Code . CodeSetAllowed csa2 )
9081 {
91- int vote = 0 ;
82+ var vote = 0 ;
9283
93- vote += ( csa1 == Code128Code . CodeSetAllowed . CodeA ) ? 1 : 0 ;
94- vote += ( csa1 == Code128Code . CodeSetAllowed . CodeB ) ? - 1 : 0 ;
95- vote += ( csa2 == Code128Code . CodeSetAllowed . CodeA ) ? 1 : 0 ;
96- vote += ( csa2 == Code128Code . CodeSetAllowed . CodeB ) ? - 1 : 0 ;
84+ vote += csa1 == Code128Code . CodeSetAllowed . CodeA ? 1 : 0 ;
85+ vote += csa1 == Code128Code . CodeSetAllowed . CodeB ? - 1 : 0 ;
86+ vote += csa2 == Code128Code . CodeSetAllowed . CodeA ? 1 : 0 ;
87+ vote += csa2 == Code128Code . CodeSetAllowed . CodeB ? - 1 : 0 ;
9788
98- return ( vote > 0 ) ? CodeSet . CodeA : CodeSet . CodeB ; // ties go to codeB due to my own prejudices
89+ return vote > 0 ? CodeSet . CodeA : CodeSet . CodeB ; // ties go to codeB due to my own prejudices
9990 }
10091 }
10192}
0 commit comments