Differentiating between cases where user enters '0' or leaves TextBox empty - c#

Well the question title may not be self explanatory, so let me go ahead and elaborate.
Consider, a TextBox that accepts only numeric value or is left empty. The value(text) entered is stored in an integer(int32) variable. The problem arises when the user enters the digit 0 or leaves the TextBox empty, as the conversion from string to int, converts an empty string to "0" as well.
So my question stands: How do I differentiate the 2 scenarios?
EDIT I figured a lot of questions may be answered by the code and exact problem(as I see it)
if (txtOtherId.Text == string.Empty)
{
otherId = Convert.ToInt32(null);
}
else
{
otherId = Convert.ToInt32(txtOtherId.Text);
}

How about an extension method?
public static class Extensions
{
public static bool TryGetInt(this TextBox tb, out int value)
{
int i;
bool parsed = int.TryParse(tb.Text, out i);
value = i;
return parsed;
}
}
Usage:
int i;
if (textBox1.TryGetInt(out i))
{
MessageBox.Show(i.ToString());
}
else
{
// no integer entered
}

What have you tried? Can we see your code?
Now, I tried the following:
int i;
i = Convert.ToInt32(""); // throws, doesn't give zero
i = int.Parse(""); // throws, doesn't give zero
bool couldParse = int.TryParse("", out i); // makes i=0 but signals that the parse failed
So I can't reproduce. However, if I use null instead of "", the Convert.ToInt32 does convert into zero (0). However, Parse and TryParse still fail with null.
UPDATE:
Now that I see your code. Consider changing the type of otherId from int to int? where the question mark makes it a nullable type. Then:
if (txtOtherId.Text == "")
{
otherId = null; // that's null of type int?
}
else
{
otherId = Convert.ToInt32(txtOtherId.Text); // will throw if Text is (empty again or) invalid
}
If you want to be sure no exceptions can happen, do this:
int tmp; // temporary variable
if (int.TryParse(txtOtherId.Text, out tmp))
otherId = tmp;
else
otherId = null; // that's null of type int?; happens for all invalid input

You could use a nullable int, and then have blank string be null.
int? myValue = String.IsNullOrEmpty(myTextbox.Text)
? (int?)null
: int.Parse(myTextbox.Text);
For clarity, the above is equivalent to
int? myValue = null;
if(!String.IsNullOrEmpty(myTextbox.Text))
{
myValue = int.Parse(myTextbox.Text);
}

Assuming that it is indeed a textbox...
string result = myTextBox.Text;
if (string.IsNullOrEmpty(result))
// This is an empty textbox
else
// It has a number in it.
int i = int.Parse(result);

There're 2 simple approaches how to do it:
string inputText="";
int? i=null;
if (!string.IsNullOrWhiteSpace(inputText))
i = int.Parse(inputText);
int i2;
bool ok = int.TryParse(inputText, out i2);

Related

Use null-conditional operator to set value to 0 if null

