Browsing 7239 questions and answers with Jon Skeet

function that recalls itself upon a catch of non critical error

I got a function named inner_logic() that can throw many types of exception , each of one of two types: critical and non-critical. The logic of inner_logic() includes an...
Jon Skeet
people
quotationmark

It feels to me like it should just be looping: while (true) // Or probably have a maximum number of retries { try { // Logic // Break out of the loop by returning if we succeed return; } catch... more 11/21/2013 1:59:41 PM

people

Is there a more elegant approach toward draining elements from one collection to the other?

Based on a condition, certain elements should be drained from one collection to the other. To avoid concurrent modification, my current approach is (pseudo...
Jon Skeet
people
quotationmark

You can use an iterator for this: for (Iterator<String> iterator = source.iterator(); iterator.hasNext(); ) { String element = iterator.next(); if (shouldDrain(element)) { destination.add(element); ... more 11/21/2013 1:57:28 PM

people

How to check if a day is a certain date in asp.net c#.net

I would like to check, if a rendered day in a webcalendar-element is X-mas Eve or the first of January or another date of a year and if so, colour that date differently. So if...
Jon Skeet
people
quotationmark

It's not really clear what you mean by "do kinda the opposite" but: static IsThirdMondayInMay(DateTime date) { // The first X in a month is always in the range [1, 8) // The second X in a month is always in the range [8, 15) ... more 11/20/2013 12:41:41 PM

people

The type 'X' does not exist in the type 'X.X' error

The project structure is here. How can I modify the project to compile? What are the best practices in organizing a project structure?
Jon Skeet
people
quotationmark

Just don't name a type the same as its namespace. That's always a bad idea. You can get round the issues in various ways, but it's ugly in many cases. Renaming either the type or the namespace would be much better. In this particular... more 11/20/2013 12:10:11 PM

people

When does BufferedWriter create file?

Using BufferedWriter.write() when is a file created? I know from the docs that when the buffer is filled it will flush to file, does this mean that: every-time the buffer is...
Jon Skeet
people
quotationmark

Using BufferedWriter.write() when is a file created? Never. BufferedWriter itself just writes to another Writer. Now if you're using a FileOutputStream or a FileWriter (where the first would probably be wrapped in an... more 11/20/2013 11:42:49 AM

people

Difference between two dates in C#

In a senario I need to find out the difference between two dates.For example Start date is 01/01/2012 and To Date is 01/10/2013 .I need to get the output as 1.9 years. int...
Jon Skeet
people
quotationmark

My Noda Time library is built for exactly this sort of thing: LocalDate start = new LocalDate(...); LocalDate end = new LocalDate(...); Period period = Period.Between(start, end); Console.WriteLine("{0} years, {1} months, {2} days", ... more 11/20/2013 11:29:37 AM

people

simpledateformat parse not parsing date properly

i am trying to print the date as : 2013/11/20 08 30 but it is printing as below after parsing the date through simpledateformat Wed Nov 20 14:00:00 IST 2013, here i passed the HH...
Jon Skeet
people
quotationmark

after parsing the date through simpledateformat Wed Nov 20 14:00:00 IST 2013, here i passed the HH and MM as 08 30 but after parsing it changed to 14:00:00 Yes, because you specified that you wanted it to be parsed as a UTC value.... more 11/20/2013 5:21:34 AM

people

ReferenceEquals and Nullable<T>

While writing a unit test, I wanted to use Assert.AreSame(..) against a Nullable<T> type and I got unexpected results. Then I realized that the below code fails: int? k =...
Jon Skeet
people
quotationmark

What is happening here? Both arguments are being boxed, to different objects. Imagine your code is actually this: int? k = 10; object x = k; // Boxing operation 1 object y = k; // Boxing operation 2 Assert.IsTrue(ReferenceEquals(x,... more 11/20/2013 5:00:15 AM

people

Assigning bool? to bool

Consider the following code: bool x; bool? y = null; x = y?? true; Assigning a bool? to a bool is a compile-time error, but the above code succeeds both at compile and...
Jon Skeet
people
quotationmark

The type of this expression: y ?? true is bool, not bool?. From section 7.13 of the C# 5 spec: The type of the expression a ?? b depends on which implicit conversions are available on the operands. In order of preference, the type... more 11/20/2013 4:39:36 AM

people

Constructor is Undefined issues

I'm currently writing a program that should read through a text file, create an array of different types of employees (each have their own subclass), then print out the...
Jon Skeet
people
quotationmark

Well yes - look at your constructor, including the clearly-inaccurate comment: // four-argument constructor public SalariedEmployee(String first, String last, String ssn, Date DayOfBirth, String ID, double... more 11/20/2013 3:44:20 AM

people