Browsing 7239 questions and answers with Jon Skeet

How to make a class visible in another class?

My problem : In a ReadFile class, I have a public Module ReadTheFileGetTheModuleName(String fichier) method which has to return an object from a Module Class. In the method, I...
Jon Skeet
people
quotationmark

The problem is that you're declaring the module variable in a nested scope, but then trying to use it outside that scope: while (...) { if (...) { Module module = ...; } } return module; What would you expect to happen... more 3/18/2015 2:34:43 PM

people

What is the correct approach when trying to remove some items in ConcurrentDictionary

Is this better: public void Test() { ConcurrentDictionary<int, string> dictionary = new ConcurrentDictionary<int, string>(); ...
Jon Skeet
people
quotationmark

I don't see any benefit in the first approach. I'd just use LINQ to find the items though: foreach (var entry in dictionary.Where(e => e.Value == "A")) { string ignored; // Do you actually need to check the return value? ... more 3/18/2015 2:23:08 PM

people

Insert c# array in json

I have and array in C# and I need to insert it in json: I have: int[] a = new int[3]; JObject something=...; a[0]=12; a[1]=65; a[3]=90; I need...
Jon Skeet
people
quotationmark

It sounds like you really just want to use JArray to wrap the array: something["numbers"] = new JArray(a); In other words, let Json.NET take care of the textual representation - you just need to tell it the logical value, which is just... more 3/18/2015 7:20:13 AM

people

update linq to sql values with select new keyword

How to update linq to sql values with select new keyword with anonymous types because I am using var keyword with select new query I need in in this but it returns an error like...
Jon Skeet
people
quotationmark

Well, you're getting the compiler error because - as it says - the properties of anonymous types are read-only in C#. More fundamentally, even if you could modify them, you'd have to expect LINQ to SQL to reverse whatever you put into... more 3/18/2015 7:02:58 AM

people

C# pass element of value type array by reference

I'd like to pass an element of an array (array contains value type elements, not ref type) by reference. Is this possible? Thank you
Jon Skeet
people
quotationmark

Yes, that's absolutely possible, in exactly the same way as you pass any other variable by reference: using System; class Test { static void Main(string[] args) { int[] values = new int[10]; Foo(ref values[0]); ... more 3/17/2015 7:34:34 PM

people

How to initialize var when it is used for different things in a method

I have a method in my controller that is performing some logic based on data submitted and my varitem should be assigned to different results? For example my controller's method...
Jon Skeet
people
quotationmark

Well, two options: Move the declaration to before the if statement, and give it an explicit type: IQueryable<Auction> auctionData; if (...) { ... } else { ... } Change the structure to only have a single declaration, e.g.... more 3/17/2015 7:10:22 PM

people

Alternative to create a new method a String object?

I'm facing with a issue: I have a csv file to prepare and extract values. Here is the code (and works fine): public class Extract { final int[] ID = new int[]{0, 10}; final...
Jon Skeet
people
quotationmark

One option would be to have an enum where you've currently got int arrays: public enum Field { ID(0, 10), NAME(15, 45); private final int start; private final int end; private Field(int start, int end) { ... more 3/17/2015 5:15:33 PM

people

How do I format my results with DecimalFormat if I'm using a separate class?

So I'm very new to Java, and I'm currently trying to create a driver class that will use my Mortgage class to calculate a mortgage. I have everything working except that my output...
Jon Skeet
people
quotationmark

Well you're not actually using df at all. You need to pass the result of calling monthlyPayment() and totalPayment() to the format method. For example: Mortgage mortgage1 = new Mortgage(annualInterestRate, numOfYears,... more 3/17/2015 5:03:58 PM

people

Modify inbuilt Java functions

Is it possible to change how functions inherent to java function without modifying the files? For example, I'd like to modify java.util.Arrays.toString() to throw an error if used...
Jon Skeet
people
quotationmark

No, you can't do that. I suggest you write your own separate class, given that you want different functionality. Obviously Java allows you to override non-final instance methods in classes, but even then the clients would need to know to... more 3/17/2015 1:57:43 PM

people

How to read XML data from the header of a mixed xml/binary file in C#

I have the task to write a reader for a file format with the following specification: First section is plain xml with metadata (utf-8); Last section is a stream of 16bit values...
Jon Skeet
people
quotationmark

While the "read to the closing tag" sounds appealing, you'd need to have a parser which didn't end up buffering all the data. I would read all the data into a byte[], then search for the separator there - then you can split the binary... more 3/17/2015 12:56:29 PM

people