Browsing 7239 questions and answers with Jon Skeet

Validate octal number

Is this a valid way to validate if a string contains only octal numbers? public static bool IsOctal(String toCheck) { if (toCheck.Contains(" ")) { return false; ...
Jon Skeet
people
quotationmark

It's not at all clear what your current approach is trying to achieve, as it's converting in both decimal and octal. If you just want to check whether the string only contains digits 0-7, it's probably simplest to use a regex: using... more 8/13/2014 8:25:38 PM

people

Months between two integer dates

I have 2 dates as integers. How can i find the month difference between these two integers in c#? For example: Int32 beginDate= 20130307(yyyymmdd) Int32 beginDate=...
Jon Skeet
people
quotationmark

You can use my Noda Time library for that. It was created specifically to make things like this simpler. // TODO: Encapsulate this conversion in a separate method LocalDate start = new LocalDate(beginDate / 10000, ... more 8/13/2014 4:02:09 PM

people

C# Extension Method Return Value Not Setting Variable

Why doesn't this extension method set the value it's applied to? public static byte ExtSetBits(this byte original, byte value, byte bitSize) { unchecked { original &=...
Jon Skeet
people
quotationmark

Am I missing something? Yes - you're not using the return value of the method in your first snippet. All you need is to set the return value, just as you're doing in the working case (your second code snippet). Changing the value of... more 8/13/2014 2:41:05 PM

people

Date formatting not working

I am trying to convert the string to date and i want that date to be in this format 'yyyy-MM-d HH:mm:ss' and i no how to get this format in string my question is i want to get...
Jon Skeet
people
quotationmark

i want to get Date in above format not as string but as 'Date '? You're asking for a Date in a particular format - that's like saying "I want an int in hex format." A Date doesn't have a format - it's just an instant in time. It... more 8/13/2014 1:40:20 PM

people

Couldn't save an Integer in a Object Array

I want to parse an String from an Object[] into an Integer and save it at the same place like this: public class ArrParseTest { public static void main(String[] args) { ...
Jon Skeet
people
quotationmark

This is the problem causing the immediate issue you're seeing: arr = input.split(";", -1); You're assigning a reference to an object of type String[] to a variable of type Object[]. That's fine, but it means you can't store non-string... more 8/13/2014 12:01:19 PM

people

Same date different long time

i've a object Date as for example this date "Fri Aug 01 00:00:00 CDT 2014" that through this validation: Date fechaInicio = filtros.containsKey("dateFechaInicio") ?...
Jon Skeet
people
quotationmark

You're only setting values down to the second, so the millisecond part is left at whatever it was from new Date(). Your description isn't entirely clear, but I suspect you just want: fechaMin.set(Calendar.MILLISECOND, 0); Alternatively,... more 8/12/2014 9:17:30 PM

people

What to do with never happening, useless exceptions?

I have this code: HttpPut put = new HttpPut(url); try { put.setEntity(new StringEntity(body, "UTF-8")); } catch (UnsupportedEncodingException e1) { // That would really...
Jon Skeet
people
quotationmark

I would throw some kind of RuntimeException which indicates that you think this really should never happen. That way: If it ever does happen, you find out about it rather than swallowing it and continuing Your expectation that it really... more 8/12/2014 8:21:55 PM

people

Dealing with different xml formats for the same data with using Xml.Linq;

I have all these XML records that are kind of written in two styles, they both contain the same information. I'm new to LinQ but I thought this expression would just fine...
Jon Skeet
people
quotationmark

Is there a single query that can get the name field from both of these styles of XML? Yes. List<string> allNames = MyDocument.Descendants("img") // Look for a name attribute first; if there isn't one, find the name //... more 8/12/2014 4:39:14 PM

people

Check if an object is of the same type as the passed one

I have class A and two classes B and C which extend A. I have an array list which contains multiple instances of all of these classes. Can I have a method which takes in an object...
Jon Skeet
people
quotationmark

Sure - you'd use getClass() to find the execution-time type: private final List<A> allKnownValues = ...; List<A> getAllValuesOfTheSameType(A sample) { ArrayList<A> results = new ArrayList<>(); for (A... more 8/12/2014 2:33:48 PM

people

Get returnvalue in xmlcomment

As a part of StyleCop, I am examining method-documentation. See the following: /// <summary> /// Copies the node. /// </summary> /// <param...
Jon Skeet
people
quotationmark

I know this seems a bit dirty, but I'd do: string xml = "<root>" + text + "</root>"; XDocument doc = XDocument.Parse(xml); XElement returns = doc.Root.Element("returns"); if (returns != null) { string returnsDescription =... more 8/12/2014 2:04:53 PM

people