Browsing 7239 questions and answers with Jon Skeet

Creating fixed length numeric string

I have a certain number of files for which I need the filenames in my program. The files have a fixed naming fashion i.e. (prefix + digits).jpg. For e.g.: head001.jpg,...
Jon Skeet
people
quotationmark

Just use string.Format, with a precision specifier saying how many digits you want: string name = string.Format("tail{0:d6}.jpg", index); See the MSDN documentation for standard numeric string formats for more details. You can build... more 11/12/2014 7:47:06 AM

people

Decimal Truncating adding unlimited zeros

I have an entry field for Xamarin forms. Where I have to store the value in a decimal field. And as per the requirement I have to have the data in ##.#### format. The...
Jon Skeet
people
quotationmark

I've reproduced this - it looks like it's simply a bug in Mono. It's very easily demonstrated: decimal x = 9m; decimal y = x / 10; Console.WriteLine(y); This should be "0.9", but it's actually "0.9000000000000000000000000000". Please... more 11/12/2014 7:22:26 AM

people

Converting from generic type to its constraint interface

I have created a generic class, with the constraint IComparable, as follows: Public Class Foo(Of T As IComparable) Private _f As T Public Sub New(ByVal f As T) ...
Jon Skeet
people
quotationmark

I would expect the compiler to know that type T is any IComparable type... Yes, it knows that - but your Bar constructor is expecting a Foo(Of IComparable) not a Foo(Of T) where T implements IComparable. You want generic covariance,... more 11/11/2014 6:20:11 PM

people

Using a delegate twice inside of a unit test

I have some unit tests where I’m using the DoesNotThrow feature of Nunit. Up until recently, I wasn’t concerned w/ the actual response coming back. However, I recently needed to...
Jon Skeet
people
quotationmark

Just get rid of the first line entirely: var response = new GetMyCollection().GetCollection(request); Assert.Greater(response.MyCount, 0); If new GetMyCollection().GetCollection(request) throws an exception, that will make the test fail... more 11/11/2014 6:05:07 PM

people

Why Java7 introduces AutoCloseable specially?

AutoCloseable is introduced in jdk1.7 and Cloesable is already in jdk1.5. And According to https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html ...
Jon Skeet
people
quotationmark

Closeable is restricted to throw IOException, which may not be appropriate for some closeable but non-IO-bound resources. AutoCloseable is declared to throw Exception, making it more general-purpose. The API for Closeable can't be... more 11/11/2014 1:48:52 PM

people

Confusion about constants in Java

I'm in the middle of studying java programming and I'm a bit confused about constants. From what I've read so far constants are final and cannot be reassigned a new value unlike...
Jon Skeet
people
quotationmark

Calendar.YEAR is just a constant saying which logical field you want to set within the calendar. The aim was to avoid having an API with setYear setDay setMonth ... In retrospect, I'd say this was a spectacularly bad idea - along with... more 11/11/2014 10:44:46 AM

people

Using Linq to XML using C#, how do I find attribute values?

I need to get all activities for specific claim ID = 13 using linq C#, I am trying the below code, but it's not working. XDocument Doc = XDocument.Load(DownloadedPath); var...
Jon Skeet
people
quotationmark

You're currently converting the value to a string but then comparing it with an int. Don't do that - it's not going to work :) It's probably simplest to convert it to an int instead: var Activites = (from e in... more 11/11/2014 9:18:08 AM

people

Ternary operator in actualization of a loop

I am trying to make two loops into one. This Loop should go through the array from the beginning to the end and otherwise. However my increment is not correct. Can anyone help?...
Jon Skeet
people
quotationmark

Personally, I find that very hard to read, as well as invalid (it won't compile) - because you're trying to use the conditional operator as a statement expression, when it's not. Personally, I'd write something like: for (int i = 0; i... more 11/10/2014 7:23:10 PM

people

Trouble with XElement Extension Method

I'm using the below code to pull out info from an XML file. But if node isn't present I get a NullReferenceException error. I thought that wasn't supposed to happen with using...
Jon Skeet
people
quotationmark

The problem is almost certainly not in ElementValueNull... it's that one of the earlier Element calls is returning null. You're calling an instance method (XContainer.Element()) and that's a perfectly ordinary instance method - if it's... more 11/10/2014 7:11:39 PM

people

JAVA JSONObject with prepared data

Sorry, I started teach java few days ago and didn't find a solution in google. I know that I can do this: JSONOblect x = new JSONObject; JSONOblect y = new...
Jon Skeet
people
quotationmark

Assuming you're using org.json.JSONObject, you can use the fact that JSONObject.put returns the value it's called on. For example: JSONObject x = new JSONObject().put("c", new JSONObject().put("a", "b")); You might want to consider... more 11/10/2014 5:12:37 PM

people