Browsing 7239 questions and answers with Jon Skeet

What are the default values of the char array in Java?

If I allocate array of characters like this: char[] buffer = new char[26]; what are the default values it is allocated with? I tried to print it and it is just an empty...
Jon Skeet
people
quotationmark

It's the same as for any type: the default value for that type. (So the same as you'd get in a field which isn't specifically initialized.) The default values are specified in JLS 4.12.5: For type char, the default value is the null... more 2/25/2016 7:04:38 AM

people

Variable off an Else If Condition statement

I have a couple of else/if statements in my code below. However I want to set a final variable that will set the final if statement depending on the users input. Lets say the user...
Jon Skeet
people
quotationmark

You'll want to use the final result whether there's a discount or not, so you should have a variable for it whether there's a discount or not. If there isn't a discount, just set the value of the variable to the original. In fact, I would... more 2/25/2016 6:47:47 AM

people

Get XElement from XDocument

I have XML <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"...
Jon Skeet
people
quotationmark

You're trying to look in a namespace with a URI of "s" - it doesn't have that URI. The URI is "http://schemas.xmlsoap.org/soap/envelope/". I'd also suggest avoiding XName.Get and just using XNamespace and the XName +(XNamespace, string)... more 2/24/2016 3:50:12 PM

people

Using WeakReference Safely

Say I have a class Publisher with a list of Subscriber objects stored in a list of WeakReference<> public interface Subscriber { void update(); } public class...
Jon Skeet
people
quotationmark

Your suggested second nullity check isn't good enough either, as the first call to get() could return a non-null value, and the second call could return null. I'd suggest: for (WeakReference<Subscriber> subRef : subscribers) { ... more 2/24/2016 3:46:59 PM

people

Why can't I use a local variable inside of a Using block?

I'm still learning the particulars of C# so please forgive me if this rudimentary. I have been looking for an answer to this but haven't found one. I have declared the local...
Jon Skeet
people
quotationmark

This has nothing to do with the using statement, and everything to do with the while loop. Leaving everything else aside, this is the problem, in essence: string myVar; while (someNonConstantCondition) { myVar = someValue; } return... more 2/24/2016 3:40:32 PM

people

How to pass a list of a class but also refer to a specific variable in that class

So here is a basic example and not the full code. I want to be able to refer to the specific variable in Symbol rather than have a bunch of if statements to know which values to...
Jon Skeet
people
quotationmark

It sounds like what you really want is to accept a Func<Symbol, DataLine>, or possibly a Func<Symbol, int>. Although at that point, you don't really need a method, given how trivial (and value-free) that method would be using... more 2/24/2016 2:53:07 PM

people

Connection is disposed automatically

this may seem trivial but it's really bothering me. I have started today using Dapper within an MVC project and created a very simple POCO object; when I run this project thought,...
Jon Skeet
people
quotationmark

it seems that placing the "sqlConn" into the using statement is making it dispose automatically Yes, that's what the using statement is for. How can I prevent this? I'd prefer not to open and close the connection manually each... more 2/24/2016 11:36:57 AM

people

Why I need a local variables to store value in SqlParameter from Ajax to WCF method

I'm currently work with WCF and Jquery ajax for testing purposes. I'm quite new with WCF. Anyways, I have a service is called "ProductsService" has four parameters that is invoked...
Jon Skeet
people
quotationmark

You don't need local variables for this - either for the individual SqlParameters or the values. Your code would be simpler as: public void InsertProduct(int categoryId, string name, string description, decimal price) { String sc =... more 2/24/2016 8:18:53 AM

people

Java Stream Cipher (With input and output txt files) outputting random characters not complete decrypted string

Don't know where I went wrong necessarily but my program is supposed to be a stream cipher that takes an input.txt file of chars and encrypts it into numbers and then decrypts it...
Jon Skeet
people
quotationmark

You're using your random number generator differently in the two cases. In your encryption code, you generate one random number, and use it for all characters: Random rng = new Random(key); int randomNum = rng.nextInt(256); while... more 2/24/2016 7:02:31 AM

people

java.net.BindException: Address already in use: JVM_Bind Multi Client Server

I have a Server project and a Client project. While the Server is running the first connection from client works fine. At the seccond connection the Server fires the...
Jon Skeet
people
quotationmark

This is the problem: while(true) { ServerSocket s = new ServerSocket(2345); pipe = s.accept(); ... } You're trying to bind to the same port repeatedly. All you need to do is create a single ServerSocket and call accept on it... more 2/23/2016 8:48:29 PM

people