Browsing 15589 questions and answers with Jon Skeet

Access item in sequence that was skipped using LINQ's .Skip()

foreach(var item in items.Where(x => x.SomeCondition == true).Skip(1)) { item.OneThing = true; item.AnotherThing = true; } For the item that was skipped using...
Jon Skeet
people
quotationmark

Well it sounds like you don't want to use Skip in this case. Just use a local variable to remember whether this is the first iteration or not. bool firstItem = true; foreach(var item in items.Where(x => x.SomeCondition)) { ... more 4/18/2017 1:51:58 PM

people

Decimal.MinValue costs more than you expect

Recently during profiling session one method caught my eye, which in profiler's decompiled version looked like this: public static double dec2f(Decimal value) { if (value ==...
Jon Skeet
people
quotationmark

If decimal.MinValue were only declared as a static readonly field, you wouldn't be able to use it as a compile-time constant elsewhere - e.g. for things like the default value of optional parameters. I suppose the BCL team could provide... more 4/17/2017 8:44:04 AM

people

How to check if date is between interval in java?

I have six int variables: currentMonth, currentDay, monthFrom, dayFrom, monthUntil and dayUntil. I need to check if todays month and day falls within a range of from and until...
Jon Skeet
people
quotationmark

I would suggest using java.time.MonthDay to represent each value involved. You then need to consider two alternative situations: from is before or equal to until, in which case you need to perform a test of from <= current &&... more 4/14/2017 11:03:56 AM

people

Java BigDecimal , equivalent of C#'s Decimal(int[] bits) Constructor

I am trying to convert an input buffer (byte array) containing data that generated with a C# application to java data types. I have some issue with C#'s Decimal dataType. C#...
Jon Skeet
people
quotationmark

In Java, you'd use BigDecimal. That's not quite the same type, but it's reasonably close. You just need to reconstruct the 96-bit integer as a BigInteger, then scale it and optionally negate it: import java.math.BigDecimal; import... more 4/12/2017 3:01:11 PM

people

VCVARSALL.BAT for Visual studio 2017

What is the location of file: VCVARSALL.BAT for Visual studio 2017?
Jon Skeet
people
quotationmark

I don't believe there is such a file - but there is VsDevCmd.bat which I believe satisfies most of the same use cases. (That's certainly what I have as my default command prompt.) It's in Common7\Tools in the x86 part of the VS... more 4/12/2017 2:22:27 PM

people

How can I test the set of my method exceptions through one test only?

NUnit 3, VS2015 I want to test exception types of my method through TestCase attributes using, but NUnit3TestAdapter doesn't see my test in VS2015 (my class is...
Jon Skeet
people
quotationmark

I suspect the problem is that the test method is generic. Instead of using the generic Assert.Throws<T>, use the overload that accepts an exception type: [TestCase(null, typeof(ArgumentNullException))] [TestCase("",... more 4/12/2017 9:46:40 AM

people

Return function from an action or a funct

I have often this kind of code: public bool MyRoutine(TheModel model) { if (string.IsNullOrWhiteSpace(model.Owner)) return false; ... This way I always need to...
Jon Skeet
people
quotationmark

No, there's nothing like this. It would be fine if you were trying to throw an exception - and indeed I have a bunch of Precondition.CheckXyz methods that do exactly that sort of thing - but there's no "conditional return statement" which... more 4/12/2017 7:41:26 AM

people

Getting different hex value

I have one device which gives data in non printable characters through serial port. I am converting the data to Hex by using Encoding(to get byte array of data) and then...
Jon Skeet
people
quotationmark

The problem is that you're trying to read binary data as if it's text. Don't use the ReadExisting() call - use Read(byte[], int, int): public void HandleDataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] data... more 4/12/2017 7:40:20 AM

people

Contructor definition: receiving "no argument given"

I'm trying to create a derived class and I am receiving this syntax error for each constructor. There is no argument given that corresponds to the required formal parameter...
Jon Skeet
people
quotationmark

This: public LabelImage(LabelImage source) { Label = source.Label; image = new MagickImage(source.image); fileinfo = source.fileinfo; } Is implicitly this: public LabelImage(LabelImage source) : base() { Label =... more 4/11/2017 5:58:58 PM

people

Offset for UTC to Daylight Savings Time incorrect

I am trying to set a time to EST, and then find what it UCT time is. (We have our reasons).. I have read that "Eastern Standard Time" should take into account the Daylight savings...
Jon Skeet
people
quotationmark

Fundamentally, you're using the wrong approach - if you want to convert to UTC, use TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo). You'll want to be careful around times that are either invalid (because they were skipped) or... more 4/11/2017 5:52:41 PM

people