I am trying to use the solution given here ->
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?
But I am working with an int.
Thanks
int? myNullableInt = null;
myIntParameter.Value = myNullableInt ?? (object)DBNull.Value;
Substitute int? for DateTime? then...
Related
I tried calling this code:
var test = int.parse(DateFormat('yyyy').format(formattedTemporaryDate));
but I get the error:
type 'String' is not a subtype of type 'DateTime'
my formattedTemporaryDate is '2022-01-08'
I think I need to transform the formattedTemporaryDate to an int, but I don't know how. And I even don't know if this really is the problem... Do you need more information to fix my problem?
Just use DateTime.parse('2022-01-08') instead of just String.
Something like var formattedTemporaryDate = DateTime.parse('2022-01-08');
What would you like var test to be at the end? A datetime? An int representation of the date?
You can convert your string to a DateTime object by calling DateTime.parse()
var formattedTemporaryDate = '2022-01-08';
DateTime dt = DateTime.parse(formattedTemporaryDate);
print(dt); // 2022-01-08 00:00:00.000
If you then want to convert it to an Int (maybe because you're doing UNIX Timestamp stuff?) You could try reverse-engineering some of the code people added here going the other direction.
For example considering the pseudocode
getResult(){
List lst=[1,"5",4];
String str="five";
if(condition){
return lst;
}
else{
return str;
}
}
can something like this be achieved in flutter.
Yes, it's possible to do something like this in dart/flutter. By the way, your pseudocode will work if you declare a bool condition;
example:
getResult(){
List lst=[1,"5",4];
var str = 1;
if(str.runtimeType == String){
print(lst[1]);
} else if(str.runtimeType == int){
print('${lst[0]}, ${lst[2]}');
}
}
In this example the output will depend on the type of the variable str. Of course you can also use other types to compare (double, bool)
The answer to the question is yes. You're free to define the function as returning dynamic and return any type you want.
Should you do that? Probably not. Look into creating a union type. A union type that is built into Dart is the FutureOr class which can represent a future or the resolved type. The Freezed package can help generate union types without too much fuss.
I am asking this hesitantly as I know this is probably a dumb question.
I am returning a Realm result and then have gone ahead and tried to cast it to a String as normal (to put in a text label).
However I'm getting an error 'init' has been renamed to 'init(describing:)'.
When I try use the describing method instead, the label prints "Optional" inside it which obviously isn't what I want.
Is there a reason I can't use :
previousTimeLabel.text = String(lastRecord?.time)
I'm sure I've done this before and it's been fine, am I missing something? (lastRecord.time is an Int).
I've checked the answer here about Interpolation Swift String Interpolation displaying optional? and tried changing to something like this :
if let previousRounds = String(lastRecord?.rounds) {
previousRoundsLabel.text = previousRounds
}
but get the same error + Initializer for conditional binding must have Optional type, not 'String'
The issue isn't String(lastRecord?.time) being Optional. The issue is lastRecord being Optional, so you have to unwrap lastRecord, not the return value of String(lastRecord?.time).
if let lastRecord = lastRecord {
previousRoundsLabel.text = "\(lastRecord.time)"
}
To summarize Dávid Pásztor's answer, here's a way you can fix it:
previousTimeLabel.text = String(lastRecord?.time ?? 0)
This may not be the best way for your application. The point Dávid was making is that you need to deal with lastRecord possibly being nil before trying to pass its time Int into the String initializer. So the above is one simple way to do that, if you're ok with having "0" string as your previousTimeLabel's text if there was no lastRecord.
My code is as follows
var List_mall_rowid = [Int]()
let mall_rowid = SQL_list?.intForColumn("MH_rowid")
var duplicate: Bool = false
for rowid in List_mall_rowid{
if rowid == mall_rowid{
duplicate = true
}
}
but the if statement fails with compiler error
binary operator == cannot be applied....
I must have tried dozens of different syntax options, getting dozens of errors. What is the correct way to make this comparison?
In your if statement your rowid is Int type and your mall_rowid is another type (I am not sure which type) so you can not compare it and if you want to compare both then both must have same type.
I have the following section of code in an app that I am writing:
...
String[] Columns = Regex.Split(CurrentLine, Delimeter);
Nullable<Double> AltFrom;
...
if (AltFrom == null)
{
Double.TryParse(Columns[LatIndex].Trim(), out AltFrom);
}
...
The line in the if clause will not compile and shows the error: cannot convert from 'out double?' to 'out double'
if I don't make AltFrom a Nullable type and rather explicitly state it as a Double, everything is happy.
Surely this is valid code. Is this just a bug in c# or am I doing something wrong?
No the out parameter really needs to be a double, not a Nullable<double>.
double? altFrom = null;
double temp = 0;
if (double.TryParse( Columns[LatIndex].Trim(), out temp))
{
altFrom = temp;
}
First, you can not implicitly convert a double? to a double. The reason is because what would be the value of the double if the double? represented the null value (i.e., value.HasValue is false)? That is, converting from a double? to a double results in a loss of information (it is a narrowing conversion). Implicit narrowing conversions are generally frowned upon in the framework.
But actually, the problem that you are seeing here is something different. When you have a method that accepts an out parameter of type T, you must pass in a variable of type T; there can not be any type variation in this case as there is with non-ref and non-out parameters.
To get around your problem, use the following:
if (AltFrom == null) {
double value;
Double.TryParse(Columns[LatIndex].Trim(), out value);
AltFrom = value;
}