Browsing 15711 questions and answers with Jon Skeet

Java Compartor.comparing() using public field?

I would like to sort a data collection based on a public field. This syntax works for sorting based on a method: history.sort(Comparator.comparing(HistoryItem::getDate)); I...
Jon Skeet
people
quotationmark

The syntax you showed uses a method reference. You can just pass in a lambda expression instead to create the Function<,>: history.sort(Comparator.comparing(item -> item.date)); (I'd question the wisdom of using public fields... more 6/9/2017 4:45:02 PM

people

Java error when calling a non static method from a non static method, saying that the non static method is being called from static context

Eclipse keeps giving me an error saying non-static method mouseMove(int,int) cannot be referenced from a static context The error is in the following method, in the...
Jon Skeet
people
quotationmark

The "static context" here is the fact that you're trying to invoke it with the class name, not an instance of the class. It's not a matter of whether or not this is being called from a static method. It's an equivalent problem to this... more 6/9/2017 4:18:52 PM

people

How to parse xs:date with NodaTime?

I have to process XML documents sent by various external systems that also contain some dates. xs:date allows adding a "time zone", i.e. an offset, to a date. E.g....
Jon Skeet
people
quotationmark

I would parse the value as OffsetDateTimePattern as that most closely represents the information you actually have in the text. My experience is that it's best to parse in a form that retains all the information you need, then use the Noda... more 6/9/2017 5:54:47 AM

people

C# executing code using condition ? method call : method call

I'm trying to add to one list or another based on a condition using ? : syntax, is this possible in c#, the syntax I was using does not compile List<int> Negatives = new...
Jon Skeet
people
quotationmark

The conditional operator evaluates one expression or other to compute a value. It doesn't execute void-returning methods. You can use the conditional operator here though, to decide which list to add to: (number >= 0 ? positives :... more 6/8/2017 9:44:20 PM

people

C# notation understanding Select(int.Parse)

I found a little script that I understand fully. I've got a string with "1 -2 5 40" for example. It reads the input string, splits it into a temporary array. Then this array is...
Jon Skeet
people
quotationmark

int.Parse is a method group - what you're seeing is a method group conversion to a delegate. To see it without LINQ: Func<string, int> parser = int.Parse; int x = parser("10"); // x=10 It's mostly equivalent to: Func<string,... more 6/8/2017 11:40:20 AM

people

How to use single object instance for class to set the multiple member value?

Curretly i am using below code to set the DateRanges Class member variables. DateRanges DateRanges1 = new DateRanges(); DateRanges DateRanges2 = new DateRanges(); ...
Jon Skeet
people
quotationmark

To make this code cleaner, I'd suggest: Renaming Lastmonth etc to follow .NET naming conventions Not bothering with any separate variables (but if you do, make them follow .NET naming conventions too) Renaming DateRanges to DateRange (as... more 6/8/2017 10:15:33 AM

people

Java doesn't complain when I don't declare on exception

public static boolean isAlive() { final String METHOD_NAME = "isAlive"; return restTemplate.getForObject(SERVICE_URL.concat(METHOD_NAME), boolean.class); ...
Jon Skeet
people
quotationmark

RestClientException is a subclass of RuntimeException, therefore it's not a checked exception: callers don't need to handle it or declare it. As a tiny example: class CheckedException extends Exception {} class UncheckedException extends... more 6/8/2017 8:49:45 AM

people

How much memory is allocated for static primitive type and static reference type in Java

I have two different class with static variables. One class with static reference type variables and another class with static primitive type variables. When i calculated the size...
Jon Skeet
people
quotationmark

getObjectSize() is meant to give an approximation of the size of objects of the specified type. They aren't affected by static fields, because static fields exist on a per-type basis rather than a per-object basis. Add as many static... more 6/8/2017 8:46:03 AM

people

How to get response form Oracle using C#?

I'm new in Oracle and trying to execute the next SQL request using C# try { connection.Open(); OracleCommand cmd = new OracleCommand(); cmd.Connection = connection; ...
Jon Skeet
people
quotationmark

Given that you're querying count(*), you'd be better using ExecuteScalar rather than ExecuteReader(), for one thing. Next, the result will be an integer, which is why GetString() fails. Change it to: int count = (int)... more 6/8/2017 8:44:35 AM

people

How can I convert float to int?

cube1.name = string.Format("Terrain_{0}_{1}", (int)Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x, ...
Jon Skeet
people
quotationmark

You've just got a precedence problem. You've got: (int) x + y which is equivalent to ((int) x) + y ... which will then promote the result of the cast back to a float in order to perform floating point addition. Just make the cast... more 6/7/2017 1:42:49 PM

people