Browsing 7239 questions and answers with Jon Skeet

Problems converting image to bytes and then to string

I have a socket communication based application and the server sends a request to a client for a signature image. The signature is taken at the client and sent back to the...
Jon Skeet
people
quotationmark

To do this I have used a picture box, who's data is converted into bytes and then into a string to send over the socket connection to the server. This all works OK. Given the code you've shown, I'd suggest it probably doesn't. In... more 1/14/2015 11:39:42 AM

people

What's happening with this expression? b = a + (a = a + 5)

a = 5 b = a + (a = a + 5) result b = 15 Why the first 'a' do not changes after that (a = a + 5)? But why second one changes? What exactly is happening here by steps?
Jon Skeet
people
quotationmark

Expressions are evaluated left to right - always, regardless of grouping. So it's equivalent to: a = 5; int lhs = a; // 5 int rhs = (a = a + 5); // Increments a by 5 (i.e. a=10), and rhs=10 b = lhs + rhs; // 15 So after this has... more 1/14/2015 7:33:03 AM

people

Which is faster if else or ? :

Which is faster in c# either int a = 10; bool _IsEven; if(a%2 == 0) { _IsEven = true; } else { _IsEven = false; } Or int a = 10; bool _IsEven = a%2 == 0 ? true :...
Jon Skeet
people
quotationmark

Well your second code wouldn't even compile. But you could just write: bool isEven = a % 2 == 0; or bool isEven = (a & 1) == 0; Why use either the conditional operator or an if statement? In general, when you have something... more 1/14/2015 6:58:05 AM

people

Objective of Get Set

Is the only objective of get set is to access a private/protected variable, without breaking concept of encapsulation? Because as per my findings I cannot see any other use of get...
Jon Skeet
people
quotationmark

No - that's usually what it does, but accessors can perform whatever calculation you want. Fundamentally, a property is meant to provide access to logical state of a value - but that logical state doesn't have to translate to a single... more 1/14/2015 6:49:01 AM

people

Is there any way to initialize variables from object and class names?(such as by using reflections)

For example: let us say there is a class Dog , a subclass Dalmatian and the Dog class has private variables String name, breed; Now I say in main: Dog spot = new...
Jon Skeet
people
quotationmark

It's easy to get the breed: public class Dog { private final String name; private final String breed; protected Dog(String name) { // Default to using the class name this(name, getClass().getSimpleName()); ... more 1/13/2015 7:02:27 PM

people

thread safe singleton using enum

I am not able to understand how Enum can be used for thread safe singleton instantiation. So let us say I have a class A which I want to make singleton. How do I do that using...
Jon Skeet
people
quotationmark

In the below shall I replace INSTANCE with A singleObj = new A();? You don't. You use: EasySingleton instance = EasySingleton.INSTANCE; You write the singleton just like a normal class, with whatever methods you want etc - but no... more 1/13/2015 6:55:16 PM

people

How do I build an expression with a certain signature?

I'm trying to call a method with a parameter of type Expression<Func<T, string>> but the expression tree that I build doesn't allow me to call the method. Here's a...
Jon Skeet
people
quotationmark

You're actually already building an expression tree which has the right type at execution time (if you just don't try to call the method, and print out the type of the object that the value of lambda refers to). However, the compile-time... more 1/13/2015 6:48:54 PM

people

Exlude a reference from blocking garbage collection of an object in Java

I have a List in Java which allows to use the same object at multiple places. (Table rows, so they are not cached multiple times in memory). Is there a way to tell the garbage...
Jon Skeet
people
quotationmark

Is there a way to tell the garbage collector, that he is allowed to remove object that are only referenced by this List, and nowhere else? Sort of. You want to make it a List<WeakReference<T>> or something similar. See the... more 1/13/2015 6:11:41 PM

people

How execute a thread before another?

I have a small question for you. I have two threads: HelloThread - This prints "hello" five times. GoodbyeThread - This prints "Goodbye" five times. I would like the...
Jon Skeet
people
quotationmark

Firstly, I'd question the use of separate threads at all here. If you want one thing to happen after another, just use a single thread. However, it's easy to wait until one thread has finished - just use join: Thread t1 = new... more 1/13/2015 1:39:54 PM

people

How do I use same variable inside two different methods without making those variables global?

My simple program will ask user to enter few cities. The user should be able to print them out by choosing another option. Now I have declared an array inside a method (city();)...
Jon Skeet
people
quotationmark

It sounds like your city() method should return the array of cities, which you can then pass to the printCity() method: public String[] city() { ... return cityInArr; } public void printCity(String[] cities) { ... } And in... more 1/13/2015 1:21:12 PM

people