Browsing 15528 questions and answers with Jon Skeet

Recursive method wont loop childs children

I´m trying to create a recursive method to create a menu. The menu I want to achieve displays the childrens children and so on. I´ve debugged the code and it goes all the way down...
Jon Skeet
people
quotationmark

Each time you call GetMenu, you're creating a new StringWriter - but you're ignoring the return value from your recursive calls. The simplest fix would probably be to change it to: public string GetMenu(Node currentPage) { var... more 2/28/2017 1:22:30 PM

people

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 exceed 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

Java Multi Threading, Atomic and volatile variable

I'm testing volatile variable and AtomicLong types. I have a class as such: package com.thread.methods; import java.util.concurrent.atomic.AtomicLong; public class...
Jon Skeet
people
quotationmark

You're not waiting for your threads to finish before you print out the result. Simply change your main method to join both threads after you've started them: public static void main(String[] args) throws InterruptedException { ... more 2/23/2017 3:20:48 PM

people

Log4j, meaning of Append = true / false

log4j.appender.LOGFILE.Append = true I've googled, the doc says If the append parameter is true, the file will be appended to. Otherwise, the file designated by filename will...
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