Browsing 7239 questions and answers with Jon Skeet

Custom Contains for List<ReferenceObject> c#

I'm trying to use List.Contains in a List My objects to compare come from a Service Reference in C# and their Equals method doesn't suit my needs. I've been looking into...
Jon Skeet
people
quotationmark

It sounds like you just want to implement IEqualityComparer<ActivitiesActivity>: public class ActivitiesActivityEqualityComparer : IEqualityComparer<ActivitiesActivity> { public bool Equals(ActivitiesActivity x,... more 3/19/2015 1:56:58 PM

people

Get month and year from Joda's LocalDate

From a LocalDate I want to get only the month-year string. E.g “March 2015” Right now I do it as follows: myDate.monthOfYear().getAsText(LOCALE) + " " +...
Jon Skeet
people
quotationmark

Yes - just create an appropriate formatter: DateTimeFormatter formatter = DateTimeFormat .forPattern("MMMM yyyy") .withLocale(LOCALE); String text = formatter.print(myDate); more 3/19/2015 1:30:42 PM

people

Async and await: multiple await expressions

I'm having a little difficulty understanding how use async and await work. I understand that when an async method hits an await expression, the method returns immediately and at...
Jon Skeet
people
quotationmark

I understand that when an async method hits an await expression, the method returns immediately and at some point in the future the await expression returns and the method continues to execute. Yes, if the awaitable hasn't already... more 3/19/2015 11:39:46 AM

people

Java abstract method with generic argument

I have the following situation: There is abstract Class (abs) and two classes which extend from abs (con1 and con2) Now I want abs to have an abstract function with an argument...
Jon Skeet
people
quotationmark

No, basically that doesn't implement the method. With the declarations you've got, I should be able to write: abs x = new con1(); abs y = new con2(); x.foo(y); It sounds like you want: public abstract class Abstract<T extends... more 3/19/2015 11:35:25 AM

people

Prevent calling base class implemented interface method in the derived class C#

Is it possible to implement an interface in a base class and allow calling/overriding the implemented method in the first derived class level but prevent calling it from any...
Jon Skeet
people
quotationmark

No, this isn't possible - it would break the whole point of polymorphism. In particular, imagine you didn't use var, but used the types explicitly: Sub1 s2 = new Sub2(); s2.Test(); That has to compile: The first line has to compile... more 3/19/2015 10:54:12 AM

people

Regarding conditional operator '?' , can some one explain this to me, in simple if else format?

Regarding conditional operator '?' , can some one explain this to me, in simple if else format? AutoFFSuccess, ActSuccess, FUPSuccess are bool values. char StatusCode =...
Jon Skeet
people
quotationmark

I would normally format that like this: char statusCode = AutoFFSuccess ? ActSuccess ? 'P' : 'W' : FUPSuccess ? ActSuccess ? 'F' : 'G' : 'E' Or: char... more 3/19/2015 10:20:46 AM

people

Filewriter will not append text to newly created file

I'm fairly new to Java. I was trying to add "List:" to the beginning of a new text file if it doesn't exist. Instead, the text file is blank, with the input below a line of blank...
Jon Skeet
people
quotationmark

The problem is with this line: new FileWriter(hi).append(hello); You're not closing the writer, which means: The file handle is potentially still open, which could cause problems when you try to write to it You're not flushing the... more 3/19/2015 9:40:36 AM

people

Why is Distinct not working in this LINQ query?

In the query below, the Distinct call doesn't seem to be doing anything: using (var db = new AccountsDbContext()) { var uniques = db.AccountMappings .Select(m =>...
Jon Skeet
people
quotationmark

Your comparer is broken - two objects which are equal don't necessarily return the same hash code. Equality and hash code generation have to be consistent with each other. You can fix this using something like: public int... more 3/19/2015 8:14:15 AM

people

Copying HashMap to another HashMap

I am having a problem copying a HashMap A to HashMap B. B is always same as A. My idea is making a small tile game using HashMaps only. Map<Point,Tile> A = new...
Jon Skeet
people
quotationmark

However, when I changed tile (1,1) to ON on HashMap A , it updated HashMap B as well. When you write: Tile t2 = A.get(new Point(1,1)); t2.setS("ON"); you're not changing either of the maps. The maps just have references to Point... more 3/19/2015 7:59:55 AM

people

How can I verify an exception was thrown with a certain parameter?

Suppose I have the following exception and method: public MyException(string type) { /* Does soemthing with the parameter */ } public void DoSomething() { // ... if...
Jon Skeet
people
quotationmark

I'd usually go with [ExpectedException(typeof(MyException)] I suggest you don't do that. You haven't told us which unit test framework you're using, but these days most provide something like: Assert.Throws<MyException>(()... more 3/19/2015 7:34:12 AM

people