This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
When using the following method there are use cases where one of the parameters, for this example RC_2 (datetime) may pass a null value. I need to have that null value converted into either a blank value or a string value so that it can be passed to a stringbuilder below is what I'm currently using,
public static string CHG_FixDatetime(string RC_1, string RC_2, string seperator)
{
var sb = new StringBuilder();
sb.Append(RC_1);
sb.Append(RC_2);
sb.Append(seperator);
DateTime dt;
if (!string.IsNullorWhiteSpace(RC_2))
sb.append(RC_2)
else
{
sb.Append(Convert.ToString(RC_2));
}
return sb.ToString();
}
I've tried multiple configurations and variations, but I'm unable to get past the System.NullException that is thrown when the RC_2 value is Null. Any help is appreciated.
Try the following solution
RC_2 = String.IsNullOrWhiteSpace(RC_2) ? "" : RC_2;
Related
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance
value = value.Replace( ", ", string.Empty);
strings in .net are immutable.
Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
I have an application that updates values in documents, however, some of these documents have multiple entries of this value. due to this I have created a Do Something loop but this is just looping and is not replacing the values.
my code is as below:
do
{
int dollarIndex = script.IndexOf("$");
string nextTenChars = script.Substring(dollarIndex - 17, 17);
string promptValue = CreateInput.ShowDialog(nextTenChars, "Input");
script.Replace("$", promptValue);
}
while (script.Contains("$"));
Strings are immutable, so you need to do:
script = script.Replace("$", promptValue);
Simply doing
script.Replace("$", promptValue);
Doesn't update the value of script
This question already has answers here:
LinqToXml does not handle nillable elements as expected
(3 answers)
Closed 6 years ago.
The XElement has explicit support for casting to Nullable<int> but it's not working as I expected. The following unit test demonstrates the problem:
[TestMethod]
public void CastingNullableInt()
{
var xdoc = XDocument.Parse("<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><okay>123</okay><boom xsi:nil=\"true\"/></root>");
Assert.AreEqual(123, (int?)xdoc.Root.Element("okay"));
Assert.IsNull((int?)xdoc.Root.Element("boom"));
}
The test should pass the last assert. Instead it gives an FormatException:
Input string was not in a correct format.
Why doesn't it parse to null correctly here?
Linq to XML is not schema aware so it will not convert xsi:nil = "true" to a nullable variable. To test this you would need to do something like:
Assert.IsTrue((bool?)xdoc.Root.Element("boom").Attribute("{http://www.w3.org/2001/XMLSchema-instance}nil") == true);
XElement does not parse <boom xsi:nil=\"true\"/> correctly. It only works if you omit <boom xsi:nil=\"true\"/>, then the value is null and it returns (int?)null.
A workaround could be to first check on the value not being empty:
!string.IsNullOrEmpty((string)xdoc.Root.Element("boom"))
? (int?)xdoc.Root.Element("boom")
: null
;
You can check IsEmpty property:
var value = xdoc.Root.Element("boom").IsEmpty ? null : (int?)xdoc.Root.Element("boom");
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
I tried convert to string value of cell in a datagridview:
string t = row.Cells[0].Value.ToString()== null ? String.Empty : row.Cells[0].Value.ToString();
MessageBox.Show(t);
MessageBox shows correctly value but application gives exception:
Object reference not set to an instance of an object.
Value property could be null; Try this
string t = row.Cells[0].Value == null ? String.Empty : row.Cells[0].Value.ToString();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why isn’t String.Empty a constant?
To make my code more readable I tried to assign String.Empty to a constant value:
const string PLATYPUS_ADDED_AND_ACCEPTED = string.Empty;
if (false) { }
else
{
toolTip = PLATYPUS_ADDED_AND_ACCEPTED;
}
but I get "the expression being added must be constant"
Isn't String.Empty always the same thing? That seems pretty constant to me.
string.Empty is a readonly field, not a constant.
The compiler has no way to know this will always be the same value.