Browsing 7239 questions and answers with Jon Skeet

Invoke code from string. parameter not accepted

I wish to invoke code that is in selected txt file. It works fine until file content is something simple like "Text string". It also works if i pass string parameter into it. But...
Jon Skeet
people
quotationmark

You haven't included a reference to the current assembly, which is the assembly declaring Global. Therefore the compiler has no idea which type you're talking about. You need to set the ReferencedAssemblies property in the... more 12/20/2013 8:15:03 AM

people

Resuming an program where it left off after deserialization

I have a program that executes events at different time intervals. Each event is assigned an eventTime like this: public abstract class Event implements Serializable{ private...
Jon Skeet
people
quotationmark

You would need to store not just the events, but also the time at which you serialized. Then after deserializing, work out the difference between the current time and the serialization time, and update all the events accordingly. So... more 12/20/2013 8:02:06 AM

people

Wrapping method to make it generic with map method

I have a method that looks more or less like this: ConcreteType Find(IEnumerable<ConcreteType> values) It iterates an IEnumerable and at some point will return one of the...
Jon Skeet
people
quotationmark

This answer is equivalent to that of C.Evenhuis, but uses an iterator block instead of a whole separate class: T Find(IEnumerable<T> values, Func<T, ConcreteType> map) { var dictionary = new Dictionary<ConcreteType,... more 12/19/2013 5:23:27 PM

people

AreEqual comparing objects with ToString

I'm using Assert.AreEqual to compare two objects. The objects are two different instances of the same class and there is a ToString method in that class. When I call AreEqual I...
Jon Skeet
people
quotationmark

ToString is simply called to report the expected and actual values. It's not what determines equality. That's the Equals(object) method, which you should be overriding in order to provide the equality semantics you're interested in. (You... more 12/19/2013 4:07:50 PM

people

Can I / Should I serialize a constant variable in a C# class?

I am implementing the ISerializable interface for my class. I have a constant variable in the class like: public const decimal Cost = 3.2M When I implement the GetObjectData...
Jon Skeet
people
quotationmark

When I implement the GetObjectData method, can I / should I serial this variable? Absolutely not. Even if you did write it out, it's not like you could change the value of the constant when you read it back in again. More generally,... more 12/19/2013 4:03:19 PM

people

abstract class with default constructor and class with private constructor difference

what's the difference between an Abstract class with a default constructor and a class with private constructor? I also have another doubt , consider this program below. Can...
Jon Skeet
people
quotationmark

what's the difference between an Abstract class with a default constructor and a class with private constructor? If there's a default constructor, it will be callable from subclasses. If it's private, you'll only be able to create... more 12/19/2013 3:15:46 PM

people

Huge List<double> save in small file

I am trying to save a huge list of doubles in a file. For now it looks like this: try{ FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.OpenOrCreate); ...
Jon Skeet
people
quotationmark

The saved file has around 15MB, which I think is quite a lot for only binary data. Well, a double is 8 bytes of data: The Double value type represents a double-precision 64-bit number You've got 2 million of them, so that means... more 12/19/2013 3:09:41 PM

people

How to access subscribers to PropertyChanged event?

While debugging in Visual Studio 2013, I'd like to know the number of subscribers to the event PropertyChanged published by a certain class (let's call it Publisher) that...
Jon Skeet
people
quotationmark

How can I access the subscribers to PropertyChanged? You don't, basically. An event only supports subscribe and unsubscribe functionality. I've researched a bit and found that this should be possible calling GetInvocationList()... more 12/19/2013 1:04:30 PM

people

Extension method must be defined in a non generic static class Pulling XML from document

I'm currently writing up an upload system that takes XML from the document and will display it on a web page. The issue I'm facing, is that whenever I'm adding the XML extraction...
Jon Skeet
people
quotationmark

Well yes, this is the problem: public static XDocument GetXDocument(this OpenXmlPart part) That's not being declared within a top-level static non-generic class, which extension methods have to be. I would strongly advise you to stop... more 12/19/2013 11:27:56 AM

people

string not recognized as valid dateTime, when it appears?

I'm getting the error "String was not recognized as a valid DateTime." When I debug through the code at the line: DateTime targetDate = DateTime.ParseExact(TargetDate,...
Jon Skeet
people
quotationmark

Holding the cursor over 'TargetDate' displays todays date 'Thu 19 December' Well that explains it. The string "Thu 19 December" is not in the form "yyyy-MM-dd" is it? You could specify a format string of "ddd dd MMMM" but that... more 12/19/2013 10:43:24 AM

people