1
1
namespace GenCode128
2
2
{
3
+ using System . Collections ;
3
4
using System . Text ;
4
5
5
6
/// <summary>
6
7
/// Represent the set of code values to be output into barcode form
7
8
/// </summary>
8
9
public class Code128Content
9
10
{
10
- private readonly int [ ] codeList ;
11
-
12
11
/// <summary>
13
12
/// Create content based on a string of ASCII data
14
13
/// </summary>
15
14
/// <param name="asciiData">the string that should be represented</param>
16
15
public Code128Content ( string asciiData )
17
16
{
18
- this . codeList = this . StringToCode128 ( asciiData ) ;
17
+ this . Codes = this . StringToCode128 ( asciiData ) ;
19
18
}
20
19
21
20
/// <summary>
22
21
/// Provides the Code128 code values representing the object's string
23
22
/// </summary>
24
- public int [ ] Codes
25
- {
26
- get
27
- {
28
- return this . codeList ;
29
- }
30
- }
23
+ public int [ ] Codes { get ; }
31
24
32
25
/// <summary>
33
26
/// Transform the string into integers representing the Code128 codes
@@ -38,35 +31,33 @@ public int[] Codes
38
31
private int [ ] StringToCode128 ( string asciiData )
39
32
{
40
33
// turn the string into ascii byte data
41
- byte [ ] asciiBytes = Encoding . ASCII . GetBytes ( asciiData ) ;
34
+ var asciiBytes = Encoding . ASCII . GetBytes ( asciiData ) ;
42
35
43
36
// 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 ) ;
51
44
52
45
// set up the beginning of the barcode
53
- System . Collections . ArrayList codes = new System . Collections . ArrayList ( asciiBytes . Length + 3 ) ;
54
-
55
46
// 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
+
58
49
// 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 ++ )
60
51
{
61
52
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 ;
63
54
64
- codes . AddRange ( Code128Code . CodesForChar ( thischar , nextchar , ref currcs ) ) ;
55
+ codes . AddRange ( Code128Code . CodesForChar ( thischar , nextchar , ref currentCodeSet ) ) ;
65
56
}
66
57
67
58
// 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 ++ )
70
61
{
71
62
checksum += i * ( int ) codes [ i ] ;
72
63
}
@@ -75,7 +66,7 @@ private int[] StringToCode128(string asciiData)
75
66
76
67
codes . Add ( Code128Code . StopCode ( ) ) ;
77
68
78
- int [ ] result = codes . ToArray ( typeof ( int ) ) as int [ ] ;
69
+ var result = codes . ToArray ( typeof ( int ) ) as int [ ] ;
79
70
return result ;
80
71
}
81
72
@@ -88,14 +79,14 @@ private int[] StringToCode128(string asciiData)
88
79
/// <returns>The codeset determined to be best to start with</returns>
89
80
private CodeSet GetBestStartSet ( Code128Code . CodeSetAllowed csa1 , Code128Code . CodeSetAllowed csa2 )
90
81
{
91
- int vote = 0 ;
82
+ var vote = 0 ;
92
83
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 ;
97
88
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
99
90
}
100
91
}
101
92
}
0 commit comments