Browsing 7239 questions and answers with Jon Skeet

IllegalArgumentException using Java8 Base64 decoder

I wanted to use Base64.java to encode and decode files. Encode.wrap(InputStream) and decode.wrap(InputStream) worked but runned slowly. So I used following code. public static...
Jon Skeet
people
quotationmark

I strongly suspect that the problem is that you're ignoring the return value from InputStream.read, other than to check for the end of the stream. So this: while (in.read(inBuff) > 0) { // This always decodes the *complete* buffer ... more 9/30/2015 5:57:44 AM

people

How to compare a given date from today

I want to compare a given date to today and here is the condition: If provided date is greater than or equal to 6 months earlier from today, return true else return...
Jon Skeet
people
quotationmark

Your first problem is that you're using DateTime.Now instead of DateTime.Today - so subtracting 6 months will give you another DateTime with a particular time of day, which is very unlikely to be exactly the date/time you've parsed. For... more 9/29/2015 4:43:43 PM

people

Java: try with resources vs try catch finally about order of autoclosing

Please, consider the following code: class Bum implements AutoCloseable{ public void bu() throws Exception{ System.out.println("Bu"); throw new Exception(); ...
Jon Skeet
people
quotationmark

As ever, the answer is in the JLS - in this case, section 14.20.3.2. Basically, if you have catch or finally blocks in your try-with-resources statement, that's converted into a "normal" try/catch/finally block which contains a... more 9/29/2015 4:26:43 PM

people

Per Invoice show string/int array of unique products

I have a list of Invoices and all the Products on each Invoice. Each Invoice can have multiples of the same product class InvoiceProducts { public int InvoiceID { get; set;...
Jon Skeet
people
quotationmark

You want ToLookup here - it's designed for precisely this scenario. var lookup = list.ToLookup(x => x.InvoiceID, x => x.ProductID); That will still contain the duplicate product IDs, but you can easily make them distinct when you... more 9/29/2015 12:58:43 PM

people

Best way to define constant in asp.net

Which of below 2 method is better to declare constant/config driven variables in asp.net from coding standing perspective, from architecture perspective. Type1- class...
Jon Skeet
people
quotationmark

Firstly, the null-coalescing operator makes this a lot simpler. Next, assuming ConfigurationManager.AppSetting["ConnectionString"] doesn't change over time, you could use: // TODO: Rename this... class Constants { private static... more 9/29/2015 12:19:03 PM

people

How to add many attribute elements in same element?

I want to add many elements with some attributes in same element. Here is my code, XmlDocument doc = new XmlDocument(); XmlElement root =...
Jon Skeet
people
quotationmark

Well basically the for loop should only contain the creation of the GEH element. You can move the creation of the DEF element outside the loop... However, I'd also strongly recommend using LINQ to XML rather than the old XmlDocument... more 9/29/2015 12:17:00 PM

people

predicate not working in join lambda expression

I have using PredicateBuilder class to dynamically creating Where clause in LINQ statements. Following is my code which is When using predicate var predicate =...
Jon Skeet
people
quotationmark

Your predicate is a predicate for tbl_login. But here, you're projecting to an anonymous type: (x, y) => new { userID = x.LNG_USER_PRIMARY_ID, loginTime = x.DAT_LOGIN_TIME, ageGroup = y.INT_AGE_GROUP } That predicate... more 9/29/2015 11:28:56 AM

people

Error:(30, 17) error: code too large solutions?

I am making a simple database of medical components. I made a simple ListView in android studio that contains names of drugs approximately 9000 names. Names are located in an...
Jon Skeet
people
quotationmark

Yes - don't put large amounts of data in source code, basically. Just include a text file with all the names, and load that at execution time instead. (In normal Java I'd use Class.getResourceAsStream; in Android you should probably be... more 9/29/2015 7:32:08 AM

people

How to properly validate file from user and detect extension change?

I have this code public bool IsImage(HttpPostedFileBase file) { var contentType = file.ContentType.Split("/")(0).ToLower() == "image" if (contentType != "image") { ...
Jon Skeet
people
quotationmark

The simplest way to check whether it's an image is to load it as an image, e.g. using Image.FromStream. If that throws an exception, it's not an image (or at least, not a supported image format). I'd trust that more than just using either... more 9/29/2015 7:23:37 AM

people

Force loop to wait for an event

I am trying to write a method that goes through a list of items - for each one adds it to a form, then waits for the user to input some data and click a button. However I have no...
Jon Skeet
people
quotationmark

There are ways of doing this using signalling if you're also using async/await - you could await a task which is completed by the button being clicked for example - but in general you should think about user interfaces in a more... more 9/29/2015 5:55:20 AM

people