Browsing 7239 questions and answers with Jon Skeet

How To Convert C# Class Properties Into Json File

Iam trying to create jsonObject and json array by using the c# class and its properties containing values. Please see the complete code below. public class Product { ...
Jon Skeet
people
quotationmark

The thing to do is take look at the JSON you want. It's an object, with a Product property, which is an array of product objects. So the simplest way to do that is create a class modelling that: public class JsonRoot { ... more 10/10/2016 6:26:08 AM

people

Why are the results of of str == str.intern() for StringBuilder using append or not different?

All.I have a java code snippet like this: String a = new StringBuilder("app").append("le").toString(); System.out.println(a.intern() == a); String b = new...
Jon Skeet
people
quotationmark

In both cases, StringBuilder.toString() creates a new string. In the first case, String.intern() finds that there's no string "apple" in the intern pool, so adds the provided one to the pool and returns the same reference - which is why... more 10/8/2016 7:14:47 AM

people

Understanding TimeUnit

I took a look into TimeUnit and toMillis() method. public long toMillis(long paramLong) { throw new AbstractMethodError(); } toMillis() method do nothing other than...
Jon Skeet
people
quotationmark

Each value within TimeUnit overrides it, basically. You never end up calling that implementation, because you never have a reference to an object of type TimeUnit - it's always a subclass representing one of the values. more 10/5/2016 3:50:53 AM

people

How to use C# LINQ Union to get the Union of Custom list1 with list2

I am using the Enumerable.Union<TSource> method to get the union of the Custom List1 with the Custom List2. But somehow it does not work as it should in my case. I am...
Jon Skeet
people
quotationmark

The oddity here is that your class implement IEqualityComparer<CustomClass> instead of IEquatable<CustomClass>. You could pass in another instance of CustomClass which would be used as the comparer, but it would be more... more 10/4/2016 11:54:34 AM

people

Java ArrayList size gets enormous when adding objects

I have a problem when I'm adding Entity objects to my ArrayList (here called entities). Each Entity-object has a long ID, int x-position and y-position First I check if the list...
Jon Skeet
people
quotationmark

The problem is that you're adding a new entry into the list every time you find an entry which doesn't have the same ID... your add call is inside your loop, and it shouldn't be. Basically, you should have something like: public void... more 10/2/2016 5:43:41 PM

people

How to handle null compare method arguments in Comparator?

I have created an implementation of Comparator<Entity>, but when I use this comparator to sort an Array<Entity>. I will receive an java.lang.NullPointerException,...
Jon Skeet
people
quotationmark

You can't "skip" the comparison. What would you expect the sorting code to do? You've got to provide it with a result. Two options are common: Throw a NullPointerException to indicate that you just don't support comparing null values.... more 10/1/2016 7:45:35 AM

people

Count number of including weeks between 2 dates

I need to get the number of weeks between 2 dates. A week for me is monday to sunday. So if the first date is on a saturday than this week should be included. if the second date...
Jon Skeet
people
quotationmark

The simplest way is probably to write a method to get the start of a week. Then you can subtract one date from another, divide the number of days by 7 and add 1 (to make it inclusive). Personally I'd use Noda Time for all of this, but... more 9/30/2016 9:49:50 AM

people

Abstract Classes & Constructors

If I have an abstract class called Employee having a constructor: public abstract class Employee { //instance fields or attributes of employee public Employee(String...
Jon Skeet
people
quotationmark

You just write a constructor which is able to provide the name and extensionNumber values to the superclass constructor, and do whatever else you like. I would personally make the Employee constructor protected as well, given that it... more 9/29/2016 7:39:57 AM

people

trying to implement observer by using delegate i get the error : cannot implicitly convert type 'void' to the class where my delegate belong to

// subject class SafeMode { private bool m_enable = false; public bool Disable() { return m_enable; } public delegate void...
Jon Skeet
people
quotationmark

Look at this statement: m_safer.DiableCommands += curr.DiableFunctionality(); That's trying to invoke curr.DiableFunctionality and then subscribe the result of the method call to the m_safer.DiableCommands event. You don't want to... more 9/28/2016 8:38:14 AM

people

Unique elements inside each child element of XML based on condition

My requirement is that there should be only one listingagent TAG for each role_type Primary inside each commercial tag....
Jon Skeet
people
quotationmark

If your constraint is that there should be exactly one listingAgent element within each commercial element, that's very simple: var broken = doc.Descendants("commercial") .Any(c => c.Elements("listingAgent").Count() !=... more 9/28/2016 6:16:48 AM

people