Browsing 7239 questions and answers with Jon Skeet

Get name of a property and declaring class

How can I use reflection to get the name and declaring class of a property of a generic type. The purpose is to get an exception if I read a property where nothing has been...
Jon Skeet
people
quotationmark

No, you can't do it like that. I would suggest something like this instead: // You could do this without the constraint, with a bit of extra work. public class ReadOnlyAfterWrite<T> where T : struct { private T? value; ... more 12/23/2014 9:22:41 PM

people

how to use/return "public static String [] list={};" (program from amplify unit 3 lesson 29)

Question: "A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word. Write the code to find the longest...
Jon Skeet
people
quotationmark

Look at this part: public static String [] list={}; { String list [] = {"high", "every", "nearing", "checking", "food ", "stand", "value", "best", "energy", "add", "grand", "notation", "abducted", "food ", "stand"}; } That is... more 12/23/2014 8:48:22 PM

people

Why are Java Timer threads disappearing?

I have code that schedules one-time tasks to execute and does this over and over. It looks something like this. public static void main(String[] args) { while(true) { ...
Jon Skeet
people
quotationmark

You're creating a Timer instance, but not making sure that it doesn't get garbage collected. From the documentation: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the... more 12/23/2014 7:33:16 PM

people

Overload division in C#

I want to overload division operator in my C# class. So, i wrote: public string[] operator/ (object obj) { } And got error: "Parser error: Overloadable unary operator...
Jon Skeet
people
quotationmark

You can overload the division operator, but: It must always be a binary operator - you've only provider one operand It must always be static At least one of the operand types must be the type you're declaring it in So for... more 12/23/2014 6:02:02 PM

people

Java Generics argument mismatch

I have an Interface Worker like below: public interface Worker<T> { public void doAllTasks (List<? extends T> tasks); } There is an Abstract Class that uses...
Jon Skeet
people
quotationmark

The problem is that the type of myWorker is just AbstractWorker<? extends Task> - in other words, it doesn't know that the type argument is really SpecialTask. It could be an AbstractWorker<SomeOtherTask>, which wouldn't know... more 12/23/2014 5:47:42 PM

people

Why is Long unable to accept 12 digit value even though I explicitly declared it to?

import java.io.*; import java.util.*; public class Solution { public static void main(String args[]) { Scanner s=new Scanner(System.in); int...
Jon Skeet
people
quotationmark

You're assigning to a long, but all the arithmetic is being done with int, because every part of the expression (k-(k/2)) * (k/2) is an int. The simplest fix would be to declare k as a long, e.g. long k = s.nextInt(); long value =... more 12/23/2014 5:19:13 PM

people

Order of Operations in android, why are so going on?

I have a strange problem with the order of execution methods. A progress bar.setVisibility(View.VISIBLE)appears only after the operation geocoder.getFromLocation is...
Jon Skeet
people
quotationmark

program must be running on the main thread without thread Well that's the problem. You're performing an expensive operation (getFromLocation) on the UI thread. Don't do that, basically. You're stopping the UI from being updated... more 12/23/2014 2:45:38 PM

people

Subclass member initialization

I've created two classes which replace the function of a Point (both variants) just to test them out, this is how I've created them: Entity.cs: namespace Xnagame { class...
Jon Skeet
people
quotationmark

Your CreatePlayer method is an instance method - in other words, it has to be called on an existing instance. You probably want to make it static: public static Player CreatePlayer() You should then remove the Position = new... more 12/23/2014 1:41:42 PM

people

IEnumerable<Func<T,S>> and LINQ type inference

C# compiler can correctly infer type of s (string) in these snippets: Func<int, string, string> f1 = (n, s) => s.Substring(n); Func<int, Func<string,...
Jon Skeet
people
quotationmark

All the information about the type isn't known beforehand. In your first working snippet, you're telling it on both lines which delegate type you want s => s.Substring(n) to be converted to. In your second snippet, the only place where... more 12/23/2014 12:55:08 PM

people

How to get current time in T07:07:45 0500 format

I want to get the current time in T07:07:45-0500 format. I have try like this: DateTime currentDate = DateTime.Now; String currentTime =...
Jon Skeet
people
quotationmark

I don't think the .NET custom date and time format strings allow that format. You could use my Noda Time project easily enough, using DateTimeOffset.Now if you want the system default time zone to be applied. (It can be done... more 12/23/2014 12:37:09 PM

people