Browsing 7239 questions and answers with Jon Skeet

Use dictionary instead of List get ith entry

Perhaps I am just having my dumb moment right now. I have the following: List<DirectoryEntry> list = new List<DirectoryEntry>(); List.Add(startEntry); int...
Jon Skeet
people
quotationmark

Well you can use list.ElementAt(i) to get the ith element in the order they happen to be returned by the dictionary - but there's no guarantee as to what order that is, or that it will stay consistent over time. I would strongly recommend... more 12/10/2014 3:56:35 PM

people

Get the first numbers from a string

I want to get the first instance of numbers in a string. So I got this input string which could be one of the following: 1: "Event: 1 - Some event" 2: "Event 12 -" 3: "Event:...
Jon Skeet
people
quotationmark

It seems to me that you just need a regular expression: using System; using System.Text.RegularExpressions; public class Test { static void Main() { ExtractFirstNumber("Event: 1 - Some event"); ... more 12/10/2014 12:12:19 PM

people

Cast error on same Class on recompile

I am getting a strange error when I am upating a project on a website. [A]StDataAccess.AgentInfo cannot be cast to [B]StDataAccess.AgentInfo. Type A originates from...
Jon Skeet
people
quotationmark

Basically you've updated the assembly - the objects in your session are from the "old" assembly, and you can't cast to a type in the "new" assembly, as types are tightly bound to their originating assembly. Options: Be prepared to throw... more 12/10/2014 11:43:27 AM

people

How to convert file to base64 UTF 8 little endian

Good day! I convert binary file into char array: var bytes = File.ReadAllBytes(@"file.wav"); char[] outArr = new char[(int)(Math.Ceiling((double)bytes.Length / 3) * 4)]; var...
Jon Skeet
people
quotationmark

You don't have any UTF-8 here - and UTF-8 doesn't have an endianness anyway, as its code unit size is just a single byte. Your code would be simpler as: var bytes = File.ReadAllBytes(@"file.wav"); string base64 =... more 12/10/2014 10:44:57 AM

people

How is it possible for me to call HasValue on a null object?

Consider the following code: int? myInt = null; var hasValue = myInt.HasValue; This is perfectly acceptable, and HasValue will return false. But how have I managed to reference...
Jon Skeet
people
quotationmark

In the first case, you have a value of type Nullable<int>, which is "the null value for the type". The Nullable<T> type is a value type with two fields, like this: public struct Nullable<T> where T : struct { private... more 12/10/2014 9:35:20 AM

people

Create a new instance of a class type only known at runtime, and cast it, based on a given object

I want to create a new instance of a class type only known at runtime, and cast it, based on a given object. I have an "if" statement to make it, since the possible class types...
Jon Skeet
people
quotationmark

For all but the casting part, you can just use: object newInstance = Activator.CreateInstance(car.Color.GetType()); If that will always implement a particular interface or have a particular base class, you could cast to that - but you... more 12/10/2014 9:21:37 AM

people

Calender in java returning the same date when changing the time zone?

I am working on a project where I have to manipulate the dates. Here is the code snipper I have My time zone is IST and the conversion time zone is CST. public class Sample...
Jon Skeet
people
quotationmark

You're printing out a java.util.Date (the result of calling getTime()). A Date doesn't include time zone information at all - Date.toString() always displays the point in time represented by that Date in the default time zone. Basically,... more 12/10/2014 7:45:17 AM

people

Check if Index value is > 1 of XElement attribute value

I have a variable that gets all the values of my xml and I would like to check to see if the index of it is greater than 1. Here is my variable var IdList =...
Jon Skeet
people
quotationmark

If you're after the count of the value elements, you can just use: var count = IdList.Count(); if (count > 1) { ... } Or if you don't need the count afterwards, you can just see whether there are any left after you've skipped... more 12/9/2014 6:48:17 PM

people

"Error: Not all code paths return a value."

My code, upon compilation, throws the titular exception. I don't understand why this happens because after extensive searching the reason the error occurs is seemingly only when...
Jon Skeet
people
quotationmark

Your code is effectively: bool CheckExisting() { // Some setup code for (int i = 0; i < fCount; i++) { // Code which isn't terribly relevant return ...; } } Now the C# 5 language specification section... more 12/9/2014 2:49:15 PM

people

compile time and run time binding in method overloading and overriding

According to my understanding: Private, final and static methods of a class follow compile time binding i.e. which method will be called is decided at compile time. But, call to...
Jon Skeet
people
quotationmark

Question: In the call itemC.equals(itemB), why the equals method of Object class is getting called. Because the compile-time type of itemC is Object. Overriding is performed at execution time based on the actual type of the target... more 12/9/2014 2:01:45 PM

people