I've been having this problem for awhile in Visual Studio 2013. It doesn't seem to understand how to apply the indentation rules properly to lambda expressions when they've been lined up incorrectly. Here is a simplified example:
var s = new Action(() =>
{
});
In the second and third row, the indent is only 3 spaces instead of 4 (the real code example is much, much larger with the inner expression spanning hundreds of lines - this was checked in by my colleague and I'm trying to fix it). I've tried every combination of reformat code, document, re-creating the curly brace, etc. Nothing seems to work. It refuses to automatically update the indentation properly.
I normally wouldn't bother with it, but it causes all the code inside to be off by 1 character as well. When I'm typing lines in the middle, the tab/shift+tab markers are 1 character off from the lines above and below and I constantly have to adjust to get things lined up again. The closest thing I can find to reference this issue is this Connect Feedback from 2013 that is supposedly fixed, but I'm on Update 4 (released Nov 2014) and still experiencing the issue.
Short of manually going through and updating the indentation for every line in the lambda expression, does anyone have an idea how I can quickly fix this code?
Blatantly ignoring the issue in Visual Studio, and providing a solution to the problem right away. Hold alt to enable block selection, select all lines, and type a single space. Just to illustrate:
If you type Hello World!, the result would be:
As a 'rant': a single lambda should not contain hundreds of lines of code, it is a very big nono maintainability wise.
Related
I want to create a simple ternary expression similar to the following:
Convert.ToInt32(stringname.Substring(0,2)) != 99 ?
Convert.ToInt32(stringname.Substring(0,2)) : 15
I get an error about incompatibilities between int and bool. Is there a simple workaround?
Does it have to be a single line? Apparently you already know the logic. Why do you need to cram it into a single code line? Cramming too much code into a single line will only make reading and debugging unessesarily hard.
Debugging will be hard, because you never know wich of those 4(four!) function calls is throwing the exception. It is also unessesarily slow, as you are doing the same two operations twice in a row. Just do it once and store the result.
My advise is to split up the code using temporary variables. One command + 1 assingment per line. This will make debugging and reading doable. Do not worry about performance. Between Compiler Optimisations and the JiT compiler, there is decent chance any underused variables will be cut out at runtime in a release build.
Thanks for the tip below
• You need to go through the string one character at a time (for loop or while loop) When you hit a < you know you have hit a tag, so store the position of this character
• Keep going (in a sub-loop, preferably) until you hit a >, that's your end marker
• Now check the character immediately before the >. Is it /?
• YES: Peek at the top of the stack. Is that string the same as the one between < and />?
If yes, pop that item and break out of the subloop (you found a match!).
If no, return false from the method - your work is done (the HTML is not valid).
• NO: then push the whole string between < and > onto the stack and break out of this subloop, and continue the main loop.
As #seesharper suggested, turn the above into psuedocode then into C#.
Good luck on your journey learning to program!
Your current code simply checks that "{open}" is complete, and that whatever the next tag is, is also complete, not that it is paired with its close tag.
You need to be operating with strings instead of characters. You're going to read in "{open}" and "{/open}" and you need to operate on them.
Start by making a list of your use cases:
You start with a close tag - Work out how do identify it is a close tag, and then when you try to pop your empty stack you know it fails your check.
You start with an open tag - Work out how to identify it is an open tag, and then push it onto your stack.
You find a "complete tag" - one in this format "" - Work out how to identify this type of tag. Do nothing with it, he does not need to be paired once identified correctly.
You encounter multiple open tags in succession. push each onto the stack.
You encounter a close tag - determine if it is properly paired with the top element of the stack - pop and continue if they are properly paired - fail if they are not.
You encounter multiple close tags in succession. Rinse and repeat 5 until tags do not match or you have an empty stack and an unmatched close.
You have a lot of good logic in your current code, but it needs to be expanded to properly perform the task assigned.
NOTE: I have intentionally not provided code, but some logic to help you toward your solution because this is a homework assignment. You will be working almost exclusively with 1) Reading a file. 2) Strings. 3) Stack. Resources for syntax, properties, and methods of each are readily available should you need to look them up.
Also, I used the wrong braces because just tags weren't showing up and it was a quick edit.
Here's my analysis of the problem (I'm not going to give you the code solution to the problem, as others have pointed out this defeats the purpose of this sort of exercise). I'm also not dealing with inconsistently formatted (but still valid) HTML and open-close tag special cases such as <br />, which are common in real HTML:
You need to go through the string one character at a time (for loop
or while loop) When you hit a < you know you have hit a tag, so
store the position of this character
Keep going (in a sub-loop, preferably) until you hit a >, that's
your end marker
Now check the character immediately before the >. Is it /?
YES: Peek at the top of the stack. Is that string the same as the one between < and />?
If yes, pop that item and break out of the subloop (you found a
match!).
If no, return false from the method - your work is done
(the HTML is not valid).
NO: then push the whole string between < and > onto the stack and
break out of this subloop, and continue the main loop.
As #seesharper suggested, turn the above into psuedocode then into C#.
Good luck on your journey learning to program!
A few years ago, I was seeking a way to convert a list of elements into the current Selection (to use for "Copy To Level" or "Copy To Current View". My particular situation is post-program from a "Smart Filter" that allows the user to select multiple family names/types, not just "Structural Framing (girder)" as in Revit's built in filter.
The solution HAD BEEN:
SelElementSet SelSet = uiDoc.Selection.Elements;
SelSet.Add(Element1);
SelSet.Remove(Element2);
The problem is, this no longer seems to be working in Revit 2016 (+). Running the code with these lines now causes a program ending error:
"Revit encountered a System.MissingMethodException: Autodesk.Revit.UI.Selection.SelElementSet Autodesk.Revit.UI.Selection.Selection.get_Elements();"
(I assume the line "SelElementSet SelSet = uiDoc.Selection.Elements" invoked .get_Elements)
I am able (at the start of my program) to obtain the current selection using
Selection All_Guys = uiDoc.Selection;
and from this I can convert everything to Ilist or List etc., based on using Tree nodes to remove particular categories/family names/family types. But then I need to be able to convert this all back to the current selection (hopefully using SelSet.Remove(Element2) for the elements that do not match the filtering), and every time I use SelElementSet, I get the program ending error above.
Note that in September, 2014 I asked a SIMILAR question. I know there are powerful arbiters on this site that are itching to mark questions as already answered -- this goes under a category "previous answer no longer works". Please read the question more carefully and don't have it thrown out just because you have the power.
I have discovered that this is because SelElementSet was removed for Revit 2015 and beyond, and has been replaced with the following (type of) structure. In my example, I clear the selection and add specified elements, but I could also have ....elementids.Remove(One_Element) from another collection of elements:
if (SmartCopyLoad.ResetSelection)
{
ICollection<ElementId> elementIds = uiDoc.Selection.GetElementIds();
elementIds.Clear();
foreach (Element One_Element in SmartCopy.MatchingElements) { elementIds.Add(One_Element.Id); }
uiDoc.Selection.SetElementIds(elementIds);
return Autodesk.Revit.UI.Result.Succeeded;
}
The result of this is the specified elements as a collection (as SelElementSet used to allow).
Note also that a major part of the problem was using old References. My years old code still referenced RevitAPI from 2014, which allowed SelElementSet, but wouldn't work in Revit2016. A word of warning to others: use older references only if necessary for programs running in the older software. Since we are only using 2015 and beyond, I can use the newer references.
This is a question for using Lucene via the NHibernate.Search namespace, which works in conjunction with Lucene.
I'm indexing a Title in the Index: Grey's Anatomy
Title : "Grey's Anatomy"
By using Luke, I see that that title is getting Tokenized into:
Title: anatomy
Title: grey
Now, I get a result if I search for:
"grey" or "grey's"
However, if I search for "greys" then I get nothing.
I would like "greys" to return a result. And I guess this could be an issue with any word with an apostrophe.
So, here are some questions:
Am I right in thinking I could fix this issue either by changing something on the time of index (so, changing the tolkenizer..??) or changing it a query time (query parser?)
If there is a solution, could someone provide a small code sample?
thanks
If you make a classic Term search using Lucene, then greys it's most likely not to show on the results, except that you make a nice tokenizing work when saving, so from where I see it, you have 2 choices or a 3rd beign a combination of them:
Use a Stemmer for indexed data and query. Stemmers are fast, and you can always find an implementation of Porter's stemmer somewhere in Google. Problem is when you look for different languages.
Use Fuzzy queries. Using a Fuzzy Query you can set the edit distance that you want to get "away" from the word being search. The thing is that because 2 words are "close" using an edition distance (i.e, Lehvenstein) doesn't mean that they're the same, but the problem of Grey and Grey's and Greys should be solved with setting an edit distance of 2.
I think you will be able to find a decent implementation of the Porter Stemmer, which is nice right here.
Hope I can help!
VS2010 / R#5.1
I have this "line" of code:
With.Mocks(_mocks).Expecting(() => {
_fooServiceMock.Expect(x => x.FooMethod()).Return(fooMockData);
}).Verify(() => {
});
I perform a R# code cleanup, which changes the code as follows:
With.Mocks(_mocks).Expecting(() => { _fooServiceMock.Expect(x => x.FooMethod()).Return(fooMockData); }).Verify(() => { });
That is, it reformats the statement such that it appears entirely on one line.
What IDE/R# setting is responsible for this? What can I change to preserve my line breaks when I perform a R# code cleanup?
I would have thought 'R# / Options / Languages / C# / Formatting Style / Line Breaks and Wrapping / Preserve Existing Formatting / Keep existing line breaks', but that doesn't seem to make any difference.
It's Place simple anonymous method on single line option in Line Breaks and Wrapping category.
Go to Tools --> Options then scroll to the bottom and under Tools (different than the first) go to Code Cleanup. If you do not have a profile to edit then just create one and select your settings. I think for what you are looking to do you want to have reformat code unchecked.
Now the next time you run Code Cleanup it won't move it to one line.
For more help check out
http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Creating_Custom_Profiles.html
Edit: Noticed this
Reformat code
Reformats you code according to options configurable in ReSharper | Options | Languages | C# | Formatting Style for C# code.