The above code has a conditional breakpoint set at its bottom line in yellow followed by the Breakpoint Settings dialog which should work with:
item.Value == "aday"
However I get the below error, I have searched online for this and can't find any reason why this should fail. Im using VS 2015 Pro.
EDIT- Thank you for pointing out the obvious error on my part, I do normally code in C#.
But now using a single '=' I get this???????
I assume that I it equates to an assignment, and adding parenthesis didn't help either?
Just tested with a sample VB.NET project.
The problem is the ==. This is C# syntax but since you have a VB.NET application you should use a single equal
item.Value = "aday"
(I have always something new to learn from SO)
If item.Value.Equals("aday") Then 'Temp If please remove
Debugger.Break()
end if
Actually works in strict mode, Gasp!!!!
Thanks to all contributions, greatly appreciated :)
I am using the C# in Visual Studio 2017.
After search in an hour, conclusion was:
rewrite the conditional expression from:
item.Value == "aday"
to:
item != null && item.Value == "aday"
MAKE SURE item was not null. so that you can refer to field of value with item.Value
Related
ASP.NET 5 MVC project from .NET 4 uses Substring is source code like
id = row.Categoryid?.Substring(0, 1) == "$" ? row.Categoryid?.Substring(startIndex: 1) :
row.Categoryid?.Substring(0)
Visual Studio 2019 quality inspector throws message
IDE0057 Substring can be simplified
at
startIndex: 1
After applying suggested fix
Use range operator
code is refactored to
id = row.Categoryid?.Substring(0, 1) == "$" ? row.Categoryid?.Substring[1..] :
row.Categoryid?.Substring(0)
which throws compile error
Error CS0021 Cannot apply indexing with [] to an expression of type
'method group'
How to fix this so that re-factor creates correct code ?
Disregarding any other problem an to address the issue at hand, you can't use a range on a method such as Substring E.g Categoryid.Substring[1..]
It would need to be:
row.Categoryid?[1..]
Or taking this to its logical conclusion:
var id = row.Categoryid?[0] == '$' ? row.Categoryid?[1..] : row.Categoryid;
As to why this was suggested is another question entirely. It's either a VS bug or maybe Resharper depending on what suggested and implemented it
Note: Assuming you are just trying to remove an $ from the beginning of the string, and there will always only ever be one (or if there are more than one, you want them removed as well). You could save yourself a bunch of printable characters by using TrimStart.
Removes all the leading occurrences of a specified character from the current string.
id = row.Categoryid?.TrimStart('$');
Update
So on further analysis it's a bug to do with the Null-conditional operator. When it's removed row.Categoryid.Substring(1) substitutes correctly row.Categoryid[1..]
Update
Further update, it seems this is a known bug and already has a pull request for this issue slated for release 16.9.P1
Fix "IDE0057: Invalid code fix with string.Substring and null-conditional operator" #47377
I am building an application on c# visual studio for a web page. I have in a dropdownlist in C# a item "soporte\andres" but when i take that value i have "soporte\andres"
This is the image about what i have in a dropdownlist:
but doing a debug i have this value in a variable:
What can I do to get the exact value I need ("soporte\andres") ?
I tried to use #dropwdownlist.value but this doesn´t work. Visual Studio continues changing "\" for "\\".
I can't see your code, but in general, you would do something like this:
string a = #"soporte\andres";
Resharper allows to replace only one issue. How to replace all same issues in entire proect?
For example, to replace
ctx.Shops.Where(t => t.ShopId == currentBase.ID).Single()
by
ctx.Shops.Single(t => t.ShopId == currentBase.ID)
you should push button as shown in the
How to replace all accurances of Sinle method in entire project?
Version of resharper 9.1 and 10
Hmm, fixing this for entire file/project/solution (aka "fix in scope") does not seem to be supported.
The closest I could think of is selecting Inspection "Replace with single call..." > Find similar issues..., and then navigate between the results and fix manually.
I'm trying to figure out how to have a short, one line conditional statement.
If this date is not null, add the filter to the current list of filters:
fromDt ?? filters.Add(FilterType.DateFrom, fromDt);
Is there a way to do this? I know I could do..
(fromDt != null) ? "something" : "something_else", but I don't need the 'else', and would really like to just use the ?? operator for null checking.
What's wrong with this?
if (fromDt != null) filters.Add(FilterType.DateFrom, fromDt);
First and foremost, your code should be readable. Even if your ?? code works, I wouldn't know what it does on first glimpse.
The code you are attempting makes your code very difficult to read. Like BrokenGlass said, you are trading clarity for raw character count.
This is the only "one line" solution C# supports.
if (fromDt != null) filters.Add(FilterType.DateFrom, fromDt);
But I encourage everyone to expand this to at least two lines (my preference is four with the braces).
Purpose of the solution aside, following one-liner might give you end result you want while using ??. Do not try this at home though.
filters.Add(FilterType.DateFrom, fromDt ?? DateTime.MinValue)
The idea is to set DateFrom to min possible value, essentially adding an open filter.
I'm using VS 2010 + resharper, and i'm tired reformatting bracket indentation in code as i want it. As example if i have code like:
operators.Keys
.ToList()
.ForEach(k => filters
.AddRange(CustomHtmlHelpers.GetIdAndValueListByPrefix(queryString, k)
.Select(t => new QueryFilter()
{
Operation = operators[k],
PropertyName = t.Item1,
Value = t.Item2
})))
And if i put ; in the end VS (or resharper) 'fixes' bracket indentation so code becomes like:
operators.Keys
.ToList()
.ForEach(k => filters
.AddRange(CustomHtmlHelpers.GetIdAndValueListByPrefix(queryString, k)
.Select(t => new QueryFilter()
{
Operation = operators[k],
PropertyName = t.Item1,
Value = t.Item2
})));
Same happens if i use resharper's code cleanup. I probably could turn off automatic code reformatting on ; but i need it in other situations.
I tried changing code formating options both in VS and resharper setting but never got indentation as i want it.
How could i configure vs or resharper so that it would not do more than one tab formating? Or maybe there is other plugin i can use (together with r#) specificly for this purpose?
EDIT: for anyone interested in this problem here is same question in r# forum http://devnet.jetbrains.net/thread/304794 anyone who would like to see better nested code indentation from r# are welcome to vote for it here http://youtrack.jetbrains.net/issue/RSRP-88220
just guessing...
go to ReSharper -> options -> Code Editing -> c# -> Formatting Style -> Other
Search in Align Multiline Constructs and try toggling the state of checkbox "Chained method calls" (I suppose your value is 'checked' for this checkbox).
if not this one, i expect that required setting is somewhere very near :-)