Browsing 7239 questions and answers with Jon Skeet

How can I pass an attribute parameter type with List<string> in C#?

How can I pass a List to a construtor? It shows a message: Error 14 An attribute argument must be a constant expression, typeof expression or array creation expression of an...
Jon Skeet
people
quotationmark

The problem isn't passing a List<string> to a constructor in general - the problem is that you're trying to use it for an attribute. You basically can't do that, because it's not a compile-time constant. It looks like ProgramList is... more 10/9/2014 10:36:51 AM

people

How to get returned value of async Task<string> methdoName()?

I'm trying to get the return string of my method but the problem is I don't know how can I get the return value from public async Task<string> Login(string username, string...
Jon Skeet
people
quotationmark

Currently you're just calling Wait() - that will block until the task completes, but won't give you the return value. If you use the Result property instead, that will block and then give you the result: string certificate =... more 10/9/2014 1:54:42 AM

people

Variable might not have been initialized when it has been initialized and declared

When it is compiled it says variable secondNumberString might not have been initialized. But I have declared it as a string and initialized it in the secondNumber switch. What am...
Jon Skeet
people
quotationmark

But I have declared it as a string and initialized it in the secondNumber switch. Well, you have if you hit any of the specified cases. But your default case is just: default: JOptionPane.showMessageDialog(null, "Invalid... more 10/8/2014 9:10:08 PM

people

Java Round number up so number of digits increments

What is the most efficient way (in Java) to round a number n up to the nearest power of ten which contains one more digit than the original number? e.g. 3 - 10 432 -...
Jon Skeet
people
quotationmark

If you're looking for a string, you can use Math.log10 to find the right index into an array: // Do more of these in reality, of course... private static final String[] MESSAGES = { "1", "10", "100", "1,000", "10,000" }; public static... more 10/8/2014 8:55:00 PM

people

Class access modifier

I have the following two classes package one; public class Student { //Some code } package two; public class Test { public static void main(String args[]) { ...
Jon Skeet
people
quotationmark

You don't have an import statement, so the compiler doesn't know that Student is meant to refer to one.Student. You can either use: import one.Student; or import one.*; ... or just fully-qualify the name when you create the... more 10/8/2014 8:20:03 PM

people

String.Format won't create correct spacing, exhausted every method

this may seem like a trivial question, but I swear I have exhausted every method I can find. I'm trying to output the contents of a dictionary to a textbox. This is written in C#....
Jon Skeet
people
quotationmark

I suspect the problem is that you're using a TextBox, and my guess is that you haven't set the text to a monospace font... but you're trying to use string formatting to position the values accurately. To investigate string formatting, I'd... more 10/8/2014 7:17:30 PM

people

How to add value to each item in array inline style

Lets say I have an array string[] arr = new string[] {"1","2","3"}; I would like to add a string to the end of value, something like this: arr.appendToEnd(" Test"); return...
Jon Skeet
people
quotationmark

There isn't anything built-in. You could use LINQ to create a new array easily: arr = arr.Select(x => x + " Test").ToArray(); ... but that's not going to modify the original array. If anything else has a reference to the original... more 10/8/2014 5:59:19 PM

people

How to get the right input for the String variable using scanner class?

I am learning Java and I was trying an input program. When I tried to input an integer and string using instance to Scanner class , there is an error by which I can't input...
Jon Skeet
people
quotationmark

nextInt() doesn't wait for the end of the line - it waits for the end of the token, which is any whitespace, by default. So for example, if you type in "27 Jon" on the first line with your current code, you'll get a value of 27 for ip and... more 10/8/2014 4:19:27 PM

people

Does Java read 0xA0 as 0xFFFD

One of my data processing modules crashed while reading ANSI input. Looking at the string in question using a hex viewer, there was a mysterious 0xA0 byte at the end of it. Turns...
Jon Skeet
people
quotationmark

U+FFFD is the "Unicode replacement character", which is generally used to represent "some binary data which couldn't be decoded correctly in the encoding you were using". (Sometimes ? is used for this instead, but U+FFFD is generally a... more 10/8/2014 4:01:14 PM

people

Do DI Containers need to instantiate the object themselves in order to use constructor injection?

Or instead of creating the object themselves, do they somehow intercept or hook into object creation (for example, a Controller instantiated by the MVC framework) and pass in...
Jon Skeet
people
quotationmark

In all the DI/IoC containers I've used, the containers do the creating themselves. You won't find any production code using new Something(dependency1, dependency2)... instead, you'll have code implicitly or explicitly asking the container... more 10/8/2014 3:56:04 PM

people