Browsing 7239 questions and answers with Jon Skeet

Writing to file

I am writing to a file for the first time, but the text on the file comes out completely wrong. Instead of numbers (which it is supposed to print), it prints unrecognizable...
Jon Skeet
people
quotationmark

You're calling Writer.write(int): Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored. My guess is that's not what you want to... more 4/9/2014 7:03:48 PM

people

Java SHA1 hash to base64 : unsigned bytes?

I am trying to hash a value (SHA1) in both C# and Java, and then return a base64 representation. I get 2 different results. I know this is because Java uses signed bytes while C#...
Jon Skeet
people
quotationmark

The problem is very simple... From your C# code: SHA256Managed sha256hasher = new SHA256Managed() SHA-256 != SHA-1. Use the SHA1 class instead in C#, or use SHA-256 in Java as well. As you apparently can't change the C# code, you should... more 4/9/2014 3:51:25 PM

people

Emulate goto in Java

I have code (simplified hypothetical example): public String handleEditRequest(...) { if (isBadArg()) { ... req.getSession().setAttribute("errorMessage",...
Jon Skeet
people
quotationmark

Just wrap the whole method in another one: public String handleEditRequest(...) { Timer timer = new Timer().start(); try { return handleEditRequestImpl(...); } finally { timer.stop(); ... more 4/9/2014 2:17:43 PM

people

In what situation a class is not affected to a accessibility level?

Pretty straight forward here. I want to know when a class in c# (and probably any oo-language) is okay being neither "public", "private", "protected"... I am asking this question...
Jon Skeet
people
quotationmark

The general rule in C# is "the default is as private as you can explicitly declare it". So: namespace Foo { class Bar // Implicitly internal class Bar { int x; // Implicitly private int x void Baz() {} //... more 4/9/2014 2:06:47 PM

people

Does this account for daylight savings?

// someTime is epoch in millis (UTC) final long timeNow = new Date().getTime(); final long midnight = timeNow - timeNow % (3600 * 24 * 1000L); final long yesterdayMidnight =...
Jon Skeet
people
quotationmark

Your current code doesn't do anything with the local time zone - everything is in UTC, effectively (certainly in terms of your code, which is dealing in "milliseconds since the Unix epoch"). If you want to make your code... more 4/9/2014 1:59:01 PM

people

Confused on a LINQ where clause

public ViewResult List(string category, int page = 1) { ProductsListViewModel viewModel = new ProductsListViewModel { Products = repository.Products ...
Jon Skeet
people
quotationmark

It's basically allowing the category filter to be optional - if the category parameter is non-null, then it has to match whatever you're looking at. Otherwise, just include all categories. more 4/9/2014 12:55:59 PM

people

Why does it not work? (send function as parameter by Runnable in java )

I have a project in java, and I have a problem. Description of goal: In Form A (MainForm) I have some buttons in enable(false) mode. I have a function LockOrUnlockButtons()...
Jon Skeet
people
quotationmark

This isn't a constructor: public Void AddCstmrForm(Runnable myFunc) throws Exception { initComponents(); ... } It's a method called AddCstmrForm with a Void return type. I think you meant: public AddCstmrForm(Runnable myFunc)... more 4/9/2014 12:43:18 PM

people

Totalhours method displays minutes in wrong format

I have the below code which displays the totalhours, but the totalhours displays it differently for values less than 60 mins. That is for 35 mins or 45 mins (greater than 30mins)...
Jon Skeet
people
quotationmark

As you're using .NET 4, you can make your life much simpler by using TimeSpan.ToString(string) to do the formatting for you: using System; class Test { static void Main() { int minutes = 31; TimeSpan ts =... more 4/9/2014 12:37:24 PM

people

Static parameters in C# methods

I have doubt in C# about Static class usage in methods. Suppose that we have a method with two parameter int and Enum in another class. public void DemoMethod(int...
Jon Skeet
people
quotationmark

You can never instantiate a TenantType - so the only value you could possibly pass into DemoMethod would be null. Think about how you'd be calling the method - if you were expecting to call (say) DemoMethod(10, TenantType.TenantUser) then... more 4/9/2014 10:41:36 AM

people

Formatting DateTime value in C#

I have a datetime variable like this: DateTime date1 = DateTime.Now; // has 9.4.2014 01:12:35 I want to assign this to another datetime or change its value like...
Jon Skeet
people
quotationmark

The code you've written just assigns a value to a variable. It doesn't return anything, and it doesn't have any inherent string representation. A DateTime value is just a date/time. It can be displayed in whatever format you want, but... more 4/9/2014 10:24:31 AM

people