I get an error "Invalid expression term 'long'" in the code below:
if (Int64.TryParse(Response.ToString(), out long time) == false)
{
this.ResponseStatus = "Unexpected Server Response!";
return false;
}
this.ResponseStatus = "License OK";
return true;
The Int64.TryParse() method takes arguments string s and out long result.
I can recreate the same error you are getting, but with ref instead of out. Here's an example:
private void Test(ref long result)
{
//some code in here
}
I try to call the method like this:
Test(ref long time);
And with this I get the same errors your are getting.
Now the question is, why you are getting the exact same error while using out.
If you don't need the variable long time afterwards, you could just call the TryParse method like this:
Int64.TryParse(Response.ToString(), out _);
The underscore _ just discards the value that would be outputted.
But I think you just didn't provide the correct code corresponding the error. You should provide us with the whole and correct code, so we can help.
The code you show is not the code that causes those compiler errors. You must have something that looks like this:
if (Int64.TryParse(long time) == false)
This yields exactly those three errors:
Error CS1525 Invalid expression term 'long'
Error CS1003 Syntax error, ',' expected
Error CS0103 The name 'time' does not exist in the current context
The format you need is exactly as you've posted it, so fix your code so it looks like the code in your question.
Related
I have the following code using pattern matching with a property pattern that always succeeds:
var result = new { Message = "Hello!" } is { Message: string message };
Console.WriteLine(message);
The above match succeeds(result is true). However, when I try to print message, I get a:
CS0165: Use of unassigned local variable.
However, the following works:
var result = "Hello!" is { Length: int len };
Console.WriteLine(len);
Also, when I use it with an if, it just works fine:
if (new { Message = "Hello!"} is { Message: string message })
{
Console.WriteLine(message);
}
Why the discrepancies?
Why the discrepancies?
Well, it's a compile-time error, and at compile-time, it doesn't know that it would match. As far as the compiler is concerned, it's absolutely valid for result to be false, and message to be unassigned - therefore it's a compile-time error to try to use it.
In your second example, the compiler knows that it will always match because every string has a length, and in the third example you're only trying to use the pattern variable if it's matched.
The only oddity/discrepancy here is that the compiler doesn't know that new { Message = "Hello!" } will always match { Message: string message }. I can understand that, in that Message could be null for the same anonymous type... slightly more oddly, it looks like it doesn't know that it will always match { Message: var message } either (which should match any non-null reference to an instance of the anonymous type, even if Message is null).
I suspect that for your string example, the compiler is "more aware" that a string literal can't be null, therefore the pattern really, really will always match - whereas for some reason it doesn't do that for the anonymous type.
I note that if you extract the string literal to a separate variable, the compiler doesn't know it will always match:
string text = "hello";
var result = text is { Length: int length };
// Use of unassigned local variable
Console.WriteLine(length);
I suspect this is because a string literal is a constant expression, and that the compiler effectively has more confidence about what it can predict for constant expressions.
I agree that it does look a bit odd, but I'd be surprised if this actually affected much real code - because you don't tend to pattern match against string literals, and I'd say it's the string literal match that is the unexpected part here.
I would like to do some kind of console framework and for that I need command and arguments for the command. Here is part of my code:
string str = "I do not know how to fix this problem";
List<string> substringList = str.Split().ToList();
List<string> allArgsExcept1st = str[1..^0].ToList();
The input is type string.
The third line throws an error:
Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>'.
I am new to C# so I don't know what to think about this error and how to possibly fix it.
Thank you.
Based on your latest edit:
string str = "I do not know how to fix this problem";
List<string> substringList = str.Split().ToList();
List<string> allArgsExcept1st = str[1..^0].ToList();
The problem you have is here:
str[1..^0].ToList()
When you are using the range/slice function ([1..^0]) on your string you are, in return, getting a string. So calling ToList() on that string will give you a List<char> (since a string is also a IEnumerable<char>) but you are assigning it to a List<string>.
Its unclear what you are ultimately trying to do here, but to fix your problem you just need to change your last line to:
List<char> allArgsExcept1st = str[1..^0].ToList();
First I was getting this error:
Invalid object name "product_images_temporary"
and after I have added the [] brackets, everything worked fine. But then when I removed them again, I got this error:
Incorrect syntax near ''
Why does this work:
[product_images_temporary]
but this throws an exception ("Incorrect syntax near ''"):
product_images_temporary
More code:
try
{
using (var sqlConnection = new DapperHelper().DatabaseConnection())
{
var sqlStatement = "SELECT * FROM product_images_temporary";
sqlConnection.Execute(sqlStatement);
}
}
catch (Exception e)
{
}
Is product_images_temporary a reserved word in SQL Server? Like datetime etc.? I can't explain this.
Between the s and the _ is the Unicode zero-width-space character \u200B. This is invisible so makes the string not what it appears to be.
This character is not legal in an SQL object identifier name and is the cause of the error you see, using [] escapes make it legal.
Simply retype the name manually or double-delete between the two characters.
As your code does work with [] it means the actual table name contains \u200B so should also be renamed.
Just rename the table, you have an invisible character in your table's name
string updatedVotes = string.Empty;
updatedVotes = updatedVotes.Substring(0, updatedVotes.Length - 1);
db.Entry(sch).State = EntityState.Modified;
sch.Rates = updatedVotes;
db.SaveChanges();
But the code doesn't compile because of the error you're seeing up there, and I've yet to understand why exactly. Any help anyone?
updatedVotes is a string, but you are trying to assign it to Rates (Which I assume is a collection of Fast.Models.Reviews).
You have the wrong types. A cat cannot be converted to a dog, therefore a string cannot be converted to System.Collections.Generic.ICollection
The error message pretty much tells you what is going on. On this line:
sch.Rates = updatedVotes;
Rates is an ICollection<Review> whereas updatedVotes is a string. You can't assign a string to an ICollection<Review>
I recently bumped into an exception in my code because I was trimming a null string.
I replaced it with the following:
SomeValue = (SomeString ?? "").Trim();
Can this code ever fail?
Thanks.
Note: I know I can add a try/catch; I'm just looking to make this line fail-proof without using a try/catch.
This will not fail (ie. throw a NullReferenceException), assuming SomeString is indeed a string.
You could achieve the same in many ways:
SomeValue = (SomeString == null)?string.Empty:SomeString.Trim();
It's not the way I would have done it, but no, this shouldn't ever fail now.
I'd probably write an extension method that calls trim after checking for null. Something like this:
public static string NullTrim(this String str) {
if(str == null) {
return string.Empty;
}
return str.Trim();
}
This allows all of the following to compile and execute without error:
"".NullTrim();
" test ".NullTrim();
((string)null).NullTrim();
Well if it was failing because of NullReferenceException, then now it will definitely not fail because of that. As for the rest, I cannot say without context.