Browsing 7239 questions and answers with Jon Skeet

Format decimal for use in XML and ignore culture

We need to insert decimal values into a XML document (done by a XML writer) which works fine. First we used XmlConvert.ToString(myDecimalValue) which printed the value with four...
Jon Skeet
people
quotationmark

Assuming this really is decimal, I suggest you round the decimal value first: decimal rounded = Math.Round(original, 2); string roundedText = XmlConvert.ToString(rounded); Obviously you can specify appropriate rounding/truncation... more 6/10/2014 10:36:46 AM

people

Could I define a 'Interface' for 'Class' instead of 'Object' in java?

We know 'interface' in java provide a common way to access objects who implementing it, but i wonder if there's a way like interface which not just for accessing the object's...
Jon Skeet
people
quotationmark

No, there's no kind of "interface for static methods" in Java. (Aside from anything else, how would you expect to specify the class in question? Even generics wouldn't help here due to type erasure.) more 6/10/2014 10:24:31 AM

people

Implementing skip/take pagination via pageIndex/pageSize pagination when skip is not divisible by take

Suppose that I have some magically paginated black box class that retrieves data by using pageIndex and pageSize as follows: public class PaginatedList<T> { // ... ...
Jon Skeet
people
quotationmark

This partly depends on how much you want to balance code complexity vs data fetching efficiency. You could always succeed by fetching twice as much data as you need: int virtualTake = take * 2; int virtualSkip = skip / virtualTake; var... more 6/10/2014 6:02:23 AM

people

How to write an XML file with a for loop?

I am trying to do a quick program that creates a very simple XML file. I want to create an XML file that uses a 'for loop' to create a portion of the XML several times. When I...
Jon Skeet
people
quotationmark

You're currently trying to use a for loop in the middle of a statement. That's not going to work. Options: Use Enumerable.Range to create a range of values, and then transform that using Select: XDocument myDoc = new XDocument( new... more 6/9/2014 7:07:47 PM

people

Is the initialization of a variable a statement or an expression?

class Demo { public static void main(String[] args) { int a; // declaration statement a = 5; // initialization statement/expression? } } I've...
Jon Skeet
people
quotationmark

A local variable declaration - with or without initialization - is a statement, as specified in section 14.4 of the JLS. It's important to note that this is not an expression in the way that a simple assignment expression is - you can't... more 6/9/2014 6:56:47 PM

people

Reading Socket InputStream in a loop when writing byte arrays

This is what you usually do when sending text data // Receiver code while (mRun && (response = in.readLine()) != null && socket.isConnected()) { // Do stuff ...
Jon Skeet
people
quotationmark

Assuming you're trying to handle a stream of messages, sounds like what you're missing is a way of specifying (in the stream) how big your messages are (or where they end). I suggest you just write a prefix before each message, specifying... more 6/9/2014 5:00:17 PM

people

creating absolute URLs that work on both localhost and live site

I want to populate a Literal control with a URL that will work on both my local machine and the live website. For now, this is what I have: string TheHost =...
Jon Skeet
people
quotationmark

Use the fact that you've already got a Uri via the Request property - you don't need to do it all manually: Uri pageUri = new Uri(HttpContext.Current.Request.Url, "/ThePageName"); Then build your tag using that - but ideally not just... more 6/9/2014 4:42:29 PM

people

c# nested classes the type or namespace could not be found

I have a class with nested subclasses: public class listDevicesModel { public int totalCount { get; set; } public Messages messages { get; set; } ...
Jon Skeet
people
quotationmark

You need to specify the nested name: @foreach(listDevicesModel.Device d in Model.devices.device) Or use implicit typing: @foreach(var d in Model.devices.device) Personally I'd avoid using nested classes here anyway, unless you really... more 6/9/2014 11:55:02 AM

people

Parsing a feed in C#

I am having problems parsing a feed in C#. I cannot get the authors of the feeds to change the code so I have to handle it. I have tried passing the feed straight into the...
Jon Skeet
people
quotationmark

EDIT: The reason your current code is failing is pretty simple - you're trying to parse an empty string: string feedAsString = ""; ... var rssFeedAsString = contentReader.ReadToEnd(); rssFeedAsString =... more 6/9/2014 8:35:30 AM

people

sending negative bytes with tcp

I am sending a byte by a TCP connection, when I send a single negative number (like -30 in this example) I get three bytes: Client Side: PrintWriter out = new PrintWriter(new...
Jon Skeet
people
quotationmark

You're using a writer, and you're calling Writer.write(int): Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored. So you've got... more 6/9/2014 5:53:47 AM

people