@@ -19,8 +19,8 @@ private class PasswordHelper
19
19
{
20
20
private readonly PasswordBox _passwordBox ;
21
21
private string _currentText ;
22
- private string _newPassword ;
23
- private string _currentPassword ;
22
+ private string ? _newPassword ;
23
+ private string ? _currentPassword ;
24
24
25
25
/// <summary>
26
26
/// Initializes a new instance of the <see cref="PasswordHelper"/> class.
@@ -44,7 +44,7 @@ public PasswordHelper(PasswordBox passwordBox)
44
44
/// 2. When password is revealed (plain text mode)
45
45
/// 3. When password is hidden (masked character mode)
46
46
/// </remarks>
47
- public string GetNewPassword ( )
47
+ public string ? GetNewPassword ( )
48
48
{
49
49
_currentPassword = _passwordBox . Password ;
50
50
_newPassword = _currentPassword ;
@@ -53,8 +53,8 @@ public string GetNewPassword()
53
53
54
54
if ( IsDeletingText ( ) )
55
55
{
56
- int charsToRemove = _currentPassword . Length - _currentText . Length ;
57
- _newPassword = _currentPassword . Remove ( selectionIndex , charsToRemove ) ;
56
+ int charsToRemove = ( _currentPassword ? . Length ?? 0 ) - _currentText . Length ;
57
+ _newPassword = _currentPassword ? . Remove ( selectionIndex , charsToRemove ) ;
58
58
return _newPassword ;
59
59
}
60
60
@@ -78,26 +78,26 @@ public string GetNewPassword()
78
78
/// 2. Character was replaced (overwrite)
79
79
/// 3. Characters were removed
80
80
/// </remarks>
81
- private string HandleHiddenModeChanges ( int selectionIndex )
81
+ private string ? HandleHiddenModeChanges ( int selectionIndex )
82
82
{
83
83
char passwordChar = _passwordBox . PasswordChar ;
84
- int currentLength = _currentPassword . Length ;
84
+ int currentLength = _currentPassword ? . Length ?? 0 ;
85
85
86
86
if ( _currentText . Length > currentLength )
87
87
{
88
88
// Characters were inserted
89
89
int insertedCount = _currentText . Length - currentLength ;
90
90
string insertedText = _currentText . Substring ( selectionIndex - insertedCount , insertedCount ) ;
91
- _newPassword = _currentPassword . Insert ( selectionIndex - insertedCount , insertedText ) ;
91
+ _newPassword = _currentPassword ? . Insert ( selectionIndex - insertedCount , insertedText ) ;
92
92
}
93
93
else if ( _currentText . Length == currentLength )
94
94
{
95
95
// Character was replaced (overwrite)
96
96
for ( int i = 0 ; i < _currentText . Length ; i ++ )
97
97
{
98
- if ( _currentText [ i ] != passwordChar && i < _newPassword . Length )
98
+ if ( _currentText [ i ] != passwordChar && i < ( _newPassword ? . Length ?? 0 ) )
99
99
{
100
- _newPassword = _newPassword . Remove ( i , 1 ) . Insert ( i , _currentText [ i ] . ToString ( ) ) ;
100
+ _newPassword = _newPassword ? . Remove ( i , 1 ) . Insert ( i , _currentText [ i ] . ToString ( ) ) ;
101
101
break ;
102
102
}
103
103
}
@@ -106,7 +106,7 @@ private string HandleHiddenModeChanges(int selectionIndex)
106
106
{
107
107
// Characters were removed (fallback)
108
108
int removedCount = currentLength - _currentText . Length ;
109
- _newPassword = _currentPassword . Remove ( selectionIndex , removedCount ) ;
109
+ _newPassword = _currentPassword ? . Remove ( selectionIndex , removedCount ) ;
110
110
}
111
111
112
112
return _newPassword ;
@@ -121,7 +121,7 @@ private bool IsDeletingText()
121
121
Debug . Assert ( _currentText == _passwordBox . Text , "Text mismatch" ) ;
122
122
Debug . Assert ( _currentPassword == _passwordBox . Password , "Password mismatch" ) ;
123
123
124
- return _currentText . Length < _currentPassword . Length ;
124
+ return _currentText . Length < ( _currentPassword ? . Length ?? 0 ) ;
125
125
}
126
126
}
127
127
}
0 commit comments