Browsing 7239 questions and answers with Jon Skeet

Base64 Decoding breaks when encoding leads with +

Everytime i encode a string using Base64 and a + is added, the decoding will fail regarding the length of the string is invalid. If the encoding does not have the leading + it'll...
Jon Skeet
people
quotationmark

+ is one of the regular base64 characters, used when the 6 bits being encoded have a value of 62. My guess is that you're putting this in the query parameter of a URL, where + is the escaped value of space. For that use case, you should... more 1/6/2016 3:03:26 PM

people

Do not nest type warning (C#)

I have the following code in a class to setup a serial port even handler. I get two warnings; CA1034 (Do not nest types), which tells me to make my delgate private (which will...
Jon Skeet
people
quotationmark

I don't even see how what I have written is a nested type You've declared your delegate type (PacketReceivedEventHandler) within your class. That's one type nested inside another. Just move the declaration outside your existing class... more 1/6/2016 2:17:35 PM

people

Type inference failure with LINQ select

I have a template TimeSpan in MVC. View @model TimeSpan? @{ var id = "id" + Guid.NewGuid().ToString().Substring(0, 5); string format = (string)(this.ViewData["format"]...
Jon Skeet
people
quotationmark

You're trying to mix query expression syntax with regular method calls, and you've ended up with something that isn't a complete query expression. You could use: listValues = from x in Enumerable.Range(0, 96) let ts = new... more 1/6/2016 1:51:02 PM

people

Getting a Type as an Interface to return from a function

I have a function that returns an interface. I want to avoid a bunch of IF ELSE and get the type via reflection. So I pass in the namespace, typename and assemblyname as strings...
Jon Skeet
people
quotationmark

I suspect you just need to create an instance of the type and cast it to the interface that you're confident it implements: return (ILevel2) Activator.CreateInstance(myType); That will use the parameterless constructor, however - if you... more 1/6/2016 1:49:21 PM

people

C# Null coalesce with LINQ

I have 2 classes which looks like this: class Widget { string Selected { get; set; } List<Option> Options { get; set; } } class Option { string InternalCode {...
Jon Skeet
people
quotationmark

Sure, with a small change: var option = widget.Options .Where(o => o.ExternalCode == widget.Selected) .Select(o => o.InternalCode) .FirstOrDefault() ?? ""; In other words,... more 1/6/2016 10:38:04 AM

people

JodaTime how to parse ISO 8601 time string into DateTime?

i have the string with time in the following format: 2016-01-07T08:00:00+00:00 When i trying to parse string using following method. public static DateTime...
Jon Skeet
people
quotationmark

Your value doesn't have a milliseconds component, so you want ISODateTimeFormat.dateTimeNoMillis(): Returns a formatter that combines a full date and time without millis, separated by a 'T' (yyyy-MM-dd'T'HH:mm:ssZZ). The dateTime()... more 1/6/2016 9:49:50 AM

people

'negative' Duration in Joda, a bug or a feature?

When trying to compare two DateTime I wrote this code private boolean compareTime(DateTime dt1, DateTime dt2) { long d1 = (new Duration(dt1.getMillis() -...
Jon Skeet
people
quotationmark

Sounds entirely sensible to me. You're passing a negative number of milliseconds into the constructor - why would you expect that to become positive? A negative duration is simply a negative amount of time - the time from "now" to "some... more 1/6/2016 9:14:23 AM

people

Error:(415, 32) return (Float)Math.sqrt (x * x + y * y);

I use for determine the space between the first two fingers Android SDK 23 marshmallow private float spacing(WrapMotionEvent event) { // ... float x =...
Jon Skeet
people
quotationmark

Math.sqrt returns a double, even if the input is float. You can't convert double to Float (the wrapper type) other than explicitly going via float (the primitive type)... but that's okay, because you don't actually want Float. The return... more 1/6/2016 7:01:49 AM

people

C# Reading Paths From Text File Says Path Doesn't Exist

I'm developing a small command line utility to remove files from a directory. The user has the option to specify a path at the command line or have the paths being read from a...
Jon Skeet
people
quotationmark

This is the problem: string line = sr.ReadToEnd(); That isn't reading a line of data - it's reading the whole file in one go. Your clearPath method expects its parameter to be a single path, not a bunch of lines glued together. The... more 1/5/2016 8:19:53 PM

people

C# XmlSerializer, edit XML file without rewriting the whole file

I am currently working on a C# app, that has to serialize some simple object into a given XML file. For example, here is a simple example class... Let's say Human public...
Jon Skeet
people
quotationmark

XML simply isn't designed for this sort of operation. Your options are probably: Live with the file being big Use multiple files instead Use some other persistence (non-XML files, or a database) instead Without knowing how big you mean... more 1/5/2016 7:11:14 PM

people