Browsing 7239 questions and answers with Jon Skeet

How to set different forecolor to single label?

lblOperationType.Text = "Text"; Label l1 = new Label(); int len = lblOperationType.Text.Length - 1; string b = Convert.ToString(lblOperationType.Text.ToCharArray()[0]); string a =...
Jon Skeet
people
quotationmark

No, in general (i.e. without custom rendering yourself) a label only has a single foreground colour. Assuming this is Windows Forms, it sounds like you might want a RichTextBox instead - that allows multiple colours, fonts etc within a... more 10/22/2013 6:08:45 AM

people

Assigning an int to a struct object without using unsafe

I have a struct definition in c# as follows public struct test { byte SetCommonPOP; ...
Jon Skeet
people
quotationmark

You could write a custom conversion operator: public struct Test { private readonly byte pop; private readonly byte svp; private readonly byte uhdp; private readonly byte mhdp; // TODO: Properties to return the... more 10/21/2013 4:45:15 PM

people

Wrapping Native Async functions with Task<T>

I'm attempting to write a c# wrapper around a third-party library written in native code for consumption in our apps, which are almost exclusively written in .NET, and I'm trying...
Jon Skeet
people
quotationmark

It sounds like you're looking for TaskCompletionSource<T>. You'd wrap your library by creating a TaskCompletionSource, creating an instance of MyNativeLibrary and registering a callback which set the result of the task completion... more 10/21/2013 1:46:50 PM

people

Can an interface have static variables in C#

It might be a silly question, I appreciate if someone can help me understand it. Can an interface in C# can have static variables? If the interface itself need to be static to...
Jon Skeet
people
quotationmark

No, an interface in C# can't declare fields at all. You can't declare a static interface at all in C#, nor can you declare static members within an interface. As per section 11.2 of the C# specification: An interface declaration may... more 10/21/2013 1:41:09 PM

people

Answer is always 0

import java.io.*; class empl { int p, n, r, i; void accept() { try { BufferedReader br = new BufferedReader(new...
Jon Skeet
people
quotationmark

Your accept method declares new local variables for p, n and r - it doesn't assign values to the instance variables, which will still have a value of 0. Rather than declaring local variables, you should just assign values to the instance... more 10/21/2013 1:34:01 PM

people

Is it possible to check via reflection that type is in using block (Dispose is called)

I need to find automatically all code that not disposed properly. Is it possible to check via reflection that my type N is used inside using statement (Dispose is called)?
Jon Skeet
people
quotationmark

No. The closest you could come is to add a finalizer - possibly conditionally so that it's only included for debug builds - which checks whether or not you've been disposed and logs the problem otherwise. (You'd probably want to keep the... more 10/21/2013 1:01:46 PM

people

Iterate over IDictionary with implicit DictionaryEntry

Consider this code: var variables = System.Environment.GetEnvironmentVariables(); foreach (DictionaryEntry vari in variables) { Console.WriteLine(vari.Key); ...
Jon Skeet
people
quotationmark

Why cannot I type foreach(var vari in variables)? Well you can - but then vari is implicitly of type object. You happen to know that each entry within the iterator is a DictionaryEntry, but the compiler doesn't. As far as it's aware,... more 10/21/2013 8:44:40 AM

people

Why [Enum].Parse has an ignoreCase parameter?

Since an Enumeration in VB.Net cannot contains duplicated values like this: Enum Test A a End Enum Then why the [Enum].Parse method has a StringCase...
Jon Skeet
people
quotationmark

An enum can contain values which differ only in case - you just can't declare them in VB. This is perfectly valid C#: public enum Foo { A, a; } Additionally, even if the enum couldn't contain values differing only in case, that... more 10/21/2013 8:39:49 AM

people

Program exits while calling await function

Hello, I want to consume REST api. I use code like this: namespace test2 { class Program { static void Main(string[] args) { TestRest t = new TestRest(); ...
Jon Skeet
people
quotationmark

There are two problems here: You've got a void async method, which is almost always a bad idea unless it's meant to be an event handler. You should generally make an async method return Task where you'd normally write a void synchronous... more 10/21/2013 8:36:59 AM

people

A better way to provide common data for enum s

Suppose, I want an int[] array to be shared by all instances of enum. Here is an example public enum SampleEnum { Enum1(1), Enum2(2), Enum3(3), Enum4(4); private int[]...
Jon Skeet
people
quotationmark

It there a reason for not allowing a reference to a static class within enum initializer? It's a static field that you're not allowed access to, and there's a very good reason: the field will still have its initial value, because the... more 10/21/2013 6:06:22 AM

people