Browsing 7239 questions and answers with Jon Skeet

Sorting a List with type T

I'm trying to sort a list of type <T> with bubble sort. Unfortunately I have issues with comparing objects with unknown type. What I've tried so far: public static void...
Jon Skeet
people
quotationmark

If you don't need to have anything other than the default comparison, you can use: // TODO: Rename the parameter... public static void BubbleSort<T>(this List<T> array) { IComparer<T> comparer =... more 6/2/2014 11:43:58 AM

people

How to load a class without referring to it?

I am using Java 6. I have static block in one of my class say "MyStaticBlockClass.java". There are no references to that class (MyStaticBlockClass.java) from the rest of my...
Jon Skeet
people
quotationmark

You could use Class.forName("your.pkg.MyStaticBlockClass"). That was how JDBC used to work - the static initializer in the driver class would register itself as being capable of handling specific URLs. Now, that mechanism was superceded... more 6/2/2014 11:39:52 AM

people

C# iterate on two dimensional array of buttons

Im trying to iterate of a two dimensional array of Buttons like this Button[,] buttonsArray = new Button[row,col]; foreach(Button button in buttonsArray) { button = new...
Jon Skeet
people
quotationmark

The compiler message says it all - you're trying to assign a new value to button, but the iteration variable (the one declared by a foreach statement) is read-only. A foreach loop can only be used to iterate over the existing content in a... more 6/2/2014 10:25:21 AM

people

Comparing two different objects using contains(Object o) returns false, when equals(Object o) returns true

[FIXED PROBLEM BELOW] I have this class: Class Doc() { private String D; Doc(String d) { D = d; } @Override public boolean equals(Object o) { if (o...
Jon Skeet
people
quotationmark

You're assuming that contains() will call memberInCollection.equals(candidate) rather than candidate.equals(memberInCollection). The documentation goes against that: More formally, returns true if and only if this list contains at... more 6/2/2014 8:17:05 AM

people

Form transparency from static void C#

Basically, I have a timer that once reaches 5000ms will set the opacity of my main form to 0.5 The problem is that I can't change the opacity of my form from a static void... Here...
Jon Skeet
people
quotationmark

Well your static method doesn't have an instance of the form to work with. The simplest approach is just to stop it from being a static method. There's no obvious reason why you'd want it to be a static method anyway - or why you'd want... more 6/1/2014 9:32:21 PM

people

How to build an anonymous object with expression trees

I have a class public class SomeClass { public int num { get; set; } public string str{ get; set; } } I need to construct a lambda expression like: (x = new {new_num=...
Jon Skeet
people
quotationmark

The approach is basically invalid, because there's no appropriate type to refer to. An anonymous type is just a normal type as far as the CLR is concerned - the compiler just generates the type for you. (In the Microsoft implementation it... more 6/1/2014 6:26:50 PM

people

Multicast delegate of type Func (with return value)?

I have the following code : Func<string, string> func1 = (param) => { Console.WriteLine("Func 1 executing"); return "Hello" + param; }; Func<string,...
Jon Skeet
people
quotationmark

Is it working as intended? It's working as specified, at least. Whether that's what you intended or not is a different matter :) From section 15.4 of the C# 5 specification - emphasis mine: Invocation of a delegate instance whose... more 6/1/2014 5:46:43 PM

people

SocketChannel write does not write all the DATA

I'm trying to send data (400016 bytes) via SocketChannel. for some reason not all the data is being sent. (I expect to see that all the 400016 bytes will be sent) Here's the...
Jon Skeet
people
quotationmark

This is the problem: nNumOfSentBytes += m_socketChannel.write(m_sendBuf); nTotalNumOfSentBytes += nNumOfSentBytes; You don't seem able to decide the meaning of nNumOfSentBytes - on the first line it looks like you're actually treating... more 5/31/2014 5:19:27 PM

people

All uploaded files are not displayed in the links

I have written as protected void btnUpload_Click(object sender, EventArgs e) { HttpFileCollection fileCollection = Request.Files; for (int i = 0; i <...
Jon Skeet
people
quotationmark

You appear to have a single hyperlink object, accessed via a variable called hyperlnk. If you want to have multiple links, you'll need to create multiple links. (You can see all the label text, because you're appending to the label text... more 5/31/2014 12:32:28 PM

people

Pure Speed for Lookup Single Value Type c#?

.NET 4.5.1 I have a "bunch" of Int16 values that fit in a range from -4 to 32760. The numbers in the range are not consecutive, but they are ordered from -4 to 32760. In other...
Jon Skeet
people
quotationmark

It sounds like you really want BitArray - just offset the value by 4 so you've got a range of [0, 32764] and you should be fine. That will allocate an array which is effectively 4K in size (32764 / 8), with one bit per value in the array.... more 5/30/2014 4:20:22 PM

people