This question already has answers here:
Nullable Object must have a value #2
(6 answers)
Closed 9 years ago.
I am trying to assign 'null' value to Datetime? object. But it is throwing "nullable value must have a value" exception.
//item.PlannedStartDate value is nothing.
fact_Initiative.Start_Date = If([String].IsNullOrEmpty(item.PlannedStartDate),
DBNull.Value, CType(Convert.ToDateTime(item.PlannedStartDate),
System.Nullable(Of Date)))
How to solve this
fact_Initiative.Start_Date =If(String.IsNullOrEmpty(item.PlannedStartDate), CType(Nothing, DateTime?), DateTime.Parse(item.PlannedStartDate))
Related
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Closed 2 years ago.
I am using sqlDatareader to read and assign variable value. The variable datatype is string.
string displayUnits = dr["DisplayUnits"].ToString(); // the variable value "DisplayUnitType.DUT_GENERAL"
Variable value is used to assign formatOptions.DisplayUnits.
formatOptions.DisplayUnits = displayUnits;
However, I am getting an error message cannot implicitly convert type string to Autodesk.Revit.BD.DisplayUnitType. I know I assigned a value to another type.
How do I casting and type conversions?
Indeed, you cannot implicitly convert a string to an enum.
You can explicitly convert a string to an enum in C#, as Chetan already pointed out.
This question already has answers here:
What is the purpose of a question mark after a value type (for example: int? myVariable)?
(9 answers)
What does the ? operator mean in C# after a type declaration?
(7 answers)
Closed 3 years ago.
I am reading this article and it has this example
int? length = people?.Length; // null if people is null
My question is if the first question mark after the int a typo ? If not what is the purpose of introducing a ? after an int. I understand why its introduced after people. The reason for that is if people is null then it will return a null. Can someone explain the purpose of the ?
This question already has answers here:
The left-hand side of an assignment must be a variable, property or indexer
(2 answers)
C# Left-Hand Side Of An Assignment Must Be a Variable, Property or Indexer
(2 answers)
left hand side of an assignment must be a variable [closed]
(6 answers)
Closed 5 years ago.
I am trying to assign value of on datatable value to other datatable
strCustIssueNo=DtMstrCustIssue.Rows[i]["CustIssueNo"].ToString();
DtMstr.Rows[i]["CustIssueNo"].ToString()=strCustIssueNo;
OR
DtMstr.Rows[i]["CustIssueNo"].ToString()=DtMstrCustIssue.Rows[i]["CustIssueNo"].ToString();
but i getting an error "Left Hand Side of assignment must be a variable,property or indexer "
Try this:
DtMstr.Rows[i]["CustIssueNo"] = DtMstr.Rows[i]["CustIssueNo"].ToString();
or
DtMstr.Rows[i]["CustIssueNo"] = strCustIssueNo;
Just remove the ToString on the left side.
This question already has answers here:
Variable is assigned but its value is never used (C#)
(4 answers)
Closed 5 years ago.
int price;
if (listBox1.Text == "Regular McYum")
{
price = 70;
}
How come the 'price' variable is assigned but never used?
The warning is just clear: assigning a variable and using it are different things. The first is you set a value to it, the second ones means you´re doing something with that value.
So in your case you should pass your variable to a method for example:
Console.WriteLine(price);
This should be a warning from your compiler, so you can - although you shouldn´t - ignore this.
If listBox1.Text does not equal "Regular McYum", then price will never get set. You need to ensure that price will receive a value on all of the possible code paths.
This question already has answers here:
Can I add an enum to an existing .NET Structure, like Date?
(3 answers)
Closed 8 years ago.
I have one question
How can I specify new value into system enum?
For example, the LoginFailureAction has a enum that define with microsoft in c# how can I get new value into this enum?
Of course, you can't change the default value of a type. The type is the type, and you can't change it.