Browsing 7239 questions and answers with Jon Skeet

Testing string and integer arrays against user input

I have created arrays for strings and integers I want to use in my program and I want to use them instead of using (name.equals "barry"||"matty" for example. I want to know...
Jon Skeet
people
quotationmark

I think you're just looking for List.contains - but that requires a List rather than an array, of course. There are two obvious options here. Firstly, you could use a List<String> to start with: List<String> names = new... more 3/29/2014 1:50:30 PM

people

Start index is less than zero, if all the checkbox is not checked in checkbox list

StringBuilder sbOccupation = new StringBuilder(); foreach (ListItem li in cblOccupation.Items) { if (li.Selected) { sbOccupation.Append(li.Text); ...
Jon Skeet
people
quotationmark

If nothing has been appended to sbOccuption, then it will be empty - so LastIndexOf(',') will return -1, which isn't a valid first argument to string.Remove. Given that you know it will always be the last character of the result, you... more 3/29/2014 1:38:33 PM

people

Static Field Initialization

private readonly CoreDispatcher dispatcher; I am initializing above field in constructor to CoreWindow.GetForCurrentThread().Dispatcher; Then I wrapped a function around...
Jon Skeet
people
quotationmark

Well, you're initializing the CoreDispatcher based on whichever thread happens to initialize the class. Given that you really want it to be done for a specific dispatcher, this feels like it's a bad idea to me. Given that you're using... more 3/29/2014 1:14:08 PM

people

What is the difference between ifne and ifnonnull?

I've just read through Java bytecode instruction listings and wondered: What is the difference between ifne and ifnonnull in Java Bytecode? I know that in a high level language...
Jon Skeet
people
quotationmark

The JVM specification is perhaps clearer on this. So for ifne (and ifeq etc) the restriction is: An ifeq instruction is type safe iff one can validly pop a type matching int off the incoming operand stack yielding the outgoing type... more 3/29/2014 1:06:52 PM

people

Removing an object from a list in a foreach loop of that list

I have made a method that eliminates any replicates of the same string in a List. now, the problem is that it gives me this error: System.InvalidOperationException: Collection...
Jon Skeet
people
quotationmark

Firstly, as noted in comments, LINQ has got you covered here: list = list.Distinct().ToList(); It's well worth looking into LINQ for data operations - it can make things much simpler. As for what's wrong with your current code - there... more 3/29/2014 10:47:19 AM

people

XmlSerializer.serialize() creates only header

When I'm trying to serialize my list of customers i get file <?xml version="1.0"?> <Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
Jon Skeet
people
quotationmark

Your variables are all private. XmlSerializer can only serialize public properties and fields. From the documentation: XML serialization is the process of converting an object's public properties and fields to a serial format (in this... more 3/29/2014 10:31:14 AM

people

Insert multiple rows of data into the database wp8

I know how to insert data into a database one by one but I want to insert multiple pieces of data into database and I don't know how to do this. Here is my code for a "one-time"...
Jon Skeet
people
quotationmark

Well you need to create multiple objects. You can either add them one at a time to the list of pending changes: var bloggs = bloggerDB.bloggers; bloggers.InsertOnSubmit(new Bloggers { Interest = "wy", Name = "opwn", TotalPosts = 1... more 3/29/2014 8:32:34 AM

people

How to check and see if a number contains a decimal?

I'm trying to create a pretty basic calculator application. I'm running into a problem with decimals. I'm pretty new to C#, so I don't really know what to do here. When I click on...
Jon Skeet
people
quotationmark

Well it sounds like you're actually wanting to test whether a string already contains a decimal point - not a number. Assuming you're not trying to internationalize this, you probably just want: if (textBox.Text.Contains(".")) { //... more 3/29/2014 7:50:40 AM

people

Object[] cannot not be converted to String[] in jsp

<% ArrayList aList=(ArrayList)request.getAttribute("read"); String[] write =aList.toArray(new String[aList.size()]); %> in above code, i am getting ...
Jon Skeet
people
quotationmark

The problem is that the type of aList is the raw type ArrayList. That means all trace of generics is removed from it - even from the generic method toList. It's simple to fix though - just use ArrayList<?>... more 3/29/2014 7:38:09 AM

people

getResource isn't working exported in a jar

So getResource does not work in a jar, or as I have it in my code it is returning the children of the file to null when I ask to list its files. In eclipse it works perfectly...
Jon Skeet
people
quotationmark

getResource works perfectly well - but you're then calling the File constructor, and the resource doesn't exist as a File. It's not a file - it's a resource within a jar file. I suspect you'll find it hard to get it to list the resources... more 3/28/2014 9:49:26 PM

people