Browsing 7239 questions and answers with Jon Skeet

C# equivalent of "value error" in Python

Is there a C# error exception for a value error? the equivalent of the python "value error" below? except ValueError as e:
Jon Skeet
people
quotationmark

Sounds like you probably want ArgumentException, or ArgumentNullException, or ArgumentOutOfRangeException depending on the precise nature. (The latter exceptions are subtypes of the first.) more 2/13/2014 1:40:51 PM

people

Wrong time separator for a C# DateTime on Microsoft Azure

if I use this line of code on an azure website DateTime.Now.ToString(new System.Globalization.CultureInfo("it-IT")) the page prints 13/02/2014 12.08.45 but the time...
Jon Skeet
people
quotationmark

I believe it's to do with the version of the CLR being used. On the CLR prior to v4, the culture information being used did have the time separator as .. On CLR v4+, it's :. I don't know why... presumably CLR v4+ is reading the regional... more 2/13/2014 11:19:37 AM

people

Could a class instance that is not being assigned to a variable get garbage collected too early?

(I don't even know whether my question makes sense at all; it is just something that I do not understand and is spinning in my head for some time) Consider having the following...
Jon Skeet
people
quotationmark

It's somewhat safe. Or rather, it's as safe as if you had a variable which isn't used after the method call anyway. An object is eligible for garbage collection (which isn't the same as saying it will be garbage collected immediately)... more 2/13/2014 10:03:12 AM

people

Add()ing timespan doesn't seem to add them

I have a function that calculates the total working hours by adding TtimeSpans. I tried to search through the web and found something TimeSpan.Add(). Here is some sample of my...
Jon Skeet
people
quotationmark

You're calling Add, but not using the result at all. Add doesn't change the value - it returns a new value. You want: TotalWorkHours = TotalWorkHours.Add(Interval[i]); Or more idiomatically, IMO: TotalWorkHours += Interval[i]; Or... more 2/13/2014 9:48:37 AM

people

How can I generate a random angle in radians in this range?

What I do to generate a random angle is this: float rand_angle = (float)random_.NextDouble() * MathHelper.TwoPi; But I want to generate a random angle from range [a;b],...
Jon Skeet
people
quotationmark

But I want to generate a random angle from range [a;b], excluding everything in the middle. Assuming b is greater than a, that's just a matter of getting a value in the range [0, b - a) and then adding a: float randomAngle = (float)... more 2/13/2014 9:43:37 AM

people

LINQ Query using 'StartsWith' with an array

In My LINQ Query I use Where Clauses to check whether a property exists within an array of values. I would like to update the query: string[] itemColour string[]...
Jon Skeet
people
quotationmark

I suspect you're looking for Any, which allows you to check whether any item in a sequence (the arrays in this case) matches a predicate: var query = from item in allItems where itemColour.Any(c => c.Contains(item.colour)) ... more 2/13/2014 9:40:21 AM

people

Getting error The conversion of a varchar data type to a datetime data type resulted in an out of range value. The statement has been terminated

this is my code sqlcommand=new sqlcommand("inset into table values('"+home.name'"+,'"+home.DOJ'")",con);
Jon Skeet
people
quotationmark

You're using the local default string conversion from DateTime (I assume) to string, and then hoping that's valid for SQL. Additionally, your code has a SQL injection attack vulnerability. Both of these can be fixed - and your code... more 2/13/2014 7:31:37 AM

people

Check environments timezone settings

I want to be able to check against the environments timezone settings and if its not a US timezone then I will handle the datetime with a more global format. For example. I have a...
Jon Skeet
people
quotationmark

The user's time zone and the user's culture are entirely separate matters. It's not clear where this code is running, but basically you want to use the appropriate CultureInfo, and then you can use DateTime.TryParse specifying the... more 2/12/2014 3:54:14 PM

people

Index out of bounds when using a two dimensional array

I am creating a text based minesweeper game in Java. The game works fine if a user creates a perfect square for a game board, however if they enter two different values, I get an...
Jon Skeet
people
quotationmark

This looks suspicious to me: int x = generator.nextInt(rows); int y = generator.nextInt(columns); Given that an x value specifies a column usually, I suspect you want: int x = generator.nextInt(columns); int y =... more 2/12/2014 12:13:30 PM

people

Accessing elments from ArrayList

I've learnt how to Create an Arraylist of Objects , such arrays are dynamic in nature. e.g. to create an array of objects(instances of class Matrices ) having 3 fields, the code...
Jon Skeet
people
quotationmark

You just use the get method: // 2nd element; Java uses 0-based indexing almost everywhere Matrices element = list.get(1); What you do with the Matrices reference afterwards is up to you - you've shown the constructor call, but we don't... more 2/12/2014 9:00:38 AM

people