Browsing 7239 questions and answers with Jon Skeet

Pass by reference?

Can anyone tell me why all the object.num's print as 1? This is driving me mad. Somehow after the for loop the values of the object.num = 1 no matter what, even though they are...
Jon Skeet
people
quotationmark

Somehow after the for loop the values of the object.num = 1 no matter what, even though they are never set to 1. Yes "they" are - "they're" set to 1 in the last iteration of this loop: for(var temp = n; temp > 0; temp--) { ... more 11/15/2014 9:05:35 AM

people

How to convert a string Date from database into Integer?

I have a string date in database. I did like below mentioned format but it shows error like input string was not in a correct format But when I referred with internet this...
Jon Skeet
people
quotationmark

It sounds like you should be using a DateTime column in the database, at which point there's no need for integers or strings: var today = DateTime.Today; // Or maybe use DateTime.Now // Use parameterized SQL rather than string... more 11/15/2014 8:41:02 AM

people

The blank final field INITIAL may not have been initialized

I'm programming in Java. I have added comments to every method to explain what they're supposed to do (according to the assignment). I have added what I know to the stub of...
Jon Skeet
people
quotationmark

The error is exactly what the compiler says it is - you've got a final field, but nothing setting it. Final fields need to be assigned to exactly once. You're not assigning to it at all. We don't know what the field is meant to represent... more 11/14/2014 5:21:57 PM

people

OleDb where like statement error

I am trying to make a search in a oledb database using an ID (integer) or a name (string) through a windows form application. The user selects the type of search with a combobox,...
Jon Skeet
people
quotationmark

The problem (or at least one problem) is that you're trying to use a parameter for a column name. You can't do that - only values can be parameterized. Column and table names have to be part of the SQL itself. So for example, this: case... more 11/14/2014 3:12:05 PM

people

Controlling DateTime format for XML responses in Web API

How can I control DateTime output format when content is requested as application/xml? The following works for JSON: JsonMediaTypeFormatter jsonFormatter =...
Jon Skeet
people
quotationmark

You shouldn't specify your own format - you should instead tell the XML serializer that the value is just meant to be a date, using: [XmlElement(DataType="date")] Then force the use of XML serializer with... more 11/14/2014 11:25:40 AM

people

At runtime, how does a C# delegate access scope in which it is defined?

The following code is from a main window in a WPF application: public MainWindow() { ... Storyboard myStoryboard = new Storyboard(); ... _button.Click +=...
Jon Skeet
people
quotationmark

The compiler is creating an extra type which stores the local variable. Both the method and the lambda expression then use an instance of that extra type. The code would be something like this: public MainWindow() { CunningCapture... more 11/14/2014 11:14:35 AM

people

Passing SqlConnection to class loses its connectionstring after dispose

I have following simple class definition (a windows console application): SAMPLE as Expected using System; namespace ConsoleApplication1 { class Test: IDisposable { ...
Jon Skeet
people
quotationmark

You're disposing of the SqlConection when you dispose of the Test - but you're then trying to use it again. The best approach is almost always to create a new SqlConnection each time you need one - and dispose of it as soon as you're... more 11/14/2014 7:47:20 AM

people

For looping and string

So I have a string, and in it, I want to replace last 3 chars with a dot. I did something but my result is not what I wanted it to be. Here is my code: string word = "To...
Jon Skeet
people
quotationmark

Look at this: string newWord = word.Replace(word[k - 1], '.'); You're always replacing a single character from word... but word itself doesn't change, so on the next iteration the replacement has "gone". You could use: word =... more 11/13/2014 10:05:42 PM

people

Property or indexer 'AnonymousType#1.FirstName' cannot be assigned to it is read only

I'm trying to make a function that changes the query result value to the values in a textbox. I can run the LINQ query, get the values, and read can print the perfectly... But...
Jon Skeet
people
quotationmark

It's because you're trying to change the value of an anonymous type - there's not such thing as a var type; that's just telling the compiler to infer the type of the variable from the expression on the right-hand side of the assignment... more 11/13/2014 7:21:59 PM

people

Primary constructors no longer compile in VS2015

Until this very day, I could make use of primary constructors, such as: public class Test(string text) { private string mText = text; } To be able to do this, in the...
Jon Skeet
people
quotationmark

Does anyone have any ideas about what could be going on? Yup - primary constructors have been removed from the plans for C# 6. They may well make an appearance in some form in a later version, but they're not in C# 6 any more. See... more 11/13/2014 6:22:26 PM

people