Browsing 7239 questions and answers with Jon Skeet

Create a Func<> with Roslyn

Inspired by this and this article, I'm trying to create a dynamic function with Roslyn. However the mentioned sources are outdated or not complete and I'm not able to create a...
Jon Skeet
people
quotationmark

Disclaimer: I haven't actually used Roslyn in anger much at all. Currently your code declares a variable, but doesn't do anything with it afterwards. Based on this random blog post, it looks like you possibly just need an extra expression... more 3/18/2014 3:06:52 PM

people

Exception to throw when instance variables are null

I'm working on making a codebase complaint with a list of PMD rules. These include that you're not allowed to throw or catch NullPointerExceptions. I found this method which...
Jon Skeet
people
quotationmark

IllegalStateException seems entirely appropriate here: Signals that a method has been invoked at an illegal or inappropriate time. That describes the situation reasonably clearly, doesn't it? more 3/18/2014 3:00:37 PM

people

Change background of JLabel in runtime using reflection

I need to change background of JLabels dynamically. I've 70 JLabels in a class. All JLabels represent some specific items. The items names are same as the variable of JLabel....
Jon Skeet
people
quotationmark

Currently you're just looking at the fields themselves - you're interested in the values of those fields. For example: Object value = fld[i].get(target); // Or null for static fields if (value == label5) { ... } Here target is a... more 3/18/2014 9:31:18 AM

people

Writing and reading a type value to/from file

I need to be able to store a type value into a file, and read it back into a type value later. What's the best way to go about this? Type type =...
Jon Skeet
people
quotationmark

I'd store the assembly-qualified name, assuming the assembly will still be present later: binaryWriter.Write(type.AssemblyQualifiedName); ... string typeName = binaryReader.ReadString(); Type type = Type.GetType(typeName); If you... more 3/18/2014 8:31:15 AM

people

why it is giving me no such method exception

import java.lang.reflect.Constructor; class tr1 { public static void main(String[] args) { try { if(args.length<1) throw(new...
Jon Skeet
people
quotationmark

why this code is giving me nosuchmethod exception? Because you don't have a constructor with the parameters you've requested: (int, char, String, String) You only have a constructor with these parameters: (int, char,... more 3/18/2014 7:03:02 AM

people

Size of HashMap not increasing

I go through a loop three times and call this method each time: // class variable private HashMap<String, ffMotorskillsSession> tempMap; tempMap = new HashMap<String,...
Jon Skeet
people
quotationmark

A Map stores key/value pairs, with only one value per key. So if you're calling put with the same key multiple times, then it will correctly stick to the same size, only having a single entry for that key. more 3/17/2014 10:33:32 PM

people

Setting XML Namespace sets it empty in children

I want to set the namespace of the root element in a XML file which works: XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "no")); XNamespace ns =...
Jon Skeet
people
quotationmark

But the direct child elements do have an empty xmlns attribute. How can I avoid that? You specify the namespace for the child elements as well: doc.Root.Add(new XElement(ns + "SomeChildElement")); The point is that elements inherit... more 3/17/2014 10:12:09 PM

people

How to do Callbacks with Dynamic?

I keep getting this error and not sure how to correct it Error 1 Cannot use 'Callback' as an argument to a dynamically dispatched operation because it is a method group....
Jon Skeet
people
quotationmark

The problem is that this call is dynamic: userService.SignIn(openId, email, Callback); It has to be, because openId and email are inferred to be of type dynamic: var openId = meResult.id; var email = meResult.emails.preferred; You... more 3/17/2014 10:05:46 PM

people

Accessing local variables of the class in anonymous Action delegate

I am struggling to understand how to access local class variable in an action delegate when both the delegate is anonymous and the containing class is created just in time as an...
Jon Skeet
people
quotationmark

It's somewhat painful to do so. You basically need to declare a local variable, give it a temporary value (so that it's definitely assigned) and then use that local variable: QueryPropertyMessage message = null; message = new... more 3/17/2014 6:56:06 PM

people

.charAt() returning nullpointerexception

I've written a small program where I have a two dimensional array with String names. In a nested for loop I check for the first character in each element of the array. If the...
Jon Skeet
people
quotationmark

You've only initialized as far as polje[1][...]. So when i is 2 (and j is anything), polje[i][j] will be null... and when you dereference it by calling charAt(0), you'll get the exception. Note that this sort of error can be avoided using... more 3/17/2014 4:06:25 PM

people