Browsing 7239 questions and answers with Jon Skeet

Deserialize json data into list in c#

I am calling an external web service and this is what I get in response after posting to their server: { "status":200, "data":{ "h21":{ "total_price":{ ...
Jon Skeet
people
quotationmark

Basically, TotalPrice should be a Dictionary<string, Availability> or similar. It's not clear what list you'd have, but that's naturally a dictionary. That's then nested within a dictionary at the top level. Sample code: using... more 2/28/2017 11:32:49 AM

people

RaiseEvent with Ternary Operator

I expected the following to work: RaiseEvent If(condition, event, event) But the compiler complains about it: Error BC30676: "If" is not an event of ... How to realize a...
Jon Skeet
people
quotationmark

Look at the syntax for RaiseEvent: RaiseEvent eventname[( argumentlist )] It's not that RaiseEvent just accepts any arbitrary expression - you have to specify the name of an event. You'll just need to use a regular If statement: If... more 2/27/2017 2:42:02 PM

people

Is it possible to: increment index by 1 and wrap back to 0 if(index > list.Count) within 1 line of code?

What I'm doing right now: index++; index %= list.Count; I want to merge them into 1 quick line, something like: ++index %= list.Count; But the compiler is not allowing me...
Jon Skeet
people
quotationmark

I'd be slightly surprised if the first version worked in C or C++, but then it does surprise me quite often. The reason it doesn't work in C# is that the left-hand side of the %= operator has to be a variable, and the expression ++index... more 2/25/2017 5:31:00 PM

people

Sort a hashmap by date

This is the my json: [ {"name": "John Smith","date":"2017-02-02T23:07:09Z","id":"1223234"} {"name": "John Doe","date":"2015-07-03T21:05:10Z","id":"3242342"}, {"name": "Jane...
Jon Skeet
people
quotationmark

I have already converted the above to hashmap. I would suggest you don't do that. I suggest you convert it to a List<Person> where each Person has a name, date and ID. (If not Person then some other class.) It makes more sense... more 2/25/2017 5:25:13 PM

people

Eclipse cannot resolve package name

I have started to learn Java along with Eclipse and the book "Thinking in Java" by Bruce Eckel. I tried to add his util package (from net.mindview.util) to a project but Eclipse...
Jon Skeet
people
quotationmark

You've got one directory too far when adding the library to the build path. The library should be showing up as TIJ4Code, in the Java directory. (Or in other words, when you choose "Add class folder" you should be choosing TIJ4Code, not... more 2/25/2017 12:53:58 PM

people

Log4j, meaning of Append = true / false

log4j.appender.LOGFILE.Append = true The doc says: If the append parameter is true, the file will be appended to. Otherwise, the file designated by filename will be truncated...
Jon Skeet
people
quotationmark

Yes, "the file designated by filename will be truncated" means that any data that previously existed in the file will be gone. This is a more general concept than just logging. Suppose you have a file initially containing the data... more 2/23/2017 10:29:22 AM

people

joda Period returns 0 months

I have 2 joda.DateTimes, which I use to create a Duration. The Duration is then converted to a Period, and although the two dates are 3 years apart, the Period.getDays,...
Jon Skeet
people
quotationmark

You're calling AbstractDuration.toPeriod: Converts this duration to a Period instance using the standard period type and the ISO chronology. Only precise fields in the period type will be used. Thus, only the hour, minute, second... more 2/22/2017 4:21:09 PM

people

Visual Studio C# Code colour of this

I have been fiddling with the colours of the various C# items in my new Visual Studio 2017 (Community) using the Tools-Options-Font and Colors. Everything is fine apart from the...
Jon Skeet
people
quotationmark

It's just a keyword, the same as all the others. I don't think you can tell VS to highlight it differently to other keywords. more 2/21/2017 3:53:24 PM

people

Unexpected result with Where()

I have this code, which is supposed to return a value type, applying at each step the transformations specified in steps. private static T Transformed<T>(T x, params...
Jon Skeet
people
quotationmark

The Where is lazily evaluated - you're never using the result of it, so the predicates are never being evaluated. You could force iteration by counting the results or similar: var ignored steps.Where(f => (x =... more 2/21/2017 3:36:25 PM

people

Java (Android) BufferOverflowException on putInt

I try this code : byte arr[] = ByteBuffer.allocate(2).putInt(1).array() But it fails with a BufferOverflowException. Is 1 too big to be stored in 2 bytes ? Or is my problem...
Jon Skeet
people
quotationmark

Is 1 too big to be stored in 2 bytes ? Well, an int is... putInt always writes 4 bytes. From the documentation for ByteBuffer.putInt Throws: BufferOverflowException - If there are fewer than four bytes remaining in this... more 2/21/2017 10:25:29 AM

people