Browsing 7239 questions and answers with Jon Skeet

java.lang.IndexOutOfBoundsException with null arrayList

My code works when tested with an ArrayList that contain objects but puts out the following error when the arrayList is empty: java.lang.IndexOutOfBoundsException: Index: 0,...
Jon Skeet
people
quotationmark

Hi my code works when tested with ArrayLists that contain objects but puts out the error java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 when the arrayList is empty. What am I getting wrong. ArrayList.get() is behaving exactly... more 10/10/2013 9:15:58 PM

people

Select data of a single column of all records using linq command

I want to select data of a particular column of all records using Linq command.Here is my code [HttpGet] public ActionResult DeleteRole(RoleManager model) { ...
Jon Skeet
people
quotationmark

You're trying to select roleName twice - you've already done the right thing by selecting it the first time. You're also overwriting the roleName field/property in the model multiple times, whereas presumably you want to retain all the... more 10/10/2013 12:37:22 PM

people

Using Volatile keyword in C#

I am using the Volatile keyword, but it is behaving someway different than expected. Here's my code private static volatile int _N = 0; var up = Task.Run(() => { for...
Jon Skeet
people
quotationmark

What I get is a different number every time, as if the keyword volatile were not used. Is it the right behaviour of the keyword? Specifying the variable as volatile doesn't make either the increment or decrement operation atomic.... more 10/10/2013 11:28:49 AM

people

Delegate to property

I would like to get a delegate to a property´s set function. This is how I do today: var property = typeof(IApplicationState).GetProperty(propertyName); var action =...
Jon Skeet
people
quotationmark

One simple option which does introduce an extra hop (but would probably have negligible performance impact) would be to just use a lambda expression: Action<IApplicationState, T> action = (state, value) => state.Foo =... more 10/10/2013 10:01:03 AM

people

BinaryFormatter ignore assembly version

I have the following method to generate a hash of an object. It works pretty good! But when I change the version of the assembly, the hash is changing even when the object is the...
Jon Skeet
people
quotationmark

BinaryFormatter.AssemblyFormat is documented as: Gets or sets the behavior of the deserializer with regards to finding and loading assemblies. There's no indication that it has an impact on the serializing path. Personally I would... more 10/10/2013 7:49:13 AM

people

Assign null to decimal using ternary operator

I am using conversion to decimal of a byte array, such that it contains either null or any other number, stored in byte. The problem here is, when I try to convert a null to...
Jon Skeet
people
quotationmark

If you want the result to potentially be null, then you shouldn't be calling Convert.ToDecimal - which always returns decimal. Instead, you should use: x = obj.sal == null ? (decimal?) null :... more 10/10/2013 7:19:19 AM

people

Error Cannot convert from int to boolean

I am facing Error Cannot convert from int to boolean, Here is the code where i am getting this problem. private boolean seeDB() { int i = 1; SQLiteDatabase...
Jon Skeet
people
quotationmark

Well yes, the problem is here: while (true) { return i; } The method is declared to return boolean, but i is declared as an int. That's not going to work. You need to either change the return type to int, or work out when you want... more 10/10/2013 5:43:20 AM

people

Returning a value to caller via chain of methods

Say I call a method. I want a return value from that method. However, this method delegates tasks to other methods, which in turn may delegate tasks to other methods still. The...
Jon Skeet
people
quotationmark

The conditional operator makes each set of choices simple - and if you lay it out correctly, it's really easy to read, once you've got the hang of the pattern: return x ? subMethod1(o) : y ? subMethod2(o) : z ? subMethod3(o) ... more 10/9/2013 8:09:20 PM

people

Windows Phone 8 creating Calendar Application

I am new to creating Windows Phone 8 application. I want to create a Calendar application that tracks my daily activity. I know there is already a build in Calendar application...
Jon Skeet
people
quotationmark

I will have to think of the leap year and also which month has 31 days and which doesn't. You shouldn't need to write any code for yourself which does that. You can either use the existing DateTime type, or potentially use my Noda... more 10/9/2013 3:42:05 PM

people

identify the list of interface a object implements

Is there a way to identify the list of interfaces a object implements. For example: LinkedList implements both List and Queue interfaces. Is there any Java statement that I can...
Jon Skeet
people
quotationmark

It sounds like you're looking for Class.getInterfaces(): public static void showInterfaces(Object obj) { for (Class<?> iface : obj.getClass().getInterfaces()) { System.out.println(iface.getName()); } } For example,... more 10/9/2013 3:28:25 PM

people