I'm new to C# but not to programming in general.
I am trying to set add some error checking to my program. There are 3 textboxes and I am trying to make it so that if the text box is left blank, it assumes a value of 0. Here is my code so far:
private void btnCalculate_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtNumberOfClassATix.Text)) // Assumes 0 if no number entered for txtNumberOfClassATix.Text.
{
txtNumberOfClassATix.Text = "0";
}
if (String.IsNullOrEmpty(txtNumberOfClassBTix.Text)) // Assumes 0 if no number entered for txtNumberOfClassBTix.Text.
{
txtNumberOfClassBTix.Text = "0";
}
if (String.IsNullOrEmpty(txtNumberOfClassCTix.Text)) // Assumes 0 if no number entered for txtNumberOfClassCTix.Text.
{
txtNumberOfClassCTix.Text = "0";
}
int classANum = int.Parse(txtNumberOfClassATix.Text);
int classBNum = int.Parse(txtNumberOfClassBTix.Text);
int classCNum = int.Parse(txtNumberOfClassCTix.Text);
double classATotal = classANum * classAPrice;
double classBTotal = classBNum * classBPrice;
double classCTotal = classCNum * classCPrice;
lblCalculatedClassARevenue.Text = $"{classATotal:c}";
lblCalculatedClassBRevenue.Text = $"{classBTotal:c}";
lblCalculatedClassCRevenue.Text = $"{classCTotal:c}";
lblCalculatedTotalRevenue.Text = $"{(classATotal + classBTotal) + classCTotal:c}";
}
This code works but I'm sure I could replace those if statements with something simpler. I've seen how to set a variable to null if another is null using the null-conditional operator but I don't really grasp it enough to adapt it to my scenario.
So far maccettura's answer is the best, but can we do better? Sure we can. Let's make a general-purpose extension method:
internal static class Extensions
{
public static int? AsInt(this string s)
{
int result;
if (s == null)
return null;
else if (int.TryParse(s, out result))
return result;
else
return null;
}
}
And now:
int classANum = txtNumberOfClassATix.Text.AsInt() ?? 0;
If it's an int, you get the int. If it's not, you get zero. Easy peasy.
Or, you might want this extension method:
internal static class Extensions
{
public static int AsInt(this string s, int default = 0)
{
int result;
if (s == null)
return default;
else if (int.TryParse(s, out result))
return result;
else
return default;
}
}
And now you can say what you want the default to be without using ??.
This style of programming is called "fluent programming"; it can make code that is very easy to read and understand.
Notice that this solution does not update the UI with zeros; if you wanted to do that then I would recommend splitting that into two steps: one which causes the mutation, and then a separate step which computes the value. Operations which are useful for both their effects and their values can be confusing.
This is a perfect time to use a method so you arent repeating yourself:
private static int GetInputAsInt(TextBox textbox)
{
int outputValue = 0;
if(textbox?.Text != null && int.TryParse(textbox.Text, out outputValue))
{
return outputValue;
}
return 0;
}
Now you are checking if the textbox itself is not null, and that the value contained therein is a int, if anything fails it returns a 0;
Call it in your other method like this:
int classANum = GetInputAsInt(txtNumberOfClassATix);
Which means your button click event would be a bit simpler:
private void btnCalculate_Click(object sender, EventArgs e)
{
int classANum = GetInputAsInt(txtNumberOfClassATix);
int classBNum = GetInputAsInt(txtNumberOfClassBTix);
int classCNum = GetInputAsInt(txtNumberOfClassCTix);
double classATotal = classANum * classAPrice;
double classBTotal = classBNum * classBPrice;
double classCTotal = classCNum * classCPrice;
lblCalculatedClassARevenue.Text = $"{classATotal:c}";
lblCalculatedClassBRevenue.Text = $"{classBTotal:c}";
lblCalculatedClassCRevenue.Text = $"{classCTotal:c}";
lblCalculatedTotalRevenue.Text = $"{(classATotal + classBTotal) + classCTotal:c}";
}
To keep it simple, a good approach is to use the conditional operator. The full example is below (broken across two lines for readability):
txtNumberOfClassATix.Text =
String.IsNullOrEmpty(txtNumberOfClassATix.Text) ? "0" : txtNumberOfClassATix.Text;
This is a nice, readable, assignment for the first part:
myString = ...
The conditional operator breaks down by providing a boolean expression (true/ false) on the left side of the ?. So, for example:
myString = anotherString == "" ? ... // checking if another string is empty
The final part is the :. To the left is the assignment if the expression is true, and to the right goes the assignment if the expression is false. To finish the example:
myString = anotherString == "" ? "anotherString is empty" : "anotherString is not empty";
The above example can be written out in full to clear up any misunderstanding as:
if (anotherString == "")
{
myString = "anotherString is empty";
}
else
{
myString = "anotherString is not empty";
}
This can apply to all the statements. The documentation is found here.
The best way to reduce the line of code is use the function for your common operation(s). In your case, you can create function which checks whether or not the object is NULL or empty. Based on the return value of that function you can proceed ahead. On the other hand, you can handle it on front-end by using different validators such as RequiredFieldValidator, CustomValidator, etc.

How can I test a decimal for null?

I've got this code intended to convert null decimal vals to a "zero" value:
decimal paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if ((null == paymentTot) || (paymentTot < 0))
{
paymentTot = 0M; // or simply 0?
}
private decimal TryConvertToDecimal(String incoming)
{
decimal result = 0.0M;
try
{
result = Convert.ToDecimal(incoming);
}
catch
{
; // nada
}
return result;
}
It compiles, but I get this warning: "The result of the expression is always 'false' since a value of type 'decimal' is never equal to 'null' of type 'decimal?'"
I don't grok just what it's trying to tell me. What sort of test do I need to appease the warning emitter and, more importantly, see to it that my code can equate to 'true' when that is the intent?
decimal is a value type and it can't be null.
If you want to have a nullable decimal then use Nullable<decimal> or decimal? which is a wrapper on decimal type, (it is still a value type though).
See: Nullable<T> Structure
You can also have your method for parsing as:
private Nullable<decimal> TryConvertToDecimal(String incoming)
{
Nullable<decimal> returnValue = null;
decimal result;
if (decimal.TryParse(incoming, out result))
{
returnValue = result;
}
return returnValue;
}
Also it is better to use decimal.TryParse if you are going to ignore the exception in parsing.
You don't Need your own Converter for this, use this instead:
decimal paymentTot;
if(!decimal.TryParse(boxPaymentAmount.Text, out paymentTot))
paymentTot = 0;
You possibly want to return a Nullable<decimal> (shorthand decimal?) from TryConvertToDecimal because decimal is a non-nullable value type.
private decimal? TryConvertToDecimal(String incoming)
{
try
{
return Convert.ToDecimal(incoming);
}
catch
{
return null;
}
}
var paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if (!paymentTot.HasValue || paymentTot.Value < 0)
{
paymentTot = 0;
}
Note however that by convention TryXXX functions return bool and use an out argument to return the parsed value upon success. One of these built in to the Framework is decimal.TryParse which you might want to use instead:
decimal paymentTot;
if(!decimal.TryParse(boxPaymentAmount.Text, out paymentTot) || paymentTot < 0)
paymentTot = 0;
will default to 0 if the parsing fails or if the parsed value is < 0, as specified in your question. I would suggest this is most likely the solution you are looking for.

