Browsing 7239 questions and answers with Jon Skeet

Why do I need to cast 'this' to interface type in a C# explicit implementation?

I have an interface: public interface Profile { string Name { get; } string Alias { get; set; } } All objects that implement Profile have a Name and an Alias, but some...
Jon Skeet
people
quotationmark

Since this within the context of an explicit interface implementation can only possibly be of type Profile and we know it was accessed through the Profile interface rather than that of the containing class or any other interface it... more 3/16/2016 11:03:24 PM

people

Get only time in milliseconds in android

I want to check if current time is 00:01AM then do a particular job for that I am trying convert only the time to milliseconds when I do that the I get different milliseconds...
Jon Skeet
people
quotationmark

I don't believe there's a calendar field for "millisecond of day", but you could use: long millisecondOfDay = TimeUnit.HOURS.toMillis(cal.get(Calendar.HOUR_OF_DAY)) + TimeUnit.MINUTES.toMillis(cal.get(Calendar.MINUTE)) + ... more 3/16/2016 1:04:43 PM

people

Awaitable method call event when finished

I have synchronous method, that calls async method: private void Sync_Click(object sender, EventArgs e) { CancellationToken token = new CancellationToken(); ...
Jon Skeet
people
quotationmark

The control returns to user before all tasks are done. Yes, that's how async works. Instead of having an async void method, you should make it async Task - and then make your Sync_Click method async as well, so you can await the... more 3/16/2016 12:45:38 PM

people

date string is showing question marks

I am using Eclipse 4.5.1 Mars. I have a very simple program which just use Hindi as locale and print out the date in a format: But when run it, the console print out question...
Jon Skeet
people
quotationmark

It's just the Eclipse console not handling the Indian numbering system. When I run that same code on Linux in a shell, I get: २०१६-०३-१६ As noted by Alexandar, changing the Eclipse console encoding to one which includes all the required... more 3/16/2016 9:04:36 AM

people

Creating a jar with sensitive information which cannot be accessed by a user

In the maven project I am creating there is a certain property file (with sensitive information, credentials) under resource folder which is used by the program. While packaging...
Jon Skeet
people
quotationmark

No. Basically, if your code is running on the user's device, and your code needs the information, then you should expect the user to have access to the information. You can obscure it in various ways - e.g. encrypting it with a key which... more 3/16/2016 6:58:48 AM

people

Cannot Implicitly convert type System.Linq.IQueryable<AnonymousType#1> to System.Linq.IQueryable<AnonymousType#2>

I have a query that works perfectly when I put everything in one statement: var rs = db.SerialNumbers .Join(db.ProductLines, sn => sn.ProductLineId, pl => pl.Id, (sn,...
Jon Skeet
people
quotationmark

Your join projects to this: (sn, pl) => new { pl.Id, pl.Category, sn.UserId } But your final assignment is to this: sn => new { sn.Id, sn.Category } They're not the same type, hence the problem. If the query is actually... more 3/15/2016 6:19:07 PM

people

BinaryWriter not taking substitute args

I'm trying to write binary user data into a .data file and for bw.Write() the substitutes {0},{1},{2},{3} cannot be used because of it being mistakened to be a parameter to the...
Jon Skeet
people
quotationmark

Just call string.Format yourself: bw.Write(string.Format("{0}{\nAuth:{1}\nPass:{2}\nDateCreated:{3}", user, auth, pass, date)); Although if you're using C# 6, you can make this cleaner using interpolated... more 3/15/2016 5:09:15 PM

people

Resharper: Possible null assignment to entity marked with "NotNull" attribute

I apologize if this question is a bit obsessive but I do like my code not to have any wiggly lines underneath it where resharper is telling me off. I have a generic list: var...
Jon Skeet
people
quotationmark

I suspect you can use a R# annotation for this. Something like: [ContractAnnotation("null => true")] public static bool IsEmpty<T>(this IEnumerable<T> source) { return source == null || !source.Any(); } I believe that... more 3/15/2016 9:36:30 AM

people

Converting a method to generic for reuse with Forms

I'm working on a statistical add-in for Excel and the method below is implemented for each plot I have to draw. I have about 10 different plots and each time only the Type of the...
Jon Skeet
people
quotationmark

You just need to constrain T with a class type constraint. You can also add a new() constraint to make the code simpler: private void ShowForm<T>(T form) where T : Form, new() { // Check if the Form already exists. Create it if... more 3/14/2016 10:20:26 AM

people

how to take a specific child node value in xml using C#

I have an xml schema like below <library> <book> <id>1</id> <name>abc</name> <read> ...
Jon Skeet
people
quotationmark

Before you try to extract the num value, you need to fix your Where clause - at the moment you're comparing a string with an integer. The simplest fix - if you know that your XML will always have an id element which has a textual value... more 3/14/2016 7:11:02 AM

people