Browsing 7239 questions and answers with Jon Skeet

Add New Code Block Dynamically To Action Delegate in .Net

I have encountered a problem about injecting new code block to an action delegate. For instance, i have a method like this: private void methodAsync(Action action) { ...
Jon Skeet
people
quotationmark

It sounds like you just want: private void methodAsync(Action action) { new Task(() => { writeMessage("Task started"); action(); writeMessage("Task completed"); }).Start(); } In other words, you're not... more 11/27/2013 7:50:50 AM

people

c# TrimEnd removes more than needed?

Here is debugger. I don't understand why TrimEnd delete 's' character. After TrimEnd() Any suggestions? recPath is private string in class. code: recPath = ""; ...
Jon Skeet
people
quotationmark

The problem is that you're passing in lastkey.ToCharArray() as the list of characters to trim. That includes the character s, so the s of Fonts is being trimmed as well. (Ditto the backslash.) From the docs for TrimEnd: The TrimEnd... more 11/27/2013 7:44:09 AM

people

Can you convert this Linq statement into Lambda without using join statements?

I see this all over the web, however I'm curious if there isn't an easier way to write this in lambda? var x = from c in db.Client from p in db.Prospects from ct in...
Jon Skeet
people
quotationmark

Basically multiple from clauses contribute SelectMany calls. So your code is something like: var x = db.Client .SelectMany(c => db.Prospects, (c, p) => new { c, p }) .SelectMany(z => db.Countys, (z, ct) =>... more 11/26/2013 5:42:00 PM

people

Cannot find xml element using linq query in Portable Class Library

in an console project I create an XDocument and using linq to get xml elements. See following code. I try to use the same in a portable class library but it does not work. Is...
Jon Skeet
people
quotationmark

You're looking for an element called title - but it's actually an attribute. The same code would fail on desktop too. You want: title = (string)el.Attribute("title") It's not really clear why you need an anonymous type here - you could... more 11/26/2013 11:33:47 AM

people

Syntax error on token "<=", invalid AssignmentOperator

I'm attempting to fix a plugin I wrote a long time ago for Craftbukkit, but I'm stumped on one section. I've searched Google with little luck, and I've asked other Java developers...
Jon Skeet
people
quotationmark

Basically this is the wrong way round: for (; result.next(); i <= rsmd.getColumnCount()) It should possibly be: for (; i <= rsmd.getColumnCount(); result.next()) Although more likely, you actually want: while (result.next())... more 11/25/2013 9:59:05 PM

people

Casting Stack to Iterable

In the following code, the method returns a Stack object which gets casted to an Iterable. public Iterable<Integer> getRoute(int x) { Stack<Integer> stack = new...
Jon Skeet
people
quotationmark

There's no actual casting here - just an implicit conversion from Stack<Integer> to Iterable<Integer> because Stack<E> implements Iterable<E> (implicitly, by extending Vector<E>, which extends... more 11/25/2013 5:08:41 PM

people

Message: Invalid byte 1 of 1 byte UTF 8 sequence in hadoop

I'm parsing XML using Hadoop, and I got the code from here. But I'm getting the following error: FINISH_TIME="1385387129970" HOSTNAME="DEV140" ERROR="java.io.IOException:...
Jon Skeet
people
quotationmark

I suspect this is the problem - it's at least a problem: XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new ByteArrayInputStream(document.getBytes())); That call to getBytes will use the... more 11/25/2013 2:03:57 PM

people

Get a List<String> from List<Object> in C#

Is there a way to extract the Color type as a list from this List? public List<object1> Color1 = new List<object1>() { new object1{Color=(Color)new...
Jon Skeet
people
quotationmark

LINQ is your friend: List<string> names = Color1.Select(x => x.Name).ToList(); Or using List<T>.ConvertAll: List<string> names = Color1.ConvertAll(x => x.Name); Personally I prefer using LINQ as it then works... more 11/25/2013 1:25:05 PM

people

The given path's format is not supported using File.ReadAllText

I have a WPF application. I am loading some data from XML file. I receive an error: System.NotSupportedException was unhandled Message: The given path's format is not...
Jon Skeet
people
quotationmark

File.ReadAllText is meant to take a filename on a file system - not a URL. You'll need to fetch it with something like WebClient.DownloadString: string text; using (WebClient client = new WebClient()) { text =... more 11/25/2013 11:46:36 AM

people

Is it possible to write to the middle of a file using Filestream in C#

Wanting to know if it is possible to insert text into the middle of a file using FileStream in .NET/C#. If not, is there another way of doing it? I see the option of Truncate and...
Jon Skeet
people
quotationmark

No - this is fundamentally a limitation of most file systems. I would recommend the approach you're suggesting (selective copying from the original file to a new file) but then just rename the files rather than copying back over the top... more 11/25/2013 11:39:44 AM

people