Browsing 7239 questions and answers with Jon Skeet

Is it possible to declare a global varaible and initialize it in a function? c#

public Tiles[,] tiles; Is a global variable, an array, I dare say, the size of which is yet to be discovered. That's why I wish to initialize it inside a function. Alas, after...
Jon Skeet
people
quotationmark

Sounds like you just want: private readonly Tile[,] tiles = InitializeTileArray(); ... private static readonly Tile[,] InitializeTileArray() { Tile[,] array = ...; // Whatever you want here return array; } Note that the... more 4/2/2015 10:53:00 PM

people

Iterating over an object of type Iterator<T>

While reading through the Wikipedia article on Generators, I found the following Java implementation to iterate over a generic type Iterator<Integer> produce an infinite...
Jon Skeet
people
quotationmark

The code sample on Wikipedia is invalid, but you can easily iterate anyway, just by calling hasNext() and next() explicitly. // We know that fibo.hasNext() will always return true, but // in general you don't... while (fibo.hasNext()) { ... more 4/2/2015 6:52:39 PM

people

return new set for one class

I ran into a problem with Java recently. I have a HashSet hashSet with a superclass 'Foo'. This set is filled with subclasses of foo : bar and dop. How do I return a new HashSet...
Jon Skeet
people
quotationmark

It sounds like you want to pass in the class (rather than an instance of the class) and use Class.isInstance: public Set<Foo> getObjectsFromClass(Class<? extends Foo> clazz) { Set<Foo> objectsFromClass = new... more 4/2/2015 6:35:11 PM

people

Current time in millisecond in MYSQL

How to get the current time in millisecond in MySQL. So far what I've got is UNIX_TIMESTAMP(). But it returns the current time in seconds. But I want the current time in...
Jon Skeet
people
quotationmark

Looking at the documentation, it sounds like you want NOW(3) ... where the value 3 is used to specify that you want 3 digits of subsecond precision, i.e. milliseconds. (Unfortunately none of the examples in the docs show that being... more 4/2/2015 5:26:54 PM

people

How to remove escape extra slash in java properties

I am using the following to write some values to a .properties file. this code works with one small problem the value that gets written is like...
Jon Skeet
people
quotationmark

That's fine. That's how it's meant to be written to the properties file. From the Properties.store documentation: The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly... more 4/2/2015 1:46:41 PM

people

Implementing an attribute with similar behavior to CompilerServices.CallerMemberAttribute

It is possible to create an attribute that has a behavior similar to CallerMemberNameAttribute? I mean, I have googled and found this article, that says CallerMemberName is a...
Jon Skeet
people
quotationmark

It is possible to create an attribute that has a behavior similar to CallerMemberNameAttribute? Only by changing the C# compiler yourself, basically. After all, you're asking for behaviour similar to something that the C# compiler has... more 4/2/2015 1:36:58 PM

people

How to check if bool is false in a loop

I'm trying to loop through an array of classes. The class has two variables: a transform and a bool. I want to loop through this in another script to see if that current position...
Jon Skeet
people
quotationmark

Well, you can just access the element at the relevant index, and then check the field value: if (TheObject.GetComponent<GetInObject>().PosInObect[i].isFilled) However, if you don't need the index, I'd recommend using a foreach... more 4/2/2015 1:33:34 PM

people

C# anonymous object with properties from dictionary

I'm trying to convert an dictionary to an anonymous type with one property for every Key. I tried google it but all I could find was how to convert a anonymous object to a...
Jon Skeet
people
quotationmark

You can't, basically. Anonymous types are created by the compiler, so they exist in your assembly with all the property names baked into them. (The property types aren't a problem in this case - as an implementation detail, the compiler... more 4/2/2015 1:19:21 PM

people

How to start merged .NET application?

So, I use ILMerge to merge my .NET assemblies to one. Basically, the one .NET application is the core, the console and the other .NET application is a GUI, the front-end. Now,...
Jon Skeet
people
quotationmark

You would just call the Main method of the console code: public class GuiApp { public void WhenYouWantToCallTheConsole() { // Probably in a different thread... ConsoleApp.Main(...); } } public class... more 4/2/2015 1:02:18 PM

people

What is a "localized message"?

I have seen in several languages that (especially exceptions) offer message and localised message methods. But it is not clear to me what the difference is - do any of the...
Jon Skeet
people
quotationmark

This one: Is this a physical location thing where the same message may be presented based on things like locale, timezone, etc. Although it's usually just locale / culture, really. Basically imagine that an error message of "The... more 4/2/2015 3:41:48 AM

people