Browsing 7239 questions and answers with Jon Skeet

Get two linq queries into one listbox

I need to get the values of two LINQ queries in to one listbox. Currently I have it going in to two separate listboxe's side by side so it works but is not optimal. The data is...
Jon Skeet
people
quotationmark

It sounds like you just want: var items = dataTable.Select(p => string.Format("{0} : {1}", p.NAME, p.TOTALS)); listBox1.Items.AddRange(items.ToArray()); more 9/20/2013 8:24:11 PM

people

C# XDocument / linq to object

How can i import xml elements to object? My code below doesn't work, it fails at the SetValue and i can't figure out why. But even then, i suspect that linq has a much cleaner...
Jon Skeet
people
quotationmark

You're asking for properties - but your type only has fields. Either make them properties, like this: public string name { get; set; } ... or use Type.GetField instead. In terms of making it prettier, I'd personally add a static... more 9/20/2013 3:27:12 PM

people

Convert string to Time

I have a time that is 16:23:01. I tried using DateTime.ParseExact, but it's not working. Here is my code: string Time = "16:23:01"; DateTime date = DateTime.ParseExact(Time,...
Jon Skeet
people
quotationmark

"16:23:01" doesn't match the pattern of "hh:mm:ss tt" - it doesn't have an am/pm designator, and 16 clearly isn't in a 12-hour clock. You're specifying that format in the parsing part, so you need to match the format of the existing data.... more 9/20/2013 2:42:36 PM

people

limit or audit the use of an interface

I have 3 assemblies: A defines an interface B references A and uses that interface C also references A how can I make sure C does not use that interface? splitting...
Jon Skeet
people
quotationmark

You could make the interface internal, and use InternalsVisibleToAttribute to allow B access to the internal members of A (by adding the attribute to A). It's very coarse-grained though - you can't do it for individual members; it's all or... more 9/20/2013 1:47:58 PM

people

How to test singletons in one test class?

I want to test the behavior of a singleton class with following methods: public class SomeSingleton { private final static int DEFAULT_VALUE1 = ...; private final static...
Jon Skeet
people
quotationmark

Fundamentally singletons are hard to test, precisely because of this sort of thing. You could add a clearStateForTesting method: static void clearStateForTesting() { instance = null; } ... but I'd suggest that you avoid the... more 9/20/2013 1:21:20 PM

people

Can't access members of a class after unboxing

I am learning advanced C#.In the following Code i am trying to do Event handling i get Error while accessing members of class sender after unboxing //Compiler is not...
Jon Skeet
people
quotationmark

This is the problem: object obj=new object(); obj=sender as Sender; The second line doesn't change the compile-time type of obj. It's still just object. (And your first line that creates a new object is completely pointless.) The... more 9/20/2013 1:07:49 PM

people

NullPointerException in PrintWriter[] array Java

I have a problem which i am trying to figure out for days now. Simple Code Overview(PseudoCode): 1 - When a user connects create a PrintWriter for him. 2 - Store the...
Jon Skeet
people
quotationmark

As far as I can tell, this is the problem (in Server): new Thread(new ServerHandler(klientSocket)).start(); Here you're creating a new ServerHandler. That's completely separate from the instance of ServerHandler which you're creating in... more 9/20/2013 12:40:25 PM

people

Assign Object array with Integer elements to Integer array

I searched the internet but didn't found any appropriate solution. In my application I've got an array of integers. I need to access (assign to) the array via reflection. The...
Jon Skeet
people
quotationmark

No, you can't cast a value which is actually a reference to an instance of Object[] to an Integer[] variable - and that's a good thing. Imagine if that were valid... consider: Object[] values = { new Integer(5), new Integer(10)... more 9/20/2013 12:18:49 PM

people

XPath / linq to xml: xmlns=""testNS"" results in no result

a xml file/string can be validated but yields no results if queried by XPath or linq to xml. The difference is: xmlns=""testNS"" xsi:... in the example strings. Without this...
Jon Skeet
people
quotationmark

If you're going to query elements that have namespaces using XPath or LINQ to XML, you have to specify that namespace. For example: XNamespace ns = "testNS"; var root = XElement.Parse(xmlString); return root.Descendants(ns +... more 9/20/2013 12:14:21 PM

people

Determining if a UTC time is within DST

In C#, given a UTC time, how can I determine whether this falls within DST in Houston, Texas, US? var utcDateTime = new DateTime(2013,1,1,0,0,0,DateTimeKind.Utc); //bool...
Jon Skeet
people
quotationmark

Get the appropriate TimeZoneInfo for Texas, then use IsDaylightSavingTime. using System; class Test { static void Main() { // Don't be fooled by the "standard" part - this is Central Time var zone =... more 9/20/2013 12:00:17 PM

people