Converting to Int in C# leaves the Value as 0

I'm trying convert decimal? to int and storing the result in "DayOffset". But because of some reason the value of "DayOffset" is getting set to 0 when I run my code. A value is passed in numberRangeHigh as 4
This is what my code looks like:
int DayOffset:
try
{
parseSuccess = int.TryParse(numberRangeHigh.ToString(), out DayOffset);
}
catch (Exception ex)
{
_foundationService.LogBusinessError(null, new ParameterBuilder(), ex.Message.Replace(" ", "_"));
return false;
}
Why are you converting to string at all? To convert decimal? to int, you should just use a cast:
int dayOffset = 0;
if (numberRangeHigh != null)
dayOffset = (int)numberRangeHigh.Value;
The code above will truncate the decimal, so 4.7 would become 4. If you want to round, use Convert.ToInt32 instead:
dayOffset = Convert.ToInt32(numberRangeHigh.Value);
As a side note, the correct way to use TryParse is this:
int DayOffset:
if (!int.TryParse(numberRangeHigh.ToString(), out DayOffset))
{
// Handle error...
return false;
}
Assuming numberRangeHigh is of type Decimal, try this:
int DayOffset = Decimal.ToInt32(numberRangeHigh);
For any nullable struct (you mentioned it was a decimal?), it's often a good idea to first check the .HasValue property (in case it's null). You could do something like this:
int dayOffset = (numberRangeHigh.HasValue) ? Convert.ToInt32(numberRangeHigh.Value) : 0;

Whoa, what the TryParse

I've got a Session that contains particular integer values, which are indexed with given controls. Normally, the following would work just fine:
int value;
int.TryParse(Session["Key"].ToString(), out value);
However, I do need to account for null. Where, if the string fails the default out would return a null. Except I noticed that int.TryParse doesn't work with:
int? value = null;
int.TryParse(Session["Key"].ToString(), out value);
So how can you try that parse, if fails it results in the null?
I found this question and the Microsoft Developer Network dictates:
When this method returns, contains the signed integer value
equivalent of the number contained in s, if the conversion succeeded,
or zero if the conversion failed. The conversion fails if the string
parameter is null or String.Empty, is not of the correct format, or
represents a number less than Min Value or greater than Max Value. This
parameter is passed uninitialized.
Which plainly states, if int.TryParse fails the integer will hold a value of zero. In the instance of my usage, zero could be a valid value. So I need null, any thoughts?
Sure; utilize the return value of int.TryParse (which returns if the conversion succeeded or not):
int? retValue = null;
int parsedValue = 0;
if (int.TryParse(Session["Key"].ToString(), out parsedValue))
retValue = parsedValue;
else
retValue = null;
return retValue;
A little verbose I'll admit, but you could wrap it in a function.
int tmp;
int? value = int.TryParse(Session["Key"].ToString(), out tmp) ? (int?)tmp : null;
The problem is the word "null." What does it mean? null could mean the value was indeterminable, an exception was thrown, simply that the value is null, or some other contextual meaning. Your question is a perfect example, because you, yourself, are arbitrarily stating that, in your opinion, null means the parsing of the string failed.
Microsoft's TryParse paradigm is great, but for limited usage. Consider these Scenarios:
string == "89"
string == null
string == "Hello World"
string == ""
string == "2147483650"
Yet, your only options are to assign an Integer or Null to your output, and to return true or false.
Assuming it worked, what are you going to do with that information? Something like this?
int? value = null;
if (int.TryParse(Session["Key"].ToString(), out value)) {
if (value == null)
// Handle "Appropriate" null
else
// Handle appropriate numeric value
}
else {
// Note: value == null here, and TryParse failed
// Handle null...
// What if the reason it failed was because the number was too big?
// What if the string was Empty and you wanted to do something special?
// What if the string was actually junk? Like "(423)322-9876" ?
// Long-Story Short: You don't know what to do here without more info.
}
Consider this NullableInt TryParse example:
public bool TryParseNullableInt(string input, out int? output)
{
int tempOutput;
output = null;
if (input == null) return true;
if (input == string.Empty) return true; // Would you rather this be 0?
if (!int.TryParse(input, out tempOutput))
return false; // What if string was "2147483650"... or "Twenty Three"?
output = tempOutput;
return true;
}
One solution is to use an enumeration TryParse instead of a boolean TryParse:
public ParseStatus TryParseNullableInt(string input, out int? output)
{
int tempInteger;
output = null;
if (input == null) return ParseStatus.Success;
if (input == string.Empty) { output = 0; return ParseStatus.Derived; }
if (!int.TryParse(input, out tempInteger)) {
if (ParseWords(input, out tempInteger)) { // "Twenty Three" = 23
output = tempInteger;
return ParseStatus.Derived;
}
long tempLong;
if (long.TryParse(input, out tempLong))
return ParseStatus.OutOfRange;
return ParseStatus.NotParsable;
}
output = tempInteger;
return ParseStatus.Success;
}
Another problem is the existence of the out variable. Your third option is to use a descriptive monad, something like this:
public Maybe<int?> TryParseNullableInt(string input)
{
if (input == null) return Maybe.Success(null);
if (input == string.Empty) { return Maybe.Derived(0); }
int tempInteger;
if (!int.TryParse(input, out tempInteger)) {
if (ParseWords(input, out tempInteger)) { // "Twenty Three" = 23
return Maybe.Derived(tempInteger);
}
long tempLong;
if (long.TryParse(input, out tempLong))
return Maybe.OutOfRange();
return Maybe.NotParsable();
}
return Maybe.Success(tempInteger);
}
You can use Monads as Single-Enumerable Values, or like so:
Maybe<int?> result = TryParseNullableInt("Hello");
if (result.HasValue) {
if (result.Status == ParseStatus.Success)
// Do something you want...
else if (result.Status == ParseStatus.Derived)
// Do something else... more carefully maybe?
}
else if (result.Status == ParseStatus.OutOfRange)
MessageUser("That number is too big or too small");
else if (result.Status == ParseStatus.NotParsable)
// Do something
With Monads, and possibly enumeration TryParses, you now have all the info you need from a descriptive return and nobody has to guess what null might mean.

