Browsing 7239 questions and answers with Jon Skeet

c # please explain why this works ,

I do not understand how this program works, If the original sum is set to 0 and then the r(remainder) is added to the sum which is 0. How is that the correct sum of the two...
Jon Skeet
people
quotationmark

Don't forget that it's in a loop. sum is 0 on the first iteration, but on the second iteration it contains the value of the last digit. So imagine you had a number of 195692. It would go through 7 iterations: num sum (before) r ... more 8/28/2015 11:56:35 AM

people

stream reader second line visual studio c#

I'm using this method to save my connection sentence into a text file: MemoryStream ms = new MemoryStream(); byte[] memData = Encoding.UTF8.GetBytes("Data Source=" +...
Jon Skeet
people
quotationmark

Any reason you wouldn't just load the whole file? string[] lines = File.ReadAllLines(@"C:\Users\Çağatay\Desktop\test.txt"); Likewise your first piece of code would be considerably simpler with: File.WriteAllLines( ... more 8/28/2015 11:44:02 AM

people

Getting "java: incompatible types: java.lang.Object cannot be converted to org.testng.ISuiteResult"

I have generated the result reports using reportNg for my automation framework but that does not seems to be handy as it does not shows the testcase description in the results....
Jon Skeet
people
quotationmark

You're using the raw Map type here: Map suiteResults = suite.getResults(); You should specify the type arguments: Map<String, ISuiteResult> suiteResults = suite.getResults(); Or given that you're not using the variable other... more 8/28/2015 10:42:41 AM

people

Java tabs ("\t") not working using FileWritter

As you can see, after every text comes a tab, everything is working fine, but after third tab (see output) it generates a space not a tab. fileWriter = new FileWriter(indexFile,...
Jon Skeet
people
quotationmark

No, it really is generating a tab - it's just that whatever you're using to view the file is deciding to handle tabs by aligning them to some boundary or other. If you make your postal value FB-6666x I suspect you'll then see a much larger... more 8/28/2015 7:47:26 AM

people

Convert string to an array of strings on the fly

I have a method that returns an array of strings. In that method I am processing only one string, and I want to return it as an array of strings (an array of strings with only one...
Jon Skeet
people
quotationmark

As w0lf suggested in comments, the simplest way of creating an array would just be: return new[] { GetMyData() }; While you could create an extension method, I personally wouldn't - and if you do, you absolutely should not call it... more 8/28/2015 7:19:51 AM

people

Error: The type or namespace name 'SqlCe' could not be found (are you missing a using directive or an assembly reference?)

I'm trying to export my data from a dataGridView to an excel file. It works like charm, but I just want to have the name of the columns too. I wrote the following code, but I...
Jon Skeet
people
quotationmark

There is no type called SqlCe as far as I can see... for the DataColumn type, you should just be using System.Windows.Forms.DataGridViewColumn, ideally via a using directive for System.Windows.Forms. Note that DataGridViewColumn doesn't... more 8/28/2015 6:00:42 AM

people

Make a line of code more elegant

I need to convert an unicode to letter and then paste the letter into a textbox. So I have a code like this: textBox1.Text = Convert.ToString(Convert.ToChar(letter_code)); I...
Jon Skeet
people
quotationmark

Assuming letter_code is of type int, I'd probably cast to char and call ToString: textBox1.Text = ((char) letter_code).ToString(); more 8/27/2015 9:59:00 PM

people

LINQ syntax for Order By largest SUM value from child collection

I have following records User --------------- UserID | Name --------------- 1 | Johan 2 | Rick --------------- UserDetail ---------------------------------- UserDetailID | UserID | Percentage ---------------------------------- 1 | 1 | 10 2 | 1 | 30 3 | 2 | 50 4 | 2 | 10 ---------------------------------- Basically I want to SUM percentage per UserID and Order by the largest Total Percentage. In TSQL : SELECT User.UserID FROM User LEFT JOIN ( SELECT SUM(Percentage) [Total], UserID FROM UserDetail GROUP BY UserID ) qry ON User.UserID = qry.UserID ORDER BY qry.Total DESC My current code : var orderedList = user.SelectMany(s => s.UserDetails).Sum(s => s.Percentage); // <-- I have no idea what I'm doing
Jon Skeet
people
quotationmark

It sounds like you don't want to use SelectMany at all - you only want one row per user. I suspect you want: var orderedList = users.OrderByDescending(user => user.UserDetails.Sum(d => d.Percentage)); more 8/27/2015 4:14:25 PM

people

Does the order matter when declaring classes in Java?

Why this code does not even compile? public class T { public static void main(String[] args) { class A extends B {} class B {} B a = new A(); ...
Jon Skeet
people
quotationmark

Yes, it matters for local classes. It's worth noting that local classes are incredibly rare in real code. I can only remember ever using one once. However, for the sake of interest... From the JLS, section 6.3: The scope of a local... more 8/27/2015 4:09:37 PM

people

Interface implementation with different method names

I have this interface: public interface INameScope { void Register(string name, object scopedElement); object Find(string name); void Unregister(string name); } But...
Jon Skeet
people
quotationmark

Not quite, but you can use explicit interface implementation: public class SomeScope : INameScope { void INameScope.Register(string name, object scopedElement) { RegisterName(name, scopedElement); } public void... more 8/27/2015 12:01:46 PM

people