Browsing 7239 questions and answers with Jon Skeet

Typecasting objects to class C#

Problem: I am trying to typecast an object type back to a root class I'm starting with a generic doubly linked list and I am able to add values but not retrieve them class...
Jon Skeet
people
quotationmark

Your Retrieve method returns the node itself - not the value within the node, which I assume is what you expected it to. That's why you're getting an InvalidCastException. All you need to change is the last line of Retrieve from return... more 9/13/2014 5:51:44 PM

people

Constructors in Java with implicit return type

I know pretty well about all the things regarding Constructors in Java, which I pen down below so as to ensure : It can be tedious to initialise all the variables in a class each...
Jon Skeet
people
quotationmark

It's just a confusing piece of text, basically. There's no concept of a return type for a constructor, as you say - you can think of them as being a bit like a method with a return type of the same type, but: Java (annoyingly) lets you... more 9/12/2014 7:11:12 PM

people

Async operation immediately awaited

I'm looking at these lines of code from another developer: bool isValid = await engine.GetTaskByIdAsync(taskId); if(isValid ) .... When I work with async operations, it...
Jon Skeet
people
quotationmark

Is there some value here I don't understand? Absolutely - it means that although you need the value returned by the operation before you can do any more work, you're not tying up a thread while you're waiting for it. That's... more 9/12/2014 7:08:41 PM

people

Cant make while() loop work when keep adding up random doubles until a particular distance is reached

I use a while() loop to keep adding up random doubles to x until x reaches a particular distance from origin. It stops running without any output. If I change the while() loop to...
Jon Skeet
people
quotationmark

You're checking whether the value is exactly the distance required. You need to check whether it's reached or exceeded the required distance: while (Math.abs(x) < distance) In general, using == or != with floating point values is a... more 9/12/2014 4:39:48 PM

people

processing Xhtml files with Xdocument class adds unwanted elements

I am working on a project which requires me to process xhtml files to fix the content of certain tags. The fixing itself is not a problem, however I have troubles when saving the...
Jon Skeet
people
quotationmark

When you add the span element, you're doing it without a namespace - whereas some ancestor element has set the default namespace. All you need to do is use the right namespace for your new elements: XNamespace ns =... more 9/12/2014 4:29:04 PM

people

Convert Func<T, TProperty> to Expression<Func<T, Property>>

I've got a generic repository implementation that allows to pass a selector in order to declare the entities primary key property: public abstract class...
Jon Skeet
people
quotationmark

You can't, basically - not in any useful way. The solution seems simple though - change the type of your property instead: private readonly Expression<Func<TEntity, TKey>> _keySelector; protected... more 9/12/2014 2:36:46 PM

people

DateTimeOffset TotalHours returns different values

The following code returns 9.25 when I feel it should return 8.25, what have I misunderstood? (new DateTimeOffset(2014,09,04,08,15,00,new TimeSpan(0,0,0))).Subtract(new...
Jon Skeet
people
quotationmark

The problem is that the DateTimeOffset.Date property returns a DateTime with a Kind of unspecified. That's then assumed to be system-local when you subtract it from the DateTimeOffset. You can fix this by asking for the UtcDateTime and... more 9/12/2014 1:41:27 PM

people

Converting number in string with negative sign at the end

From the MSDN documentation I should use NumberFormatInfo's NumberNegativePattern property to set the expected pattern for negative number values. So I tried: var format = new...
Jon Skeet
people
quotationmark

You don't need a format here - it looks like the NumberNegativePattern is only used when formatting, not parsing, and then only for the N format. However, there's a NumberStyles value for this: Console.WriteLine(double.Parse("1.000-", ... more 9/12/2014 1:30:09 PM

people

How to mock a Class with IEnumerable

I am using Moq for unit testing in C# and want to put some fake data in the following class. public class UserResponse { public IEnumerable<usertab>...
Jon Skeet
people
quotationmark

Well you're not "faking" it at all - you're just using an array as the implementation. There's nothing wrong with doing that - personally I like using real code within tests, so long as: You have confidence in the other code you're... more 9/12/2014 1:21:46 AM

people

How do you tie a String variable to a String variable?

Hello so I am attempting to write a code that test the operators and need a little help declaring variables. So in my code under my if statement I declare a certain answer to be...
Jon Skeet
people
quotationmark

You're not declaring the variable at all. You're just trying to assign to it. You can just add String answer; to the code before you start trying to assign to it. Additionally, you almost never want to call new String(String), and you... more 9/12/2014 1:13:31 AM

people