Browsing 7239 questions and answers with Jon Skeet

Trying to understand super() behavior

I'm learning for my java certification and I came across this piece of code. class Feline { public String type = "f "; public Feline() { System.out.print("feline...
Jon Skeet
people
quotationmark

super.type is just referring to the same variable as this.type... there's only one object involved, and therefore one field. When you create an instance of a subclass, it doesn't create two separate objects, one for the superclass and one... more 12/23/2015 8:34:19 AM

people

Round off dateTimePicker value

I am calculating the difference between dateTimePicker2 and dateTimePicker1 and converting it to minutes as this, durinmin = Convert.ToInt32((dateTimePicker2.Value -...
Jon Skeet
people
quotationmark

Having said that I wouldn't expect that behaviour, I've just noticed that Convert.ToInt32 rounds instead of truncating - so it's behaving exactly as expected. The TotalMinutes property is returning 17.6666 (etc) which is being rounded up... more 12/23/2015 8:30:42 AM

people

DataGridView and IEnumerable

I am using LINQtoCsv (Info here), and have hit a snag. I have an IEnumerable set up like that page says: IEnumerable<Character> characters = cc.Read<Character>(file,...
Jon Skeet
people
quotationmark

I suspect it's as simple as setting the DataSource: view.DataSource = characters; Depending on what LINQ to CSV does, you may want to pull everything into a List<T> first: var characters = cc.Read<Character>(file,... more 12/23/2015 7:09:44 AM

people

Convert a byte array from Encoding A to Encoding B

I have a pretty interesting topic - at least for me. Given a ByteArrayOutputStream with bytes for example in UTF-8, I need a function that can "translate" those bytes into another...
Jon Skeet
people
quotationmark

As mentioned in comments, I'd just convert to a string: String text = new String(raw.toByteArray(), encoding); byte[] utf8 = text.getBytes(StandardCharsets.UTF_8); However, if that's not feasible (for some unspecified reason...) what... more 12/22/2015 10:41:40 AM

people

assign null values to properties inside list

I have declared list as below List<myList> mylist= new List<myList>(); Here is class for myList internal class myList { public int ID { set; get; } ...
Jon Skeet
people
quotationmark

I suspect you're looking for the null-coalescing operator here to provide a default value if the existing value is a null reference. // Names changed to look more conventional people.Add(new Person { Id = (int) item["ID"], Name = (string)... more 12/18/2015 12:23:05 PM

people

Why Does The Size Of Binary File Does Not Decrease While Using BinaryWriter

Why does the size Of a binary file not decrease while using BinaryWriter? Consider this first piece of code: static void write() { BinaryWriter sr = new...
Jon Skeet
people
quotationmark

This has nothing to do with BinaryWriter, really - it's the File.OpenWrite call, whose documentation includes: The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For... more 12/18/2015 12:06:31 PM

people

Short if form does not work

(myCondition!="true") ? output("false"); doSomethingElse(); : output("true") Why does the IDE say at the double-dot "Expected semicolon" ?
Jon Skeet
people
quotationmark

The conditional operator isn't a "short if form"... it's an operator, which evaluates either the third operand or the second operand based on the evaluation of the first operand. The result of the whole expression is the result of... more 12/18/2015 9:16:30 AM

people

Crazy behavior in loop c# wpf

I have a really strange behavior in one of my view models. First, in this directory I have three csv files. When I start debugging first it goes inside the foreach and then it...
Jon Skeet
people
quotationmark

Okay, I think I see at least one potential problem (and definitely code to be avoided)... Every time you evaluate the AllDatabases property, you add all the files to the collection. Property fetches shouldn't do that - they shouldn't... more 12/18/2015 9:12:21 AM

people

Why does this code result in a NumberFormatException?

I was trying to run the following line of code: System.out.println("11111111111111111111111111111111 is " + Integer.parseInt("11111111111111111111111111111111", 2)); and the...
Jon Skeet
people
quotationmark

Integer.toBinaryString treats the int value as an unsigned integer, as per the documentation: Returns a string representation of the integer argument as an unsigned integer in base 2. The unsigned integer value is the argument... more 12/18/2015 7:58:42 AM

people

VSCode dependency for XDocument

I am trying to write a C# console app to read XML and process using XDocument. It seems straightforward, but I cannot get the intellisense to recognize and the compiler to build...
Jon Skeet
people
quotationmark

There are two problems here: Your using directive tries to pull in a namespace ending in XDocument. You want using System.Xml.Linq; which is the namespace containing XDocument You need to specify the framework assemblies you need for the... more 12/17/2015 3:54:40 PM

people