Browsing 7239 questions and answers with Jon Skeet

Declare a single static variable that will be used by many WebMethod functions

I have a session variable that I need to use on a cs page with many webmethod functions. If I declare it as follows I don't always get the latest variable. Sometimes it gives me...
Jon Skeet
people
quotationmark

Currently you're initializing the variable once, when the class is first loaded. You want to have a different value on each request. Rather than having a variable for that, you should have a property or method. For example: private... more 5/14/2015 12:49:49 PM

people

Using lambda to format Map into String

I have a map with Integer keys and values. I need to transform it into a String with this specific format: key1 - val1, key2 - val2, key3 - val3. Now, I'm using forEach to format...
Jon Skeet
people
quotationmark

I think you're looking for something like this: import java.util.*; import java.util.stream.*; public class Test { public static void main(String[] args) throws Exception { Map<Integer, String> map = new... more 5/14/2015 12:43:49 PM

people

Best way to compare Periods in using NodaTime (or alternative)

I have a requirement to have a relative min/max date validation able to be stored in a database to customize an application per customer. I decided that the NodaTime.Period due...
Jon Skeet
people
quotationmark

Just as an additional point to Matt's already-excellent answer, we provide an option for creating an IComparer<Period> with a specific anchor point, e.g. var febComparer = Period.CreateComparer(new LocalDate(2015, 2,... more 5/14/2015 12:06:58 PM

people

Sort array list by numbers then by letters

I have array list of strings: "1A", "12A", "12B", "6", "A", "5B", "B", "13". If I do myList.Sort(); then I get: "1A", "12A", "12B", "13", "5B", "6", "A", "B". But what I...
Jon Skeet
people
quotationmark

Your comparer is too simplistic. Your comparison needs to split each value into the number and the rest, then compare the numbers first, then the strings if they're equal. So it would be something like: public sealed class... more 5/14/2015 11:54:47 AM

people

Encrypt Content by RSA in Android / Java

public static String encryptByPublicKey(byte[] data, String key) throws Exception { byte[] keyBytes = decryptBASE64(key); X509EncodedKeySpec x509KeySpec = new...
Jon Skeet
people
quotationmark

Never, ever, ever pass arbitrary binary data to the String constructor. You don't have encoded text, you have arbitrary bytes. That's not what the String constructor is for. Ideally, don't represent the binary data as text at all - but if... more 5/14/2015 9:34:56 AM

people

Linq between dates query

I am trying to convert the following sql query as a linq query, however I keep experiencing an error - Operator '<=' cannot be applied to operands of type 'string' and...
Jon Skeet
people
quotationmark

The problem is that you're introducing strings into the mix for no reason at all. Unless you have to convert a DateTime to or from a string, don't do it. Your query should be as simple as: DateTime preWeek =... more 5/14/2015 9:30:11 AM

people

Why conditional operator not work in java switch case?

I do the following code int cnt=1; switch(cnt){ case (cnt<=10): System.out.println("Less than 10"); break; case (cnt<=20): System.out.println("Less than...
Jon Skeet
people
quotationmark

That's simply not how switch/case statements work - in Java or in various similar languages (C, C++, C#). It's not the point of them. Switch/case statements aren't a sequence of conditions - they're a sequence of constant values (with... more 5/14/2015 5:51:29 AM

people

javac is able to find the file but not the java command

Example: If there is a file HelloWorld.java in c:\xyz\javaprograms\HelloWorld.java in the command prompt at the following default directory: c:\users\username>javac...
Jon Skeet
people
quotationmark

You don't pass a filename to the java command - you just pass a classname, and the classloader has to know how to load a class with that name. It worked when you were in the right directory, because the current directory is implicitly on... more 5/13/2015 4:20:18 PM

people

Display the string with new line with out using \n in jsp

I have a form with a text area as one of the input fields. I want to store the user entered data. In the process I need to store the \n (enter key). But here the problem is in the...
Jon Skeet
people
quotationmark

When I display the same data in the browser it shows in single line. Yes, it would - because a newline in HTML doesn't get rendered as a newline. If you look at the raw HTML, I suspect you'll still see the line breaks... it's just... more 5/13/2015 1:15:54 PM

people

Can't remove methods from delegate object invocation list properly

As I read here and there a little, adding to or removing methods from the invocation list with or without the new keyword of a delegate object in C# is exactly the same and...
Jon Skeet
people
quotationmark

You've got two different syntaxes here: DelegateType x = new DelegateType(MethodName) DelegateType x = new DelegateType(ExistingDelegateInstance) Those do different things - the first builds a new delegate instance based on a method... more 5/13/2015 12:43:42 PM

people