InvalidCastException: Specified cast is not valid

This part of code is responsible to capture user input from keyboard and use it. When i press some button (ex. C) on keyboard variable TAG receives this as object (byte) value 3. i cannot find out why debugger returns the following error:
System.InvalidCastException. Specified cast is not valid.
num and tag declared as integer value. what is wrong? In this line int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag; - debugger points to .Tag at the end of line as error.
private void Question_KeyDown(object sender, KeyEventArgs e)
{
int? num = null;
this.qta.get_answer_number_by_key(new int?(e.KeyValue), ref num);
if (!num.HasValue)
{
this.SwitchQuestion(e.KeyValue);
}
else
{
num -= 1;
bool? nullable2 = false;
bool? end = false;
if (this.pnlAnswers.Controls.Count >= (num + 1))
{
Valid valid;
int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag;
this.qta.test_answer(this.q, tag, ref nullable2, ref end, ref this.pass);
this.e = end.Value;
if (nullable2.Value)
{
valid = new Valid(MessageType.Valid);
}
else
{
valid = new Valid(MessageType.Invalid);
}
valid.ShowDialog();
base.Close();
}
}
}
i`ve tried to change
int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag;
to
byte? tag = (byte?) this.pnlAnswers.Controls[num.Value].Tag;
and error gone, however i have issues with post-processing of receiving this values.
You need to cast the object referenced by Tag property to it's actual type which is byte. Then you can do further conversion against the byte object :
byte tagByte = (byte)this.pnlAnswers.Controls[num.Value].Tag);
int? tag = (int?) tagByte;
//or in short :
//int? tag = (byte)this.pnlAnswers.Controls[num.Value].Tag;
Simple test I did to confirm this behavior :
byte initialValue = 3;
object TAG = initialValue;
int? tagSuccess = (int?)((byte)TAG); //successfully convert TAG to type int?
int? tagFails = (int?)TAG; //throw InvalidCastException
The accepted answer does not always work.
When a Tag is set to 1 in the property editor of your Visual Studio forms designer, it will be assigned string type, that is "1". you can see that in the debugger when the exception occurs. There are quotes.
A string cannot be cast to any numeric type directly. In this case, (byte) will yield the same exception.
A general but dirty solution for tags of type string OR integer is this..
private int TagValue(object cTag)
{
int iTag = 0;
try { iTag = (int)cTag; } catch { iTag = int.Parse((string)cTag); }
return iTag;
}

Categories