Browsing 7239 questions and answers with Jon Skeet

C# object instance not working properly

I have a class Node as below: public class Node { public Dictionary<string, string> dictionary; public Node(Dictionary<string, string> dictionary) { ...
Jon Skeet
people
quotationmark

because I didn't add an item with key "2" at this.dictionary. Yes you did. You did it with this call: tmp.AddE(tmp, "String2","2"); That adds an entry with key "2" and value "String2" to the dictionary referred to by tmp... which... more 7/3/2016 4:28:34 PM

people

Use of the executeUpdate(String) method is not supported on this type of statement

PreparedStatement pstmt; for (int i = 0; i < cnt; i++) { String squery = "insert into response(seat_no,question_no,ans_opt,screenshot) values(?,?,?,?)"; pstmt =...
Jon Skeet
people
quotationmark

You've already specified the SQL when you prepared the statement: pstmt = con.prepareStatement(squery); You shouldn't then use Statement.executeUpdate(String) - instead, just use the parameterless executeUpdate() method: int num =... more 7/3/2016 2:13:44 PM

people

one of the parameters of a binary operator must be the containing type c#

public static int[,] operator *(int[,] arr1, int[,] arr2) { int sum; int[,] res = new int[arr1.GetLength(0), arr2.GetLength(1)]; for (int i = 0; i <...
Jon Skeet
people
quotationmark

The compiler is already telling you what is wrong - but to quote section 7.3.2 of the C# 5 specification: User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains... more 7/2/2016 9:49:01 AM

people

Why is Regex.Match not matching the same strings as Regex.Matches?

I made a find and replace dialog with a regex option. There is a button to test a regex, highlighting all matches, and a button to find individual matches. With some regular...
Jon Skeet
people
quotationmark

I suspect you have a space or something at the start of the text selected by your RichTextBox. At that point, it makes total sense. Look at your regular expression: (duplicate of )*([0-9]:+)* That will match the empty string. So for... more 7/2/2016 7:14:28 AM

people

Automatically call base method prior the derived one

I have a base class: abstract class ClassPlugin { public ClassPlugin(eGuiType _guyType) { GuiType = _guyType; } public eGuiType GuiType; ...
Jon Skeet
people
quotationmark

The standard solution to this is to use the template method pattern: public abstract class Base { // Note: this is *not* virtual. public void SomeMethod() { // Do some work here SomeMethodImpl(); // Do... more 7/1/2016 8:28:34 AM

people

How to select the latest dateTime of every week ? C#

I have to implement an algorithm who receive a list of DateTime and its recovers the latest DateTime of every week. Example: 29/06/2016 -> Lastest date of the last week of...
Jon Skeet
people
quotationmark

Note: this answer assumes that you're interested in "natural" weeks, which don't really care about the year. For example, December 31st 2015 was a Thursday, and January 1st 2016 was a Friday - to my mind (and using the algorithm in this... more 7/1/2016 8:14:27 AM

people

How assign a method reference value to Runnable

I hava a problem about Java 8 Runnable. public static void main(String[] args) { Runnable r1 = Test::t1; Runnable r2 = Test::t2; Runnable r3 = Test::t3; } public...
Jon Skeet
people
quotationmark

r2 is fine due to section 15.13.2 of the JLS, which includes: A method reference expression is congruent with a function type if both of the following are true: The function type identifies a single compile-time declaration... more 6/30/2016 9:15:32 AM

people

Why is .NET `PixelFormat` has an `Format8bppIndexed` element?

A group of 8 bits can store 256 values. A gray level can have 256 variations. So, why do they need Format8bppIndexed? Why is it Indexed? Why not Format8bpp ?
Jon Skeet
people
quotationmark

It's indexed because each value is an index into a colour table, which may well be RGB or RGBA with considerably more depth. For example, you could have a palette of 32-bit entries (8 bits for each of red, green, blue and alpha), but the... more 6/29/2016 7:08:34 PM

people

Cast of parameterized delegate to non parameterized analogue

Can anyone suggest why I cannot cast non-generic delegate to generic with appropriate type parameters? Particularly having two delegates as shown below public delegate void...
Jon Skeet
people
quotationmark

Simply put, they're different types. Imagine you had two classes like this: public class A1 { public int Value { get; set; } } public class A2 { public int Value { get; set; } } They're different classes despite looking... more 6/29/2016 5:32:39 AM

people

TcpClient getting disposed automatically

Using using (NetworkStream stream = client.GetStream()) Causes the socket to close.
Jon Skeet
people
quotationmark

I believe this is the problem: using (NetworkStream stream = client.GetStream()) That is automatically closing the stream at the end of the block, which in turn (I believe) closes the socket. The documentation is frankly unclear on this... more 6/28/2016 4:09:39 PM

people