Browsing 7239 questions and answers with Jon Skeet

are destructors necessary in C#?

I have a concern. I'm a first year student of computer science. Normally I'm very inquisitive in class but, not always my teacher has an answer, or not always knows the answer....
Jon Skeet
people
quotationmark

Destructors (or finalizers) are good to have in the language - but you should almost never use them. Basically you should only need them if you have a direct handle on an unmanaged resource, and not only is that incredibly rare, but using... more 4/12/2014 6:23:37 AM

people

Referencing an object more than once in a line

So I'm writing my version of Tetris. Here's what I'd like to do: screen[0][1].setColor(Color.RED); screen[0][1].setStatus(1); Is there a way to do this in only one line?
Jon Skeet
people
quotationmark

A few options spring to mind: Write a method in whatever class this is which knows about both the status and the color: // Implicitly sets color to red. Consider using an enum for statuses screen[0][1].setStatus(1); Write a method... more 4/12/2014 6:19:03 AM

people

Linq/C#: How to split List into variable length chunks based on list item information?

I am trying to split up a list of type Record in Linq into sub lists based on certain Type information. There is always one record with type "a" before and one with type "b" after...
Jon Skeet
people
quotationmark

You can't cleanly do this with normal LINQ, as far as I'm aware. The streaming operators within LINQ rely on you being able to make a decision about an item (e.g. whether or not to filter it, how to project it, how to group it) based on... more 4/11/2014 10:45:56 PM

people

Windows javac path env issues

I have my javac under my project directory. Lets say at C:\workspace\myworkspace\java\bin\javac what i want to do is, in ant, use that particular javac for building. so i...
Jon Skeet
people
quotationmark

Two potential problems... Firstly, it's possible that this: <exec executable="java\bin\javac"> ... is making Ant look for a file called java\bin\javac within the directory c:\workspace\myworkspace, where it doesn't exist. It's... more 4/11/2014 10:27:54 PM

people

Why is following Java naming conventions not required for compilation

Why is it not needed for a Java application to follow the Java naming conventions? In other words, why doesn't the Java compiler raise an error when the conventions are not...
Jon Skeet
people
quotationmark

Why is it not needed for a Java application to follow the Java naming conventions? Because they're conventions rather than rules. It would be very odd for naming conventions to be enshrined in a language specification as actual rules... more 4/11/2014 6:51:57 PM

people

Webservices : Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement'

I have a scenario where i have created a web service, which returns the first name and last name of the username passed through client(webservice consumer). but im getting an...
Jon Skeet
people
quotationmark

Well the error message seems reasonably clear - GetUserDetails is returning XElement (from LINQ to XML) rather than XmlDocument (from the older API). This shouldn't be a problem though, as basically you just need to select an element and... more 4/11/2014 12:53:58 PM

people

C# LINQ to objects query to change the field value based on List

I have the follwing objects objItem (id,name,qty) - list<items> objSel(selId) - list<int> objSel.selId is the selected item id of objItem. How to write...
Jon Skeet
people
quotationmark

Your pseudo-code is quite confusing, but I suspect you want something like: List<Item> items = ...; List<int> selectedIds = ...; foreach (var item in items.Where(x => !selectedIds.Contains(x.Id))) { item.Quantity = 0;... more 4/11/2014 10:07:04 AM

people

Where should I declare my variables?

When declaring my variables at the beginning of the class, I can't use them in a conditional statement in other methods. I tried declaring them inside the method it works but I...
Jon Skeet
people
quotationmark

There's no "one size fits all" answer here because there are different reasons for declaring variables. Static variables (declared directly within the class, with the static modifier) are class-wide, rather than specific to a single... more 4/11/2014 8:25:02 AM

people

Get Distinct Category Name From Class

I have a class with following structure :- public class Gallery { private string _category; public string Category { ...
Jon Skeet
people
quotationmark

If you just need the category names, you need to project from the list of galleries to a sequence of category names - and then Distinct will work: // Variable name changed to match C# conventions List<Gallery> galleryList =... more 4/11/2014 6:16:16 AM

people

Getting data value using date condition

I get Syntax error (missing operator) in query expression string strSql2 = "Select N_Serie,MacID from " + cmdb_ModelStock2.Text + " WHERE Date_Ajout = " + cmdb_Date2.Text; I...
Jon Skeet
people
quotationmark

The first thing to do is to stop constructing your SQL like that. If you really need to pick the table dynamically, you should make sure you use a whitelist of valid ones... but for the "where" clause you should use parameterized SQL -... more 4/11/2014 2:14:20 AM

people