Browsing 7239 questions and answers with Jon Skeet

Any chance of Object auto casting to Integer?

I am wondering whether the below code should work in any scenario? Object value = attValue.getValue(); // Returns an Object, might contain an Integer if (value instanceof...
Jon Skeet
people
quotationmark

Yes, that's entirely feasible given the way autoboxing is guaranteed to work on small values, and is permitted to work on larger values. For example, this is guaranteed to print true: Object x = 5; Object y = 5; System.out.println(x ==... more 10/16/2015 4:20:45 PM

people

Why do many of the methods in the Java API have "abstract" modifiers?

Student level question, I'm hoping stackOverflow can help me gain a deeper understanding here: I've noticed that many of the methods (such as those in Graphics2D, for example) in...
Jon Skeet
people
quotationmark

So, how is it that I'm able to import a class like Graphics2D, create an instance of it and use its abstract methods? Because you never create an instance of Graphics2D directly. Look at your code - you won't find this: Graphics2D... more 10/16/2015 2:53:15 AM

people

How to iterate throgh XAttribute of XElement in C#?

I have the following XElement structure: XElement xmleElement= <portal> <form patientid="72615" consentformid="430" appointmentid="386919" actiontype="3"> ...
Jon Skeet
people
quotationmark

Sounds like you want something like: var encodedSignatures = doc.Descendants("signature") .Select(x => x.Attribute("encodedSignature").Value; Or to be more explicit about the path: var encodedSignatures =... more 10/16/2015 2:14:03 AM

people

How to make an enum isXXX() method?

How would I insert isRED() and isBLACK() methods into this enum? I can't figure it out - even after googling for some time.. I don't know what value to access. enum Suit { ...
Jon Skeet
people
quotationmark

The simplest approach would probably be to have a boolean field indicating whether or not that suit was red. For example: enum Suit { SPADES(false), HEARTS(true), DIAMONDS(true), CLUBS(false); private final boolean red; ... more 10/15/2015 9:24:47 PM

people

Linq with group by returning count 1 when when it should be 0

I'm having a bit of a trouble with a linq query which counts the group by returns. For results that returns 0 in the MSSQL query, it's returning 1. Here is the LINQ: from qs in...
Jon Skeet
people
quotationmark

It sounds like you need to make two changes to fix this part - and another to fix ordering. If you want to find the count of the elements where the left join actually matched something (rather than "using" the DefaultIfEmpty call) you... more 10/15/2015 7:53:39 PM

people

Why does String.Format work but SqlCommand.Parameters.Add not?

I have a Project table with two columns -- ProjectId and ProjectName -- and am writing a function that constructs and executes a SqlCommand to query the database for the ids of a...
Jon Skeet
people
quotationmark

Basically parameters in SQL only work for values - not identifiers of columns or tables. In your example, only the final parameter represents a value. If you need to be dynamic in terms of your column and table names, you'll need to build... more 10/15/2015 3:53:34 PM

people

How does the values of an array changes when we passed it as an argument to other function?

When i passed an array as an argument to the function, the original array gets changed, but the array should not get changed right? please correct me if am wrong. below i passed...
Jon Skeet
people
quotationmark

You're wrong. What you're passing in isn't the array - it's a reference to the array. Arrays are reference types in Java, so the value of a1 (for example) isn't an array object - it's a reference to an array object. When you pass that... more 10/15/2015 4:30:20 AM

people

How to check whether a SemaphoreSlim can be entered?

I want to assign a list of objects in a SemaphoreSlim fashion, e.g. suppose each object can be used by two threads at the same time. My plan is to create a SemaphoreSlim class for...
Jon Skeet
people
quotationmark

I suspect you can use Wait(TimeSpan.Zero) to say "try to acquire the semaphore, but abandon the attempt if you can't do so immediately." That would be my expectation, but the documentation doesn't explicitly talk about what happens if you... more 10/15/2015 4:18:12 AM

people

Can't create multiple instances of a class in Java

I'm trying to create multiple characters(squares) on the screen that move around randomly. I have already created a CharMove class that creates a square, and moves it around...
Jon Skeet
people
quotationmark

However, I tried creating multiple instances of this class in a seperate java file and only 1 instance was created. Nope, you're creating multiple instances. However, that doesn't make any difference because you don't have any... more 10/14/2015 10:54:40 PM

people

The method *****is undefined for the type***** in Java

I'm trying to call a method from an abstract class Sprite in another package, but I got "The method getSymbol() is undefined for the type Sprite" Here's the code. And here is...
Jon Skeet
people
quotationmark

This is the problem: public class ArrayGrid<Sprite> implements Grid<Sprite> The way you've declared the class, Sprite is the name of a type parameter. You've made this a generic class, and I suspect you didn't mean to.... more 10/14/2015 10:33:41 PM

people