Browsing 7239 questions and answers with Jon Skeet

catch exception library not working as expected. What is wrong with this code below?

I have a JUnit 4 Test and I am using https://code.google.com/p/catch-exception/ library @Test public void testInvalidArgument() throws Exception{ verifyException(new...
Jon Skeet
people
quotationmark

I haven't used the library myself, but it seems reasonably clear why your usage would fail: your code for the first statement is equivalent to: Operation op = new Operation("" , "."); verifyException(op,... more 4/27/2014 2:55:01 PM

people

Assigning Java array to non array attribute Serilizable

Why first line of following method compiles while second not? I would expect both fail. import java.io.Serializable; public class ArrayConversions { Serializable serial =...
Jon Skeet
people
quotationmark

The first line compiles because all arrays implement Serializable. From the JLS section 10.8: Although an array type is not a class, the Class object of every array acts as if: The direct superclass of every array type is... more 4/27/2014 11:21:42 AM

people

Weird Thread exception when using Dispatcher.BeginInvoke in a thread

I'm running a pretty time-consuming method in a thread, and one of the things it does, is if there is no image available it sets a grid named Background's background to a solid...
Jon Skeet
people
quotationmark

SolidColorBrush is a dependency object - and you're creating it in the non-UI thread, then trying to use it in the UI thread. Try this instead: Action action = () => { SolidColorBrush scb = new SolidColorBrush(Color.FromRgb(21, 21,... more 4/27/2014 6:38:58 AM

people

Syntax error in the below update command

Could someone please tell me where I am going with the syntax in this update command? CurrentDb.Execute "UPDATE VolunteerDetails" & "SET FirstName=" & Me.frst_Name_txt...
Jon Skeet
people
quotationmark

Your SQL will look something like: UPDATE VolunteerDetailsSET FirstName=Foo, LastName='Bar' WHERE VolsID=10 Three problems with this: You have no space between VolunteerDetails and SET You have no apostrophes around the first name to... more 4/26/2014 3:56:40 PM

people

How to convert property to it's name?

Could anyone help me for this case: public class MyClass { public int MyProperty{ get; set; } private void MyMethod() { // here I wat to get the name of MyProperty. ...
Jon Skeet
people
quotationmark

One option is to use an expression tree: var name = ExpressionTrees.GetPropertyName<MyClass, int>(x => x.MyProperty); ... public static class ExpressionTrees { public static string GetPropertyName<TSource, TTarget> ... more 4/26/2014 7:59:41 AM

people

Break or return from Java 8 stream forEach?

When using external iteration over an Iterable we use break or return from enhanced for-each loop as: for (SomeObject obj : someObjects) { if (some_condition_met) { ...
Jon Skeet
people
quotationmark

Either you need to use a method which uses a predicate indicating whether to keep going (so it has the break instead) or you need to throw an exception - which is a very ugly approach, of course. So you could write a forEachConditional... more 4/26/2014 7:49:19 AM

people

How to group a list of lists using LINQ (ex: List<List<int>>)

I know I can easily do this with some for loops, but wanted to see if there was a way to do it with fluent LINQ. I'm trying to find out how many of each sub-list I have. I was...
Jon Skeet
people
quotationmark

You'd need to implement a IEqualityComparer<List<T>>, which you can then pass into GroupBy. For example: public class ListEqualityComparer<T> : IEqualityComparer<List<T>> { public bool... more 4/26/2014 7:36:15 AM

people

Getting jar specific path as string

My package structure is as follows : src/xxx/ src/yyy/ I have a class in the package src/xxx/ lets call it classA.java and I need to get the path of src/yyy/ how can I do it ? I...
Jon Skeet
people
quotationmark

It's somewhat unclear to me what you're trying to do. But if the file is within a jar file, FileReader isn't going to work anyway - you don't have a file as far as the operating system is concerned. You should just use getResourceAsStream,... more 4/25/2014 4:44:07 PM

people

Elegant way to freeze closures

Is there any elegant way to "freeze" the variables used in an action that is returned from a method? Just have a look at the following code: static void Main(String[] args) { ...
Jon Skeet
people
quotationmark

There's no general way to do this - but if you're only bothered by the specific issue of foreach, then there are two options: Start using a C# 5 compiler (you don't need to target .NET 4.5 or anything like that). The rules around... more 4/25/2014 3:02:10 PM

people

C# Rotate bits to left overflow issue

I've been trying to get this to work for several days now, i've read a thousand guides and people's questions, but still, i cant find a way to do it properly. What i want to do...
Jon Skeet
people
quotationmark

You're getting an overflow exception because you're operating in a checked context, apparently. You can get around that by putting the code in an unchecked context - or just by making sure you don't perform the cast back to byte on a... more 4/25/2014 2:38:31 PM

people