Browsing 7239 questions and answers with Jon Skeet

Attribute value must be constant error using Contentful Space

Am getting the error message: Attribute value must be constant while defining a Space annotation for my Space class: I've looked at the question In Java why this error:...
Jon Skeet
people
quotationmark

If you look at the Javadoc for Space you'll see that the locales attribute is a String[] - so just provide the IDs of the locals: @Space(value = "id", dbVersion = 1, models = NewsVault.class, locales = { "en" }) more 1/20/2017 8:01:39 AM

people

Google Protocol Buffers serialize to byte array

I'm following the tutorial for using Google Protocol Buffers for C#. I don't see an example for converting an object into a byte array - does anyone know how to do that? I've...
Jon Skeet
people
quotationmark

Assuming you're using the Google.Protobuf nuget package, you can just use: using Google.Protobuf; ... byte[] bytes = fp.ToByteArray(); You need the using directive for Google.Protobuf to make the IMessage.ToByteArray extension method... more 1/19/2017 6:56:30 PM

people

Pass array to variable parameter method

I'm using JCommander in a project. Its documentation defines its parse method to be like parse public void parse(String... args) Parse and validate the command line...
Jon Skeet
people
quotationmark

That code will already work fine. The args parameter of parse is already of type String[] - the String... syntax just means that the compiler is allowed to convert this: parse("foo", "bar", "baz"); into parse(new String[] { "foo",... more 1/19/2017 8:42:35 AM

people

Using a class constructor to create object arrays of 2 different types

I'm implementing a database of sorts for a data structures project and i'm having a really hard time wrapping my head around the logic of what I need to do. I have an abstract...
Jon Skeet
people
quotationmark

Firstly, I'd question the approach - it sounds like this single class is trying to do too many things. But if you really, really want to do it, I'd recommend static factory methods calling a private constructor: public class... more 1/19/2017 7:04:40 AM

people

Call extension method created on parent class overloaded in child class

I am having code as below where WebServiceSender is the parent class. I have added an Extension method to this as "ExtTest". I have one child class "ChildWebServiceSender"...
Jon Skeet
people
quotationmark

How can i call Extension method ExtTest ? Instance methods will always be preferred over extension methods - the compiler only checks for extension methods after everything else has failed. In this case, the simplest approach is just... more 1/17/2017 3:26:51 PM

people

IOException while calculating MD5 and SHA1 checksums

I have a problem while executing a method that computes MD5 and SHA1 checksums for a given file. My methods look like this: SHA1: public string HashFile(string toHash) ...
Jon Skeet
people
quotationmark

You're opening the file without closing it. Just open the stream separately so you can close it in a using statement: MD5 md5 = MD5.Create(); using (var stream = File.Open(...)) { var hash = md5.ComputeHash(stream); //... more 1/17/2017 11:38:59 AM

people

Search For keys Matching values in a dictionary of list in C#

I have a dictionary of lists. var dicAclWithCommonDsEffectivity = new Dictionary<string, List<int>>(); var list1 = new List<int>(){1,2,3}; var list2 = new...
Jon Skeet
people
quotationmark

Is there a way without looping through each item in the list of dictionary value. Something has to loop - dictionaries are only designed to look up by key, and you're not doing that other than for the first check. You can do this... more 1/17/2017 7:16:43 AM

people

Custom Numeric Format Negative section bug

Not sure that it is 100% bug but behavior really strange. My task is properly format fraction. For example if : int numerator = -7; int denominator = 100; Then if next line...
Jon Skeet
people
quotationmark

Your format string is "#/100;-#/100;0". That's a very strange format string. You're saying: If the number is positive, use a format of "#/100". If the number is negative, use a format of "-#/100". If the number is zero, use a format of... more 1/16/2017 9:41:08 PM

people

Compute Week of Year issues with ZonedDateTime and Calendar API

I was trying to compute week of year from a ISO-8601 Date format String input. Initially I tried this with java.time.ZonedDateTime but it gives incorrect result for Input Date -...
Jon Skeet
people
quotationmark

There are four problems with your code to start with: You're using the system default time zone when you use Calendar, which may well change which date the Instant falls on. If you set the calendar to use UTC you'll make it more... more 1/16/2017 9:58:56 AM

people

Why does my async method not wait on "serialPort = await SerialDevice.FromIdAsync()"?

I'm trying to make a class that initializes a serial connection between a raspberry and arduino. The class should also be capable of reading and writing over the established...
Jon Skeet
people
quotationmark

All your async methods return void - that means they start executing, and the calling code has no easy way of waiting until it's actually completed before continuing. As soon as your use an await expression on something that hasn't... more 1/15/2017 6:33:24 PM

people