Browsing 7239 questions and answers with Jon Skeet

Is try/catch without finally bad

So I know that a finally block of code will execute regardless of an exception, but is it bad to not use it? I've been using just try/catch and wanted to know if this was a bad...
Jon Skeet
people
quotationmark

If you don't have anything you need to clean up at the end, that's perfectly fine. finally blocks are almost always about resource cleanup... and there are plenty of times where you want to catch an exception but don't have any resources... more 10/4/2015 7:53:15 AM

people

Parsing date and time, try catch, variable for hours out of scope

I want to parse date and time. I want to catch if there is a format exception and I do the following: try { DateTime time = DateTime.Parse(Console.ReadLine()); ...
Jon Skeet
people
quotationmark

You've only declared time within the try block, so it's out of scope after that. You could declare it beforehand: DateTime time; try { time = ...; } catch (FormatException) { Console.WriteLine("Wrong date and time format!"); ... more 10/3/2015 9:03:04 AM

people

Use a Decimal Value as a Hexadecimal Value

I have the int 15 (or the string "15", that's just as easy), and I need to use it to create the value: "\u0015" Is there some conversion which would accomplish this? I can't...
Jon Skeet
people
quotationmark

Given that you want a Unicode code point of 21, not 15, you should definitely start with the string "15". If you try to start with 15 as an int, you'll find you can't express anything with a hex representation involving A-F... So, given... more 10/2/2015 3:33:31 PM

people

How to "downgrade" C# Decimal data type to some arbitrary number of decimal places?

I have a database table with a field defined in a very simplified form like that CREATE TABLE Product ( ProductId INT, Price SMALLMONEY ) I have a LINQ entity framework...
Jon Skeet
people
quotationmark

I suspect the problem is that you're not actually performing the rounding using decimal.Round. Instead, you're expressing it in the query which is being translated into SQL. If you want the rounding to be done in .NET instead, you need to... more 10/2/2015 3:19:47 PM

people

JSON.NET Cast Exception on DeserializeObject()

Can't get over this error, can someone please help. Error: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Models'. JSON (Validated): [ { "ModelID":...
Jon Skeet
people
quotationmark

You should specify the type you want to deserialize to in the call, as a generic type argument: Dim modelList As Models = JsonConvert.DeserializeObject(Of Models)(jsonString) Although you may find that doesn't work, and that you... more 10/2/2015 2:58:19 PM

people

Behaviour of Generic Type in Java

Stack class public class Stack { public void push(LinkedList ll) { } LinkedList class class LinkedList<Item> { private class Node<Element> { private Element...
Jon Skeet
people
quotationmark

In the declaration of LinkedList, Item is the name of a type parameter. It only has direct meaning within the LinkedList class itself. When you're declaring your push method and using LinkedList, you'd specify a type argument, i.e. the... more 10/2/2015 1:59:06 PM

people

Convert Joda LocalDateTime to Unix timestamp

How do I convert a org.joda.time.LocalDateTime to an Unix timestamp, given that local time is in UTC timezone? Example: new LocalDateTime(2015, 10, 02, 11, 31, 40) 1443785500.
Jon Skeet
people
quotationmark

Given that you want the Unix timestamp "the given LocalDateTime, in UTC" the simplest approach is just to convert it to a DateTime by specifying the DateTimeZone for UTC, and convert that: LocalDateTime local = new LocalDateTime(2015, 10,... more 10/2/2015 11:42:36 AM

people

Java inherance method with return type

Is it correct to write my classes like this? In question is the method getPrice() in the Item class. Every Item needs to have a getPrice(). But I can't actually return something....
Jon Skeet
people
quotationmark

It sounds like Item should be an abstract class then, with getPrice() being an abstract method: public abstract class Item { private final String description; public Item(String description) { this.description =... more 10/2/2015 9:50:43 AM

people

What is the precedence when more than one method overload matches?

I am trying understand OOP concepts in C#. In the following sample code: why the ins1 prefers generic method why the ins2, ins3 prefers non-generic method Note:when I comment...
Jon Skeet
people
quotationmark

Section 7.5.3.2 of the C# specification is the relevant part here - "Better function member". The result is more simply demonstrated as: using System; class Test { static void Foo<T>(T item) { ... more 10/2/2015 9:34:42 AM

people

Joda DateTime with TimeZone Information

I have the following: val startWithTZ = new DateTime("2016-10-01T00:00:00", DateTimeZone.UTC) val endWithTZ = new DateTime("2016-10-01T11:00:00", DateTimeZone.UTC) val...
Jon Skeet
people
quotationmark

Logically, I've always felt that an Interval should be between two Instant values rather than two DateTime values. However, the Joda Time API is what it is. When you create an Interval from two DateTime values, the end time is converted... more 10/2/2015 9:01:08 AM

people