Browsing 7239 questions and answers with Jon Skeet

Object not holding instance when accessed through method

I made a Model Class "RestaurentList" which populates two collections with data from a json file. After instantiating an object to the class in my ViewModel i databind the...
Jon Skeet
people
quotationmark

Look at the line of code before your call to populatePartialList: _list = new RestaurentList(); You've created a new instance of RestaurentList. That will have called populateList(), but not waited for it complete. Assuming your real... more 10/6/2014 9:27:05 AM

people

LongAdder[] + System.arraycopy() =?

The simplified scenario is that I have an array of LongAdders and multiple threads are accessing this array in order to increment variable at a given index. Is it safe to...
Jon Skeet
people
quotationmark

A LongAdder array will only contain references anyway, so that's what will be copied. The fields within LongAdder are irrelevant to that. Whether it's "safe" to use arraycopy depends on what you're doing with it - if you're using that to... more 10/6/2014 9:15:40 AM

people

Can I get the time of compilation of a java class from withing itself?

I'd like to print the time of compilation of a class. class Test { public Test() { System.out.println("Compiled:" + getCompilationTimestamp()); } } Is this possible?
Jon Skeet
people
quotationmark

Looking at the class file format, I don't believe that information is stored anywhere - so the answer would be "no". If you're building your classes into a jar file, you might want to include a metadata file which specifies this... more 10/6/2014 9:12:31 AM

people

Cannot get child nodes of a specific type that contain a specific text

Using the code below, I can receive all the <job> elements in my Xml. However, when I try to search for jobs that have a child called <Name> and their text equals...
Jon Skeet
people
quotationmark

I'm not an XPath expert, but I suspect your query now is trying to find jobs where the name element is equal to a receiverjob element. I suspect you want something like this: "descendant::d:job[name/text()='receiverjob']" It's also... more 10/6/2014 8:23:31 AM

people

how to get a class with forName() in a different package?

Alright, consider the following example: inside of the src/ folder, i have the following packages : org/ com/ Class Foo is located in org.somepackage; Class Doo is located...
Jon Skeet
people
quotationmark

This is the problem: java -cp src -jar FooJar.jar From the java tool documentation: When you use the -jar option, the specified JAR file is the source of all user classes, and other class path settings are ignored. Try running it... more 10/6/2014 7:17:08 AM

people

Java to C# conversion finding public fields of subclass with reflection

I'm currently porting part of a framework over to C# from java. I used the following line in Java to get the declared fields in order of declaration of the subclass of my...
Jon Skeet
people
quotationmark

You just need to use BindingFlags.DeclaredOnly: Specifies that only members declared at the level of the supplied type's hierarchy should be considered. Inherited members are not considered. For example, assuming you want private... more 10/5/2014 8:08:16 PM

people

How to remove an element from a String array?

I have an array which contains three items, for example: title_list = new String[] { title1, title2, title3 }; These values are coming from SharedPreferences. I would like to...
Jon Skeet
people
quotationmark

You can't remove an element from an existing array - once you have created an array, its length is fixed. You can replace elements, but you can't add or remove them. Options: Change the value of the variable to refer to a new list,... more 10/5/2014 7:48:01 PM

people

LINQ: Find if one item in List appears in a string array

I have a string array and another object that has a list of objects, on of the properties is a string. public string[] allowedroles; foreach (var role in allowedroles) { if...
Jon Skeet
people
quotationmark

It sounds like you want: var authorize = user.RolesList.Exists(r => allowedRoles.Contains(r.Name)); Or transform the list of roles into their names, and see whether that intersects with the allowed roles: var authorize =... more 10/5/2014 4:04:16 PM

people

How would one do > x < in C#

I'm attempting to make a random number generator that then selects one of three options based on the random number. I would like to use x < as the second choice of three but...
Jon Skeet
people
quotationmark

Well the simplest option is to use else if so you only need to check one condition anyway - which means it would handle 33 as well (currently not handled): if (rand < 33) { Console.WriteLine("rand was in the range [0, 32]"); } else... more 10/5/2014 3:59:21 PM

people

How to differenciate a not assigned public int property new instance object from an assigned one

Lets have a simple class with an int property public class SimpleClass { public int myInt { get; set; }// for having a property and not "public int myInt;", see Jon...
Jon Skeet
people
quotationmark

Unless you have code to specifically remember the difference between a property which has been initialized with its default value, and one which hasn't been set at all, you can't tell the difference. I have two suggestions, however: You... more 10/5/2014 3:56:17 PM

people