Browsing 7239 questions and answers with Jon Skeet

Specified cast is not valid (double precision to int)

Here I need to cast to double precision to integer. Example: obj.DayDifference = !string.IsNullOrEmpty(reader["DateDiff"].ToString()) ? ...
Jon Skeet
people
quotationmark

What you're doing is equivalent to: object x = 32.5; int y = (int) x; You can't do that - when you unbox, you have to unbox to the actual type of the value1. So you'd need: object x = 32.5; int y = (int) (double) x; The cast to... more 7/25/2014 6:24:55 AM

people

set bits 1 3 in an int given an int with only bits 1 3 set as desired

unsigned int x = 0xdeadbeef; unsigned int y = 0x00000006; unsigned int z = 0xdeadbee7; How to compute the value in z from the values in x and y? Bits 1-3 of y are 011 and I...
Jon Skeet
people
quotationmark

It sounds like you should: Mask off bits 1-3 so that they can't be set OR the result with bits 1-3 of y So: // 0xe == 8 + 4 + 2 (i.e. bits 1-3) z = (x & ~0xe) | (y & 0xe); Note that ~ is the bitwise inverse operator, so to... more 7/25/2014 6:15:37 AM

people

Task.WhenAll() only executes 2 threads at a time?

In this problem I am trying to cache a single value, let's call it foo. If the value is not cached, then it takes while to retrieve. My problem is not implementing it, but...
Jon Skeet
people
quotationmark

Task.WhenAll() doesn't start the tasks - it just waits for them. Likewise, calling an async method doesn't actually force parallelization - it doesn't introduce a new thread, or anything like that. You only get new threads if: You await... more 7/25/2014 6:10:48 AM

people

Async method throws exception instantly but is swallowed when async keyword is removed

I'm getting some behaviour which I cannot understand when throwing exceptions in async methods. The following code will throw an exception immediately when calling the ThrowNow...
Jon Skeet
people
quotationmark

I thought async methods run synchronously until reaching a blocking method. They do, but they're still aware that they're executing within an asynchronous method. If you throw an exception directly from an async void method, the... more 7/25/2014 5:57:39 AM

people

Lambda expression gives error while sending parameters while creating a new Task

I wrote the following code: int n1 = 5 Task<int> myTask = new Task<int>(n1 => lib.MultiplyNumberTimes2(n1)); myTask.Wait(); Console.WriteLine("Result is " +...
Jon Skeet
people
quotationmark

The compilation error says exactly what you're doing wrong - you're trying to use the name n1 for two different things. You've got a local variable here: int n1 = 5; ... and then you're also trying to use it your lambda expression... more 7/24/2014 9:16:27 PM

people

Creating a thread using an Anonymous method how to solve issue with Closure?

Suppose you have the following: For i as Integer = 0 To 10 For j as Integer = 0 to 10 Dim t as New Thread ( Sub() Console.WriteLine("Hello: " &...
Jon Skeet
people
quotationmark

You just need to take a local copy of i and j within the loop: For i as Integer = 0 To 10 For j as Integer = 0 to 10 Dim iCopy = i Dim jCopy = j Dim t as New Thread ( Sub() ... more 7/24/2014 9:11:20 PM

people

Error suggests I'm missing a using directive or assembly reference, but I'm not....

I'm pretty new to working with more than one project in a solution. I have two projects: AutoPoster.Droid and AutoPoster.Core. In AutoPoster.Droid I have an adapter class that...
Jon Skeet
people
quotationmark

You can't use namespaces like that. You can't specify just part of a namespace, from after AutoPoster. You either need a using directive for AutoPoster.Core.Model: using AutoPoster.Core.Model; namespace AutoPoster.Droid.Adapters { ... more 7/24/2014 8:42:34 PM

people

One line toasting error in java Incompatible types

When I want to do a one line toast in Java I get this error message: JavaTester.java:34: incompatible types found : void required: android.widget.Toast Toast newToast =...
Jon Skeet
people
quotationmark

You're calling the .show() method, which has a void return type. Either don't try to use the result: Toast.makeText(this, "This is a one liner", Toast.LENGTH_LONG).show(); Or use two statements: Toast newToast = Toast.makeText(this,... more 7/24/2014 8:29:39 PM

people

Encode current date into short unique string

I need to encode current datetime into some unique string to store it in database. I found this article how to generate a unique token which expires after 24 hours? but for me...
Jon Skeet
people
quotationmark

Okay, if you want it from "about now" to some point in the future, and you want seconds granularity, and you want ASCII symbols, let's assume base64. With 8 characters of base64, we can encode 6 bytes of data. That will give us 248... more 7/24/2014 3:26:34 PM

people

? operator without else part

I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements): if(someStatement) { ...
Jon Skeet
people
quotationmark

Basically, you're trying to use the conditional operator for something that it's not designed for. It's not meant to optionally take some action... it's meant to evaluate one expression or another, and that be the result of the... more 7/24/2014 1:22:17 PM

people