Browsing 7239 questions and answers with Jon Skeet

Multiplying int with long result c#

Wonder why. C# .Net 3.5 int a = 256 * 1024 * 1024; int b = 8; long c = b * a; Console.WriteLine(c);//<-- result is -2147483648 Where does this minus from?
Jon Skeet
people
quotationmark

Where does this minus from? From the integer overflow. Note that your code is equivalent to: int a = 256 * 1024 * 1024; int b = 8; int tmp = b * a; long c = tmp; Console.WriteLine(c); I've separated out the multiplication from the... more 1/26/2015 5:39:58 PM

people

How to access the "findById" method of a RESTful service through "getJSON"?

This is the following code of my RESTful service class: @RequestScoped @Path("/empresas") public class EmpresaEndpoint { @Inject private EmpresaRB empresaRB; @GET ...
Jon Skeet
people
quotationmark

Well without having used that particular framework, it looks like it's mapping to the right method based on the path - it will use findById if the path has an ID, e.g. $.getJSON("rest/empresas/100", function(data) { // ... } (That... more 1/26/2015 5:25:18 PM

people

AtomicBoolean vs Synchronized block, whats the difference

I am trying to understand the difference between the two following code blocks AtomicBoolean ab = new AtomicBoolean(false); using the following to get and set state....
Jon Skeet
people
quotationmark

If all you're trying to do is make getting and setting a single boolean value atomic, then yes - you can use AtomicBoolean instead without any synchronization. Of course, synchronized allows a far wider range of uses, such as performing... more 1/26/2015 5:21:41 PM

people

How to redefine a property in C# through interface inheritance?

I have an interface 'IBase' that specifies a nullable int. A later interface 'IDerived' hides the nullable int and 'redefines' it as non-nullable. interface IBase { int?...
Jon Skeet
people
quotationmark

However, even though it's a private property, I can still access it through the IBase interface! It's not a private property. It's just a property using explicit interface implementation. That means it's public through the interface,... more 1/26/2015 5:16:32 PM

people

DateTime to date conversion, not the correct value

I try to convert a string into a datetime: String dateString = "2015-01-14T00:00:00-04:00"; DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ"); DateTime...
Jon Skeet
people
quotationmark

It's getting the correct value - basically 4am UTC, which is midnight in a UTC offset of -04:00 (as per the original text), or 11pm on the previous day for EST (as per the displayed result). The problem is that you're using... more 1/26/2015 3:46:26 PM

people

ThreadPool.QueueUserWorkItem with function argument/parameter

I have the following method, which I want to run using WaitCallBack delegate (C# Thread-Pooling) technique: public void ExportData(string data){ //Codes goes in here } how to...
Jon Skeet
people
quotationmark

Some options: Declare the method with type string and cast Use a lambda expression instead, e.g. ThreadPool.QueueUserWorkItem(ignored => ExportData(value)) where I assume value is a string variable in scope at the time. This will... more 1/26/2015 10:10:53 AM

people

Java Parameter and Enhanced For Loop copy issue

assume i have code like this; public void insert(Student[] stus) { int count = 0; for(Student s: stus) { s.setId( bla bla); stus[count].setId(bla bla) // is...
Jon Skeet
people
quotationmark

Yes - s is just a variable which contains a value copied from stus. That value is a reference to an object - changes made via s will still be visible from stus. It's only the reference that's copied. So your loop can just be: for (Student... more 1/26/2015 8:33:12 AM

people

C# get object from list where object.value equals

So I have a list of objects from a class. In this list I want to get the object where Table.name == "value" Class Table{ public string name; private string primarykey; ...
Jon Skeet
people
quotationmark

You can easily do it with LINQ, but it won't be more efficient: var match = tables.FirstOrDefault(t => t.name == value); Now match will be null if there are no tables matching that criterion. You need to work out what you want to... more 1/26/2015 8:20:04 AM

people

Java 7 : Does knowing a file is symbolic link or not helps?

In Java 7 it provides me a way to detect whether a file is symbolic link or not , but why anyone would want to know that . Files.isSymbolicLink(target) //here target is a...
Jon Skeet
people
quotationmark

Suppose you're writing a recursive directory copy - you may decide not to follow symbolic links. Or maybe you're creating an archive in a format that doesn't support symbolic links - you may want to warn the user if you encounter one. Or... more 1/25/2015 3:43:44 PM

people

How to display alternative object's representation inside ListBox?

Some time ago I have encountered post in which the author stated that if I have a ListBox the objects inside of it do not have to be represented by string returned by ToString()...
Jon Skeet
people
quotationmark

You can specify the DisplayMember for the ListBox - in this case you'd set it to "Id" so that the control would fetch that property from each item being displayed. Here's a short but complete example (using C# 6 for brevity): using... more 1/25/2015 8:59:27 AM

people