Browsing 7239 questions and answers with Jon Skeet

Is there any advantage to declaring a class static in C#?

In C#, you can declare a class as static, which requires all members to also be static. Is there any advantage (e.g. performance) to be gained by doing so? Or is it only a matter...
Jon Skeet
people
quotationmark

Or is it only a matter of making sure you don't accidentally declare instance members in your class? It's that, but it's more than that: It prevents instantiation by not having an instance constructor at all (whereas all other... more 3/8/2014 9:00:24 AM

people

How to get the current date format of Android emulator

I want to get the current Date Format of Android emulator. Can anyone help me? Not like this SimpleDateFormat FormattedDATE = new SimpleDateFormat("M-d-yyyy"); Calendar...
Jon Skeet
people
quotationmark

There are various options: DateFormat defaultFormat = DateFormat.getDateInstance(); DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG); DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); // etc And... more 3/8/2014 8:05:13 AM

people

Index is outside bounds of array

When I run this it says that index was outside of bounds for the array but it is not. If I change public static ConectorRec[] ch = new ConectorRec[74]; to ConectorRec[] ch = new...
Jon Skeet
people
quotationmark

When I run this it says that index was outside of bounds for the array but it is not Yes it is. Even if I couldn't pinpoint the problem, I'd always bet on the runtime being more accurate than the developer's opinion on this... This... more 3/7/2014 6:33:49 PM

people

Reuse class / no references

Task: reuse C# code in different projects but without project referencing (don’t want extra dll/references just because of a small utility class). There’re 4 projects, one of them...
Jon Skeet
people
quotationmark

I wonder is there a way to source link files so that the class inside a file gets project-specific unique namespace… Well you could use preprocessor directives: #if PROJECT_FOO namespace Foo #elif PROJECT_BAR namespace Bar #elif... more 3/7/2014 6:32:21 PM

people

Calculate time between the current time and a certain day

I am fairly new to (Java) programming and I am trying to build a little Android app that will tell you if it's already weekend or not. I already got it so far that it can tell...
Jon Skeet
people
quotationmark

Here's the process, but as a description rather than code, leaving the rest for you as an implementation exercise: Get the current time in milliseconds, and remember it for later Create an appropriate Calendar object Work out the day... more 3/7/2014 6:14:28 PM

people

how to convert from image to short string?

I use Base64 system for encode from image to string this code Bitmap bitmap = BitmapFactory.decodeFile(picturePath); ByteArrayOutputStream stream = new...
Jon Skeet
people
quotationmark

You can't. Images typically contain a lot of data. When you convert that to text as base64 it becomes even bigger (4 characters for every 3 bytes). So yes, that will typically be very long if it's a large image. You could compress the... more 3/7/2014 4:49:33 PM

people

Why can I use the shorthand `[Authorize]` instead of the name of the actual class, `[AuthorizeAttribute]`?

I am browsing through the ASP.NET web stack source code and noticed that the AuthorizateAttribute class is actually named as such (see here). So, why is it that I can use...
Jon Skeet
people
quotationmark

The C# compiler automatically tries appending the suffix Attribute when you're using an attribute (which is obvious syntactically). It just makes things easier. From the C# 5 spec section 17.2 (Attribute Specification): By convention,... more 3/7/2014 4:39:11 PM

people

Strange java behavior of wait/notify

I've found strange behavior of java concurrency. See on the next code below: public class Test { static CountDownLatch latch = new CountDownLatch(1); public...
Jon Skeet
people
quotationmark

It's because you're waiting on a Thread instance. Thread uses wait/notify/notifyAll internally, so you shouldn't do that yourself as well - you'll confuse Thread, and Thread will confuse you. In particular, when a thread exits, it calls... more 3/7/2014 4:07:22 PM

people

Methods and Strings

I am new to Java programming. My homework problem states that to Write a program that calls a method to print the number of vowels in a sentence (use ‘nextLine()’to read a...
Jon Skeet
people
quotationmark

The problem is with your method declaration. You need to declare the type of your str parameter, like this: public static String vowel(String str) Additionally, you're only declaring str2 within your loop - so you can't use it for the... more 3/7/2014 1:03:46 PM

people

Assigning a number to a text block

I want to set the Text of a TextBlock according to the Value of a Random Number I thought about using toString for getting the String of the Random number and then assigning it...
Jon Skeet
people
quotationmark

The problem is that you're not calling the ToString method. In this statement: num1.Text = Number1.ToString; ... ToString is a method group, which can be used for conversions to delegate types. So for example, this is... more 3/7/2014 12:38:34 PM

people