Browsing 7239 questions and answers with Jon Skeet

Zip File getting corrupted when i save it to isolated storage

I have downloaded a zip file from blob storage and save it to isolated storage of windows phone like this :- FileStream fs is downloaded from blob. public static void...
Jon Skeet
people
quotationmark

You're using a StreamWriter. That's for text. You shouldn't be using it to copy a zip file at all. Never use any TextWriter for binary data. Next you're using StreamWriter.Write(object), which is basically going to call ToString on the... more 7/2/2014 8:36:11 PM

people

Java switch using methods

I am wanting to call methods from an object from a switch statement. Here, I am switching on a String: switch (properties.getProperty("my.property")) { // getProperty() retuns a...
Jon Skeet
people
quotationmark

No, you can't. switch/case statements are for simple "this constant value maps to this set of actions; this constant value maps to this set of actions; ...". If you need more complex processing, you should just use if statements. more 7/2/2014 8:25:07 PM

people

Can an abstract class constructor instantiate a new object?

I have a group of services that get injected into my controllers. Can my abstract class constructor instantiate new instances of commonly used objects? public class ReportService...
Jon Skeet
people
quotationmark

Can my abstract class constructor instantiate new instances of commonly used objects? Absolutely. The constructor for an abstract class can do basically anything a normal constructor can. The only restriction is that you can only call... more 7/2/2014 8:06:49 PM

people

Java which element in the ListIterator is considered previous?

I'm currently trying to learn how to implement my own ListIterators. I have most of it implemented and ready to go, except I'm confused by the previous() method. By standard...
Jon Skeet
people
quotationmark

It's easiest to think of the cursor as being between two elements. So at the start, it's before dog. Calling next() returns dog, and moves it to between dog and cat, etc. You've finished iterating when the cursor is after snake. So next()... more 7/2/2014 7:58:39 PM

people

Why compiler doesn't tell about compile time exception?

Can any one help me, what will happen when i will execute this program. It should throw compile time error, but its not giving it. public class Test { public static void...
Jon Skeet
people
quotationmark

It's because you have a return statement in your finally block - so the IOException is not actually going to be thrown out of your getCount() method. If a finally block completes abruptly (i.e. it throws an exception or has a return... more 7/2/2014 3:03:08 PM

people

C# generic constraints : Interface

I got a question for some code: interface IDistance<T> { double distance(); double distance(T obj); } class Point<T> where T : IDistance<T> ...
Jon Skeet
people
quotationmark

Look at this code within Point<T>: T obj; public double dist(T val) { return obj.distance(val); When the compiler tries to understand what this expression means: obj.distance(val) it has to resolve the distance member. If T is... more 7/2/2014 11:23:09 AM

people

automatic properties in C#

Need to understand the benefit of using automatic properties apart from less line of codes? Intially we used as below : private int sample; public int Sample { get {return...
Jon Skeet
people
quotationmark

You're still creating a private variable - it's just done for you behind the scenes by the compiler. The variable is given an "unspeakable name", ensuring you can't refer to it in source code. You're still getting all the benefits of... more 7/2/2014 6:49:42 AM

people

Java project does not recognise imports from a generated jar file

I am using the command line jar cvfm server-model.jar nz to generate a jar file called server-model.jar Some output: adding: nz/api/pojos/core/util/(in = 0) (out= 0)(stored...
Jon Skeet
people
quotationmark

Your jar file contains source code - if you want to use a jar file as a library in a project, you need to compile the source code first, and put the class files in the jar file. For example: $ javac -d bin... more 7/2/2014 6:04:21 AM

people

Constructor overloading same arguments

Suppose I have class with 2 fields: x and y, of type double. Is it possible to define 2 constructors so constructor1 will create object setting its x property to what parameter...
Jon Skeet
people
quotationmark

No, you can't do that. Typically you'd do something like: private Test(int x, int y) { this.x = x; this.y = y; } public static Test fromX(int x) { return new Test(x, 0); } public static Test fromY(int y) { return new... more 7/1/2014 8:00:45 PM

people

what does it mean when declare a string with more than one value?

here is an example, public string a_UId, a_ProgramID, a_BuildingID, a_UserName, a_Level; public long b_Linetype= 0, b_TotalLine = 0;
Jon Skeet
people
quotationmark

You're not "declaring a string with more than one value". You're declaring several different variables. It's really important to differentiate between variables and values. So: public string x, y, z; is equivalent to: public string... more 7/1/2014 7:59:18 PM

people