Browsing 7239 questions and answers with Jon Skeet

Why incrementing int primitive in while loop does not loop forever

I have a following code sample int i = 1; while(i != 0) { i++; } I was expecting this to run in an infinite loop but it didn't. Then when I printed the value...
Jon Skeet
people
quotationmark

I was expecting this to run in an infinite loop but it didn't. No, it wouldn't. Eventually the value will become 0 again. In particular, it will logically execute like this: 1 2 3 ... 2,147,483,646 2,147,483,647 -2,147,483,648 //... more 1/13/2014 6:59:32 AM

people

Double to int rounding normal way from .5

I'm making calculation and for that I'm using the following expression: Player.targetx = (int) (((e.getX() - Frame.x)/(Screen.myWidth/World.worldWidth))-8); For example: if...
Jon Skeet
people
quotationmark

Casting to int always truncates towards 0. To round, you can use Math.round() instead. However, that always rounds halves up: class Test { public static void main(String[] args) { System.out.println(Math.round(-7.7)); // -8 ... more 1/12/2014 4:34:43 PM

people

C# Dynamics: Convert.ChangeType versus Cast

Could someone explain why casting a dynamic object as a class returns that class while using Convert.ChangeType returns a dynamic object, particularly at runtime? For instance: ...
Jon Skeet
people
quotationmark

At execution time, there's no such thing as dynamic really. However, the call to Convert.ChangeType is providing a dynamic value as an argument. Any method call using a dynamic argument is treated as having a return value of dynamic,... more 1/12/2014 1:43:27 PM

people

Why is tinyxml's FirstAttribute() return null in simple function?

Running on: Linux Mint 16, QtCreator 3.0.0 with Qt 5.2.0. tinyXML last version. XML working with: <users> <user> <username>testadmin</username> ...
Jon Skeet
people
quotationmark

Your XML doesn't have any attributes. It has text within elements, but no attributes. If your XML had something like: <user name="foo" /> then it would give you an attribute. And you could have multiple attributes of... more 1/11/2014 11:23:25 PM

people

How to set position of MediaElement?

I have this button: private void Answer1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { MediaElement1.Play(); } The problem is that if I click it and then...
Jon Skeet
people
quotationmark

Yes, just set the Position property: MediaElement1.Position = TimeSpan.Zero; MediaElement1.Play(); more 1/11/2014 11:22:16 PM

people

How do i get the cropped integer number

I'm trying to calculate the age based on the DOB. Int32 DOB = 19900427; Int32 current = 20140111; Int32 result = current - dob; Now i just need to display the starting 2 digit...
Jon Skeet
people
quotationmark

Don't do it that way. Just don't. You can't get a useful age representation by subtracting one value from another - you'll find that the difference between two people who were born a day apart can differ massively based on exactly when... more 1/11/2014 5:46:48 PM

people

How to store convert date time in int?

I need to store DateTime in int. So I tried below codes Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy")); or Int64...
Jon Skeet
people
quotationmark

Just use DateTime.Ticks instead - there's absolutely no reason to start converting to and from strings here. long ticks = DateTime.Today.Ticks; // Later in the code when you need a DateTime again DateTime dateTime = new... more 1/11/2014 1:12:26 PM

people

NullReferenceException was unhandled in C# Timer TweetSharp

I am working on a twitter project that lets user to auto-tweet to the mentioned tweets using C#. I am using TweetSharp with C#. The problem is, when I try to refresh the form_load...
Jon Skeet
people
quotationmark

I've come across exactly this problem in the last week. I strongly suspect that it is related to access tokens expiring or other errors: I've regularly seen this occur after my program has been active for 5 minutes (of polling every 20... more 1/11/2014 1:04:00 PM

people

enclosingInstance.newMermberClass(args) in java effective java text

In effective java second edition, the author says, It is possible, although rare, to establish the association manually using the expression enclosingInstance.new...
Jon Skeet
people
quotationmark

It means "create a new instance of MemberClass, using enclosingInstance as the reference for the new instance". An inner class has an implicit reference to its enclosing class - normally if you just call new MemberClass() within an... more 1/11/2014 11:06:52 AM

people

DateTime.Parse changes format

I'll try to illustrate an example: var dateNow = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"); produces: 10/01/2014 21:50:34 var dateNowParse =...
Jon Skeet
people
quotationmark

How to parse the date, and keep formatting You need to keep the format alongside the DateTime if you want to. A DateTime does not have any concept of being in a particular format. The value of the DateTime returned by Parse isn't... more 1/10/2014 8:01:08 PM

people