When does ScrollViewer.ChangeView fail? - c#

ScrollViewer.ChangeView doesn't always work. When it doesn't - it returns false (MSDN).
What are the conditions for that happening?
EDIT
Most methods either work or throw an exception. This one has a return value. This means there are conditions where it fails (and returns false). What are those conditions?
EDIT 2 (after awarding the bounty)
I awarded the bounty so as not to waste the reputation points. I'm still looking for THE answer. (Also, the answer was written before my edit to the question.)

The return value of ScrollViewer.ChangeView signals whether the view changes (true) or not (false).
The most obvious reason why the view may not change after a ChangeView call is because it was already displaying the requested horizontal and/or vertical offsets and/or zoom factor before the call.

Related

1 == 0 returns True. Why?

I've been tracing an issue for a bit, and it turned out it was caused by an if-condition where apparently 1 == 0 returns True. I'm curious as to what exactly went wrong there?
currentChapter = 1, and selectedChapter = 0 but the break point seems to be reached anyway.
I fixed the issue by adding the following braces:
if (currentChapter == selectedChapter)
{
SelectPage(selectedButton);
}
But, the fix doesn't really make sense to me. According to #jon-skeet's answer on this question: Does C# support if codeblocks without braces?
Yes, it supports it - but it takes the next statement, not the next line.
In accordance, the next statement should be SelectPage(selectedButton), which therefore should be ignored if the if-statement returns false.
What am I missing here? I'm mainly concerned that this behavior may repeat in the future as I'm used to not adding braces when they do not seem necessary.

Is it good practice to wrap trivial functions in wrappers?

I'm doing a code review and I've noticed that the developer has done this:
UserSession.LocationId = CheckInteger(elementValue);
with this wrapper
private int CheckInteger(string elementValue)
{
int outNumber;
int.TryParse(elementValue, out outNumber);
return outNumber;
}
I can't see that this brings very much to the party. Should I push back, or just let sleeping dogs lie? I don't think there's any particular company policy that covers this.
Are they sleeping dogs or wolves? This code effectively discards non-integer values and returns 0 for failed cases.
This may or may not be appropriate, but the name certainly is misleading - no check is performed at all. Errors are simply discarded. It's almost equivalent to hiding exceptions.
This can hurt you when invalid values aren't detected and 0s are set in unexpected places.
Such a method should at least be renamed to GetIntegerOrDefault so it's explicit that it will return a default value on error.
You should also ensure that 0 is a valid value for LocationId or at least an expected default. Otherwise you may encounter some very hard to diagnose bugs.
I've encountered bugs in a Stock Exchange application that send portfolio performance ratios of -100%, because someone 10 levels bellow decided to return 0 on invalid exchange rate values, and nobody remembered 5 years later.
Given that you could just, in this case, write int.TryParse(elementValue, out UserSession.LocationId) I would say this is not a good idea.
Now, if you needed to add logging to it or deal with special cases/errors in a consistent way, then sure go for it.

Replace values located close to themselves with the mean value

I'm not sure if SO is the proper place for asking this, if it's not, I will remove the question and try it in some other place. Said that I'm trying to do the following:
I have a List<double> and want to replace the block of values whose values are situated very close (say 0.75 in this example) to a single value, representing the mean of the replaced values.
The values that are isolated, or alone, should not be modified.
Also the replaced block can't be longer than 5.
Computing the mean value for each interval from 0, 5, 10.. would not provide the expected results.
It happened many times that LINQ power surprised me gladly and I would be happy if someone could guide me in the creation of this little method.
What I've thought is to first find the closest values for each one, calculate the distance, if the distance is less than minimum (0.75 in this example) then assign those values to the same block.
When all values are assigned to their blocks run a second loop that replaces each block (with one value, or many values) to its mean.
The problem that I have in this approach is to assign the "block": if several values are together, I need to check if the evaluating value is contained in another block, and if it so, the new value should be in that block too.
I don't know if this is the right way of doing this or I'm over complicating it.
EDIT: the expected result:
Although you see two axes only one is used, the List is 1D, I should have drawn only the X axis.
The length of the lines that are represented is irrelevant. It's just to mark on the axis where the value is situated.
It turns out that MSDN has already done this, and provided an in-depth example application with code:
Data Clustering - Detecting Abnormal Data Using k-Means Clustering

error provider is not blinking uniformly

I have an error provider providing error for 4 controls..
when I set all the four errors, only two of them blink together at a time and all four settle down after certain time..
even if I set two errors, both blink alternatively..
but I want all of them blink together...How can I do this? (I don't prefer using more than one errorProvider)
You've hinted to an (IMO) acceptable solution, with your last statement: use 2 error providers, one dedicated to blinking exactly one control at a time (the latest one with an invalid input, or the one you'd expect to be corrected ASAP, or whatever criteria you deem most important), and a second one that "silently" displays the icon on all controls with invalid input.

DragDropEffects.Scroll: why -2147483648 not 8?

A standard enumeration in System.Windows.Forms:
[Flags]
public enum DragDropEffects
{
Scroll = -2147483648,
//
// Summary:
// The combination of the System.Windows.DragDropEffects.Copy, System.Windows.Forms.DragDropEffects.Link,
// System.Windows.Forms.DragDropEffects.Move, and System.Windows.Forms.DragDropEffects.Scroll
// effects.
All = -2147483645,
None = 0,
Copy = 1,
Move = 2,
Link = 4,
}
Quite a strange value for Scroll, don't you think?
As I understand these values all come from "the old times" of COM\OLE DROPEFFECT... But why were they chosen so in the first place? Did author try to reserve the interval between 8 and 0x80000000 for something? Is it usefule somehow or is there an interesting story behind it or it's just another long-lived illustration of the YAGNI principle?
It is a status flag, separate from the principal drop effects (Copy/Move/Link). Short from leaving room for future drop effects, picking the high bit allows a trick like checking if the value is negative. Same kind of idea as an HRESULT or the GetAsyncKeyState return value.
Yes, it looks like an "interesting" hack of some sort. Common sense would suggest using 8, but maybe there's some Windows version related reason why 8 couldn't be used, and so the author used -2147483645 (-0x80000000) instead. It's not that unusual a number - whoever wrote it is just starting with a binary '1' from the high significant end rather than the low significant end.
Perhaps scrolling was regarded in some other group of drag/drop effects to copy/move/link, and so the author wanted to place it at the other end of the word, along with any other future similarly different effects.
Maybe there's some awful piece of logic somewhere to test to see if a DragDropEffects variable is greater than zero (intending to mean "anything that isn't none"), and Scroll should not fall in that range?
Bit of a mystery. At the very least you'd think they would put the constant in as hex, to show it's not just some totally random number.
This allows a quick check for > 0 in order to know whether Copy, Move, or Link are being invoked. It excludes None as well as Scroll.

Categories