Browsing 7239 questions and answers with Jon Skeet

How to Distinguish an Object from Static Field and an Object from Non Static Field/Variable

class A { static A STATIC_A = new A(); A() {} } boolean isComingFromStaticField(Object a) { // returns true if a comes from static field, for example A.STATIC_A //...
Jon Skeet
people
quotationmark

There's no such thing as a "static instance" and a "normal instance". There are only static variables (and methods etc). So for example: public class Foo { static Object bar = new Object(); Object baz = bar; } Here the values of... more 12/17/2013 9:55:09 AM

people

Input string not in correct format to parse into DateTime

I am facing problem while converting DataTime into Time on 12 hour clock machine. Following code works fine on 24 Hour clock machine. (new DisplayReminder(_name, _displayText,...
Jon Skeet
people
quotationmark

It's not clear what you're trying to do, but just getting the time of day shouldn't involve string conversions: TimeSpan time = _stateTime.TimeOfDay; I'd strongly advise you to avoid string conversions unless they're inherently part of... more 12/17/2013 9:52:26 AM

people

Task continuation blocking UI thread

I am writing a continuous polling loop to watch for some events to happen and then take some action on UI thread. I am writing following code public static void HandlePopup(this...
Jon Skeet
people
quotationmark

You're explicitly telling the continuation to run in the current synchronization context by specifying TaskScheduler.FromCurrentSynchronizationContext() So yes, that will block the UI thread because it's running in the UI thread... more 12/17/2013 8:22:43 AM

people

two of three byte different in sha256 hash function

I am using this function to calculate the SHA 256 public static String getSHA1(String plainText) { MessageDigest md; try { md =...
Jon Skeet
people
quotationmark

This is the problem: hexString.append(Integer.toHexString(0xFF & bytes[i])); This will lose any leading 0s - in other words, any byte less than 16 will come out as a single hex digit instead of two. There are plenty of fixes for... more 12/17/2013 8:18:23 AM

people

Improve LINQ query performance

Assuming there is a class public class StopTime { public TimeSpan? ArrivalTime { get; set; } public TimeSpan? DepartureTime { get; set; } public string StopID { get;...
Jon Skeet
people
quotationmark

It sounds like you want a lookup: var stopTimesByTripId = StopTimes.ToLookup(st => st.TripId); Or to narrow it down by ListOfTripId first: var tripIdSet = new HashSet<string>(ListOfTripId); var stopTimesByTripId =... more 12/17/2013 8:11:53 AM

people

XML issue with array indexing

<?xml version="1.0" encoding="ISO-8859-1"?> <servers> <server serverid="server1" asset="Sample" > <port portid ="port1" asset="Sample" ...
Jon Skeet
people
quotationmark

If you're using .NET 4, you can just use Zip: var server = new XElement("server", new XAttribute("serverid", ServerId), new XAttribute("name", ServerName), PortName1.Zip(PortId1, (name, id) => ... more 12/17/2013 7:03:45 AM

people

How to read whole the text in text file except last line?

I have write a code to print the whole text in text file but i couldn't know how to enable it to read the whole text except last line The Code : public class Files { /** *...
Jon Skeet
people
quotationmark

The simplest approach is probably to print the previous line each time: String previousLine = null; String line; while ((line = reader.readLine()) != null) { if (previousLine != null) { System.out.println(previousLine); } ... more 12/16/2013 5:14:39 PM

people

Get notified when static property changes in wpf

Here microsoft described that in wpf 4.5 we can use INotifypropertyChanged for static properties as well. So I tried to do that. Here is the code: public static event...
Jon Skeet
people
quotationmark

Unless anything uses the sender parameter, it won't matter. Logically it would make sense to use the type: handler(typeof(TypeDeclaringEvent), new PropertyChangedEventArgs(PropertyName)); EDIT: Note that in the document you referred to,... more 12/16/2013 1:28:12 PM

people

Restrict lambda expression argument to the properties of a class

I have a base class which needs to process the names of the properties of derived classes (explicit access). To do so, I created a method which takes a lambda expression as...
Jon Skeet
people
quotationmark

How can I restrict the GetMemberName method so that it can accept only members of the classes which derive from my base class? It's not clear what you mean by "accept" here, but if you're trying to restrict the set of valid lambda... more 12/16/2013 8:52:50 AM

people

Convert Stream data to custom object in C#.net

I want to convert stream data from response stream into a custom object. I want to convert respose stream into custom object,I am following these steps. My code is as...
Jon Skeet
people
quotationmark

The trouble is that you're trying to deserialize an object when you've already read all the data from it just beforehand: readStream = new StreamReader(receiveStream); Console.WriteLine (readStream.ReadToEnd()); After those line, the... more 12/16/2013 6:48:15 AM

people