Browsing 7239 questions and answers with Jon Skeet

Using a common static value to programatically identify classes

In a reporting application, I have a number of different types of graphs, each of which is output with code in a specific class. Eg. clsPieChart, clsBarChart, etc. Each of those...
Jon Skeet
people
quotationmark

I would suggest having a Dictionary(Of String, Type) instead - that keeps all the information in a single place, and means you can perform the mapping programmatically. You could make another mapping in the other direction as well, of... more 10/8/2013 7:15:45 AM

people

Printing only the first 5 characters of a string in one line

Im trying to print just the first 5 characters of a given string in one line. My problem is the whole string is not being printed. Part of the end is being cut off. int l = 0; ...
Jon Skeet
people
quotationmark

However, the last parts of the string is not being printed. Yes, that's right - because of your loop condition. You're iterating (string.length()/5) times - which rounds down. So if the string has 12 characters, you'll only iterate... more 10/8/2013 6:18:26 AM

people

Switch statements illegal start of type, identifier expected, and orphaned case?

I'm trying to create a program where a pseudorandom number generator generates a number and then passes it to an object, which then calls different methods in other Java files....
Jon Skeet
people
quotationmark

Could anyone suggest what could be wrong? You've put a switch statement in the middle of a class declaration. Statements like this must be in methods or constructors. You haven't given much context here, but you might want something... more 10/8/2013 5:56:52 AM

people

How to overcome lack of friendship in C#?

I quite like the friendship feature of C++. Say, we have a Parent and Child. Child's ctor takes a Parent as a parameter: Child::Child(Parent & parent) { //...
Jon Skeet
people
quotationmark

The only type-based access would be nested types. If you nest one type within another, the nested type has access to all the members of the enclosing class, including private members. The nested type can also be private. For... more 10/8/2013 5:52:13 AM

people

Most efficient way to read two nodes from multiple XML files?

I'm looking for a fast and efficient way to retreive 2 strings from multiple xml files. The files them selves aren't that big only 50kb to 100kb and the number of files can range...
Jon Skeet
people
quotationmark

I would just use LINQ to XML in the simplest possible way: var query = from file in files let doc = XDocument.Load(file) from customer in doc.Descendants("Customer") select new { Company... more 10/7/2013 3:14:38 PM

people

Implementing interface but returning a subclass of the return type?

I am working with ASP.NET MVC in C#. I am creating service classes. I have a service interface for Driver service interfaces (IDriverService). It has the following method...
Jon Skeet
people
quotationmark

You can effectively overload the return type using explicit interface implementation: Driver IDriverService.New() { return New(); // Calls the method below } public SubclassOfDriver New() { return new SubclassOfDriver(); } Now... more 10/7/2013 2:04:10 PM

people

Given a DateTimeZone and two Instants, determine if a LocalTime falls between the two Instants

For each of my users, I store a tzid which I convert to a DateTimeZone holding information about their local time zone. I want to send the user a daily email at 8 AM local time;...
Jon Skeet
people
quotationmark

Given these two Instant called previousRun and nextRun, and the DateTimeZone called tz how would I determine whether the localTime called eightAM falls between the bounds of this job run? Doing this in a general way is somewhat... more 10/7/2013 1:03:55 PM

people

End of file NullPointerException

What I wanted is to reach EOF by typing Ctrl + z from command line with BufferedReader reading from console. The following code does so. But the problem is, it issues a...
Jon Skeet
people
quotationmark

Or more precisely, what is the proper way of reaching EOF with bufferedReader reading from console? Currently you're actually detecting the characters '^' and 'z' it's not like '^' is really a control character. The exception you're... more 10/7/2013 8:08:40 AM

people

Parse csproj via c# cannot parse ItemGroup

I try to get all dll names from csproj file, but cannot get anything! So,i try to get al elements from ItemGroup tag with liq query: var elem =...
Jon Skeet
people
quotationmark

You're ignoring this: xmlns="http://schemas.microsoft.com/developer/msbuild/2003" That sets the default namespace for descendants. So you want: XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003"; var elem =... more 10/7/2013 6:22:18 AM

people

Add comma between values by using Linq

My values come from ComboBox: 2|722|742|762|77 I delete unnecessary characters as follows: foreach (var item in checkListBox) { string[] list = item.Split( new...
Jon Skeet
people
quotationmark

It sounds like you just want string.Join: string commaSeparated = string.Join(",", list); (Note that this is not part of LINQ - it's not the same kind of "join" as for joining multiple sequences. It's joining several strings together... more 10/6/2013 5:18:14 PM

people