Browsing 7239 questions and answers with Jon Skeet

Unit test a dynamic instance throws exception if not abstracted from a specific base in c#

I have a class similar to the following class ObjectCaller { public void Process (String NameSpace) { Type t = Type.GetType(NameSpace + ".Main"); if (t !=...
Jon Skeet
people
quotationmark

Just test it by passing it a namespace which contains a class called Main which isn't derived from Window. Now you may find that tricky because if Type.GetType(string) is passed a value which is a non-assembly-qualified name, it will only... more 1/14/2016 8:37:14 AM

people

GZipStream works when writing to FileStream, but not MemoryStream

If compress some json text, and write that it to a file using a FileStream I get the expected results. However, I do not want to write to disk. I simply want to memorystream of...
Jon Skeet
people
quotationmark

You're calling ToArray() before you've closed the GZipStream... that means it hasn't had a chance to flush the final bits of its buffer. This is a common issue for compression an encryption streams, where closing the stream needs to write... more 1/13/2016 7:50:08 PM

people

Array of dates returns object System datetime not the actual value of the array

Question is I have a function call "GetDatesBetween" what this does is looks at my two datepickers leave_s_dp and leave_e_dp and gets the dates between them. I then want to turn...
Jon Skeet
people
quotationmark

You're calling ToString() on an array. That doesn't do what you expect it to. It's not clear exactly what you do expect it to do, but it's almost certainly not what you want. You quite possibly want to call string.Join,... more 1/13/2016 3:56:29 PM

people

Is there a replacement for 'as' with value types?

I'm fully aware why ascannot be used for value types. However, the only replacement I am aware of is: if (!(obj is MyValueType)) { // Type check #1 // Exception or...
Jon Skeet
people
quotationmark

You can use: var maybeValueType = obj as MyValueType?; if (maybeValueType != null) { // Use maybeValueType.Value } However, this performs worse than is + casting - or at least has in the past. It's possible that C# 7 will fix this... more 1/13/2016 11:27:03 AM

people

Can I make this implicit conversion from an operator overload work?

I am trying to overload the division operator in a class to return a double. I have two classes: Length and Angle. In the Angle class, I have initializers that accept different...
Jon Skeet
people
quotationmark

A conversion isn't a division - those are two separate operations. You appear to be trying to combine them at the moment. Fundamentally, it seems that you should remove this operator: // Kill this public static Length operator /(Length... more 1/13/2016 10:11:13 AM

people

linq query on list id's

I have a list of id's and I need to get all records correscponding to each id in the list . below is the sample code public List<Items>...
Jon Skeet
people
quotationmark

You may be having trouble due to how you've passed in the IDs. You may find that converting that sequence into a list or an array would help: // Various names to be more conventional/readable public List<Items>... more 1/13/2016 8:25:33 AM

people

Error left hand side of an assignment must be a variable

I am currently converting from Vb6 to C# where the below Vb6 code is allowed : Private Property Let gUnit(Optional bResolve As Boolean, aNoseHi) gNoseLo(Optional parameter) =...
Jon Skeet
people
quotationmark

A "property with a parameter" in C# is an indexer. You can't give it a name like you can in VB though1. You declare it like this: public int this[bool parameter] { get { ... } set { ...} } Now that may or may not be appropriate... more 1/13/2016 7:20:31 AM

people

How to avoid code duplication inside two methods?

I have two identical methods, but one of them have return statement inside try catch public void A(Guid agentId) { var agent = _agentsProvider.GetAgentById(agentId); ...
Jon Skeet
people
quotationmark

Unless GetPreviousCacheEntry could have problematic side-effects, it seems to me that you don't need method A at all. Just call method B and ignore the return value if you're not interested in it. As noted in comments, the methods aren't... more 1/12/2016 5:15:34 PM

people

How to set Date object to point to different time zone in java

In the code below I have used calendar object to initialize time Zone to GMT and get the time accordingly but when I put back in date object, its automatically converting to my...
Jon Skeet
people
quotationmark

its automatically converting to my local time zone i.e. IST No it's not. A Date object doesn't have a time zone - it's just an instant in time. You'll see the system-local time zone if you call toString() on a Date, because that's... more 1/12/2016 4:17:51 PM

people

How should I override equals and hashcode method to avoid adding equal objects o HashSet?

I have studied similar examples and override these methods like this, but still get added to the HashSet<NamedObject> objects with the equal names BUT different id. public...
Jon Skeet
people
quotationmark

I have studied similar examples and override these methods like this, but still get added to the HashSet<NamedObject> objects with the equal names BUT different id. Yes, because you're only using the id in the hash code, and... more 1/12/2016 7:05:12 AM

people