Browsing 7239 questions and answers with Jon Skeet

How to change the date of a Calendar instance in Java?

I have a class DayOfWeek which takes a date from the user and prints the day of the week on that date. For example the user will enter his date as: (with the format MM DD...
Jon Skeet
people
quotationmark

You're currently passing an int to SimpleDateFormat... you should pass the Date value. You don't need a Calendar at all for that: // BAD CODE: DON'T USE - KEEP READING import java.text.*; import java.util.*; public class DayOfWeek { ... more 7/23/2017 2:50:25 PM

people

Can't make a function call in in spite of the using statement and extension needed

So I wanted to be able to choose my environment when running dotnet (an .net core mvc-project) from the terminal. I found this post and thought the second highest answer was a...
Jon Skeet
people
quotationmark

Although the namespace is Microsoft.Extensions.Configuration, the extension is in the Microsoft.Extensions.Configuration.CommandLine assembly, which is in the Microsoft.Extensions.Configuration.CommandLine package. You need to add a... more 7/23/2017 12:53:25 PM

people

error 1061. T doesn't contain a definition for <propertyName>

i am having around 7 models who have same properties(atributes). On view page i am using a model(name = commonModel) which contains all those properties and a extra property to...
Jon Skeet
people
quotationmark

You just need to constrain the type parameter T to be derived from your base class: // Names changed to follow .NET naming conventions private T RelocateValues<T>(BaseGrammar baseModel, T tempModel) where T : BaseGrammar { ... more 7/23/2017 12:15:51 PM

people

Is there a way to use an object from a list only if it exists?

I have a List of objects, lets say DataTables. Is it possible to use an object from the list only if it exists? For example, if the TableList contains 5 DataTables named...
Jon Skeet
people
quotationmark

Assuming TableList has an element type that is a reference type, you can use FirstOrDefault() to return the first match or null, then the null conditional operator to only call the method if the target is... more 7/23/2017 11:06:35 AM

people

.net async await, what I am doing wrong

Trying to understand use async await to done some work and something just doesn't work. have a code: private async Task<string> getStringAsync() { var tsk = await...
Jon Skeet
people
quotationmark

You're using Result, which blocks until the task completes. You're using that from the UI thread. So the UI thread will be blocked until the task completes. Now, that task comes from getStringAsync, which uses await (something). As the... more 7/21/2017 9:59:47 PM

people

parsing json w variable key value pairs in c#

I've got the following [ { "name": "ATVI", "dailyClosePrice": [ { "3/15/2017": 210.08, "4/6/2017": 235.08, "4/21/2017":...
Jon Skeet
people
quotationmark

You definitely shouldn't be parsing this by hand - Json.NET will be absolutely fine with this, so long as you model it correctly. The JSON represents a list of objects, each of which has a name (string) and a dailyClosePrice which appears... more 7/21/2017 9:46:41 PM

people

Convert string to ASCII without exceptions (like TryParse)

I am implementing a TryParse() method for an ASCII string class. The method takes a string and converts it to a C-style string (i.e. a null-terminated ASCII string). I had been...
Jon Skeet
people
quotationmark

Two options: You could just ignore Encoding entirely, and write the loop yourself: public static bool TryParse(string s, out byte[] result) { result = null; // TODO: It's not clear why you don't want to be able to convert an... more 7/21/2017 11:54:56 AM

people

Why I can access class that is defined outside of namespace?

I was wondering Why I can access class when I defined it outside of namespace scope? I am not very familiar with namespaces, I am just learning, but I thought that namespace...
Jon Skeet
people
quotationmark

Namespaces have nothing to do with access. It's important to differentiate between namespaces and assemblies - they're often closely related, in that types in a namespace Foo.Bar would be likely to be in assembly Foo.Bar.dll, but that's a... more 7/21/2017 7:59:26 AM

people

Can't convert returned string from service to int in c#

I create a service as you can see : public int ValidateAndSubmitReception(NajaResult ReceptionData) { ClientRequest.Headers["Content-type"] = "application/json"; ...
Jon Skeet
people
quotationmark

Firstly, ignore the backslashes in the debugger. They're misleading. The string you've got is: "-1" That's the actual text of the string, as you'd see it if you printed it to the console. While you could just remove the quotes... more 7/21/2017 7:46:02 AM

people

Avoid boilerplate code when initializing collection properties in c#

Is there a way to avoid writing exactly the same type twice when initializing properties (or fields) in c#? (something similar to diamond operator in Java would do..) For...
Jon Skeet
people
quotationmark

No, there's nothing like that in C# at the moment. The obvious potential fix would be to allow fields to be declared using var, but that's more complicated than it may sound. I'd be somewhat surprised to see anything like Java's "diamond... more 7/21/2017 7:28:42 AM

people