Browsing 7239 questions and answers with Jon Skeet

swapping two key/value pairs in Dictionary of C#

here is what I want to do. there is a Dictionary having 54 key/value objects. I want the key/value pair at index i to be swapped with the key/value pair at index j... int i=1;...
Jon Skeet
people
quotationmark

Dictionary<,> instances don't have "indexes" - you shouldn't treat them as ordered at all. Any order you may happen to notice when iterating over entries should be seen as an implementation detail. If you want a specific order,... more 10/3/2013 12:04:57 PM

people

Java ArrayList issue with try catch

I want to clear an ArrayList in try-catch. I do know that we can do it with clear() method.But when I try clear every items with for loop. It show below result: public class...
Jon Skeet
people
quotationmark

The problem has nothing to do with try/catch, and everything to do with this loop: for (int i = 0; i < list.size(); i++) { System.out.println("deleted item: " + list.get(oldsize)); list.remove(i); } That will only remove... more 10/3/2013 8:22:01 AM

people

Calendar Class in Java

I am trying to print HOUR_OF_DAY,Minute and Second using Calendar Class. I used below command in my code. System.out.println( Calendar.HOUR+" "+ ...
Jon Skeet
people
quotationmark

Calendar.HOUR, Calendar.HOUR_OF_DAY etc are just constants used to identify aspects of a date/time. They're typically used like this: Calendar calendar = Calendar.getInstance(); int hour = calendar.get(Calendar.HOUR_OF_DAY); I can't... more 10/3/2013 7:49:17 AM

people

How can I curry an Expression into another Expression?

I'm not sure if this is strictly currying, but I'm basically wanting to achieve the following. Given an Expression: Expression<Func<T1, T2, TResult>> expression I...
Jon Skeet
people
quotationmark

I think you can just derive from the ExpressionVisitor class in a simple way. Here's a proof of concept - it may be overly simplistic, but I think it's what you're after: using System; using System.Linq.Expressions; class Test { ... more 10/3/2013 6:11:54 AM

people

MethodInfo.Invoke TargetException

I've an Issue with System.Reflection, when I call MethodInfo.Invoke method it gaves me the TargetException exception that says: Object does not match with target, Here the...
Jon Skeet
people
quotationmark

You're trying to call the delegate's Invoke method, but on a Tester.Main instance. That's just wrong - because the Tester.Main instance isn't an instance of the appropriate delegate. If you're trying to actually raise the gameVideoLoader... more 10/3/2013 5:49:30 AM

people

NullReferenceExeption in C# when the container is initialized

In the code listed below, I get a "NullReferenceExeption" with the error : "Object reference not set to an instance of an object". I am completely new to C#, but guess the error...
Jon Skeet
people
quotationmark

The problem is that the VTEST_FSUB<V> constructor body is executing before the VTEST_RUN constructor body. So when do_virtual is called, localSMT is still null. Then do_virtual tries to call a method on localSMT, hence the... more 10/2/2013 8:04:02 PM

people

use of wait notify, whats wrong in my approach

I am trying to implement a case of shared variable like semaphores. In that I have 3 thread which should be run sequentially. below is my program. Could u tell me where I am...
Jon Skeet
people
quotationmark

In that I have 3 thread which should be run sequentially. Well you're not running sequentially - you're running the three concurrently, which is the nature of what happens when you start three separate threads. Those threads aren't... more 10/2/2013 7:46:24 PM

people

How do I return form objects that are not null as a receipt?

I have a web application that has about 50 or more fields split up into several panels and I am using them in a asp:wizard across several steps. At the end of the form I want to...
Jon Skeet
people
quotationmark

It's because that's invalid syntax - you want to call string.IsNullOrWhiteSpace and invert the result: .Where(txt => !string.IsNullOrWhiteSpace(txt.Text)) Currently you've got the ! in the middle of the method invocation, which is... more 10/2/2013 6:57:12 PM

people

Get an array of a field within an array of object containing that field

I have an object like this: public class SavedFileInfo { public String Address; public Int32 DataPort; public Int32 AudioPort; public String Description; ...
Jon Skeet
people
quotationmark

Personally I'd use LINQ: var files = mySFI.Select(x => x.Filename) .ToArray(); Alternatively, there's Array.ConvertAll: var files = Array.ConvertAll(mySFI, x => x.Filename); As an aside, I would strongly advise... more 10/2/2013 6:54:48 PM

people

Modifying collections in foreach loop

Quick question: public void ChangeObject(MyClass a){ a.property = 55; } public void Test(){ List<MyClass> obj_list = get_the_list(); foreach( MyClass obj in obj_list...
Jon Skeet
people
quotationmark

Will this make all the values in obj_list have the property 55? Yes - assuming MyClass really is a class. Imagine that the list is really an addressbook - there's the address of a house on every page (element). The list doesn't... more 10/2/2013 5:47:42 PM

people