Browsing 7239 questions and answers with Jon Skeet

Get specific Data from inside a HashMap

First of all I must apologies for I am uncertain how to word my title well. However the problem I am facing is a continuation of another question that brought me one step closer...
Jon Skeet
people
quotationmark

Well instead of relying on the default toString() implementation of HashMap, just loop over the entries: for (Map.Entry<String, Long> entry : nameSumMap.entrySet()) { System.out.println(entry.getKey() + ": $" +... more 7/8/2015 8:42:16 PM

people

How to deserialize multiple JSON objects in C#?

I'm passing multiple JSON objects from my frontend to a C# backend - how can I deserialize them into C# classes so they can be used later on in my application? Before I go any...
Jon Skeet
people
quotationmark

You're trying to deserialize to a single form here: var forms = JsonConvert.DeserializeObject<Form>(Request.Params["formJson"]); Just change it to: var forms =... more 7/8/2015 1:24:35 PM

people

How to order IGrouping without changing its type?

I have an oject of the type IGrouping and would like to order the elements inside the group without changing the type of the object. In other words, I have var tmp =...
Jon Skeet
people
quotationmark

If you can, just put the ordering earlier, e.g. var states = SimulationPanel.innerPanel .Children .OfType<StateBar>() .Where(x => x.isSensorState()) .OrderBy(x => (decimal)(x.Margin.Left /... more 7/8/2015 8:50:12 AM

people

Converting a StreamReader to a Stream

The Deserialize method in protobuff-net takes in a Stream as a argument. Before I used to use FileStream and pass that and it worked , but now I have to use StreamReader cause I'm...
Jon Skeet
people
quotationmark

You can't, logically - a StreamReader is for text data, whereas protobuf data is binary data. It's not clear where you're getting the StreamReader from, but you should look for APIs designed for binary data instead. In practice, you may... more 7/8/2015 8:24:00 AM

people

error in Connection String in C#

I am new to C#. I want to use my Microsoft SQL Server database file test.mdf in my output software in C#. In the past, I had just copied the connection string in Visual Studio...
Jon Skeet
people
quotationmark

As the error message says, you can't use the value of one instance field when initializing another. You probably don't want dir as a field anyway. Just move all of this into the body of the constructor... or ideally, only create your... more 7/8/2015 8:04:34 AM

people

NoSuchMethodException when instantiating a class using newInstance()

I'm using the following code to instantiate every class in a package: for (Class listenerClass : ClassGetter.getClassesForPackage(this, "cullan.listener")) { ...
Jon Skeet
people
quotationmark

The problem is that you're not trying to construct an instance of AgilityListener - you're trying to construct an instance of the anonymous inner class created in AgilityListener.startTimer. That's why the stack trace has: Caused by:... more 7/8/2015 5:30:11 AM

people

reference variable used in ternary conditional? (ex: var?this:null)

Is there a way to reference the first part of a ?: statement in groovy? For example, is there any way to shorten def time = map.get('time') ? map.get('time').get('milliseconds')...
Jon Skeet
people
quotationmark

It sounds like you just want to use the safe navigation operator: def time = map.get('time')?.get('milliseconds') If map.get('time') returns a null reference, the result of the overall expression will be null, and get('milliseconds')... more 7/7/2015 7:17:10 PM

people

Where is the instance of "IntConverter" stored?

Let's suppose we have the following program: public class Program { private static Dictionary<Type, Func<object, object>> converters = new Dictionary<Type,...
Jon Skeet
people
quotationmark

Firstly, it's worth being clear that your question doesn't actually involve expression trees at all - your lambda expression is just being converted into a delegate. Now, that lambda expression is this: (obj) =>... more 7/7/2015 2:07:33 PM

people

Why TcpClient.Connect is not throwing exception in async method even if it is running synchroneously

Why this code is not throwing System.Net.Sockets.SocketException even if no await is specified? (no server is listening at specified port ) Why Thread.Sleep(4000); is not...
Jon Skeet
people
quotationmark

An async method never throws an exception directly. Instead, if an exception is raised within the body of the method, the task returned by the async method becomes faulted. The sleep isn't executed because the exception does prevent the... more 7/7/2015 1:21:37 PM

people

How do you get the sum of the product of two properties in an IEnumerable using LINQ?

I have an IEnumerable<dynamic> from an SQL query using Dapper, and I would like to add together the product of two properties of the dynamic objects in the IEnumerable. I...
Jon Skeet
people
quotationmark

I would use Sum instead of Aggregate: decimal total = orderDetails.Sum(x => (decimal) (x.quantity * x.unitPrice)); Depending on exactly what your situation is, I can imagine this potentially working without any casts, or needing more... more 7/7/2015 1:18:54 PM

people