Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I ran a static code analysis tool on our tool and looking at its results the code below was one of the things it was talking about:
SpreadSnapshot oSnap = new SpreadSnapshot();
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So I changed it to the code below and it fixed the error that the tool was talking about:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
{
oSnap.SetRowCol(fpSpread, row, col);
SpreadSetComboBox(fpSpread, list, displayProperty);
}
So in your opinion Which style of coding do you think is more appropriate and less error-prone?
Thanks
The latter - it ensures that you don't end up using oSnap after the using statement.
Aside from anything else, it would be pretty odd for SetRowCol to return something disposable... what would that even mean?
The two mean completely different things, unless SetRowCol returns this at the end. In the first, you're disposing the results of SetRowCol. In the second, you're disposing the SpreadSnapshot.
If both are disposable, you should do a using for both:
using (SpreadSnapshot oSnap = new SpreadSnapshot())
using (oSnap.SetRowCol(fpSpread, row, col))
{
SpreadSetComboBox(fpSpread, list, displayProperty);
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given the Following code:
List<Digit> tempDigits = input.Digits;
string test = tempDigits.ToString(); // -> {2,2,7}
tempDigits.Reverse();
string test2 = tempDigits.ToString(); // -> {2,2,7}
This is my code:
and that the result (in between, I renamed the variable according to naming convention as suggested by Thiessen)
For some reason, the reverse soes not seem to do its job.
It must be something really simple. But I cant get what the Issue is. I hope, someone can help out.
The code you've posted would work correctly, in a vacuum. (Except, in what world does List<>.ToString() produce "{2,2,7}"?)
My main guess would be, as Lesiak commented, that this.Digits and input.Digits points to the exact same List<> instance. So Digits1.Reverse() reverses the list and Digits2.Reverse() reverses the reversal, putting it back how it started. Perhaps this and input are the same thing. Or perhaps they are two different things that happen to use the same underlying Digits list. Who knows?
There are many other possibilities, some of which would be impossible to guess at based on the information provided. For example:
Maybe the input control that's giving you that list is trying to actively manage the list, and reorders it as soon as it notices it's been reversed.
Maybe you're not using the System.Collections.Generic.List<> type, but instead you're referencing some other namespace where someone has written a List<> type that doesn't behave how you'd think it would. Maybe it doesn't even implement Reverse(), so the compiler is picking up on the Enumerable.Reverse() extension method, which does nothing to mutate the underlying data.
It really is impossible to know for sure without knowing more about your environment.
The information you give is incomplete to address the issue.
A reproducible example from the information you provide does not show the same symptoms.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> digits = new List<int> {2,2,7};
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
digits.Reverse();
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
}
}
Which gives the expected output
227
722
https://dotnetfiddle.net/Hs9H0k
My guess would be, this.Digits is not a mutable reference. But that is just an hypothesis.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a bit of a problem with my c# code.
I have a while loop:
while(current.parent != null)
{
solution.Add(current.move);
current = current.parent;
}
All of the variables have values, but for some reason it doesn't enter the while. I placed a break at the while and see that the parent property is not null, but it just skips the entire while.
Any ideas why? or how I can modify this to work?
Because current.parent is null to begin with.
Probably because current is the top most in your tree, i.e. has no children. (Or whatever you want to call it)
The code in your question is very vague but I would guess just using
solution.Add(current.move);
while(current.parent != null)
{
current = current.parent;
solution.Add(current.move);
}
may help.
This may solve the actual problem you are having (other than not knowing how to use a debugger ;))
So here you log the current move always, then log any parent moves.
Maybe look at refactoring this with a do..while loop!!
After seeing that every node in the tree had a null value (but the properties of the node weren't), I changed everything to work with the Node's properties instead, which finally worked.
Thanks guys =)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a for loop and it keeps breaking after going around once however I'm unsure on why it is doing this.
for (int fX = 1; fX < 17; fX++)
{
foreach (RoomItem OldItem in Room.GetRoomItemHandler().GetFurniObjects(fX, 26))
{
Logging.WriteLine(OldItem.BaseItem.ToString());
Room.GetRoomItemHandler().RemoveRoomItem(OldItem, false);
}
}
NOTE:
Logging.WriteLine is the same as Console.WriteLine, its just my logging system.
Result of console:
1
But the console should be outputting (1,2,3 and so fourth)
Any ideas? I even tried locking the foreach iteration but that didn't work.
You modify the collection while you iterate through it. This results in undefined behaviour.
You may fix this with using a constant list for the foreach like so:
foreach (RoomItem OldItem in
Room.GetRoomItemHandler().GetFurniObjects(fX, 26).ToList())
{
...
}
But I think you should rather think through your design again.
You should not be changing a collection while iterating through it using a for-loop, re-write using while or do-while.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Suppose you have a parallel loop in C# and want to use : Interlocked.Increment
Also, suppose there is a heavy process in your loop. Do you think putting Interlocked.Increment before or after that heavy process might make any difference?
In other words, which of the followings is more preferred?
UPDATE: Suppose our criterion for being better or worse is the overall speed.
Prog1:
int iter = 0;
Parallel.For(...{
HeavyProcess()
Interlocked.Increment(iter);
});
Prog2:
int iter = 0;
Parallel.For(...{
Interlocked.Increment(iter);
HeavyProcess()
});
The "one that is preferred" is simply: the one that is functionally correct. Is it meant to record the number of complete operations (end)? or the number of started operations (beginning). Other than that: no difference.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
C#:
var articles = Article
.OrderBy(x=> x.Name)
.Where(x=> x.Name.Contains(pattern))
.Select(x=>new {x.Name + " (article)"})
.ToList();
It's good to write this way in C# and it's called "methods concatenation". In fact, I don't remember exactly how they are called, I read it in Jon Skeet's book. The idea is that each method is on a new line, and it's normal in C#.
What about Ruby? Is it normal to write:
articles = Article
.order(:name)
.where("name like ?","%#{pattern}%")
.map(&:name)
.map {|c| c << " (article)"}
Method chaining is a staple in many languages, Ruby included. Its use is largely a matter of personal taste – some, like tokland, don't like the large expressions that can result, whereas I will gladly expand an expression to eliminate temporary variables.
Subjective question, so her's a subjective answer (as Ruby programmer):
I prefer not to create "holes", in my code I wouldn't insert that level of indentation.
You can also insert the dots at the end of the line. Which is more readable? hard to say, I prefer the latter (although I don't mind at the beginning of the line, it's not a big deal). So I'd probably write (note that those two maps could be joined):
article_names = Article.
order(:name).
where("name LIKE ?", "%#{pattern}%").
map(&:name).
map { |name| name + " (article)"}
In my experience, long chains make code harder to follow. When the chain grows too much (5, 6 elements?) I tend to break it creating intermediate variables with meaningful names, this helps me to further describe the expression:
filtered_articles = Article.order(:name).where("name LIKE ?", "%#{pattern}%")
names = filtered_articles.map { |article| "#{article.name} (article)" }