Browsing 7239 questions and answers with Jon Skeet

Why does Code Analysis in VS2013 RC lead me into a box canyon?

I ran Code Analysis on a utility I'm maintaining, and it advised me to change this: private static extern int ReadMenu1File(string Menu1Path); ...to this: private static...
Jon Skeet
people
quotationmark

I suspect you've misinterpreted the advice. I suspect it was actually suggesting: private static extern int ReadMenu1File([MarshalAs(UnmanagedType.LPWStr)] string Menu1Path); EDIT: This fits in... more 9/24/2013 5:03:18 PM

people

Appending a byte to String in java using escape sequence

I wish to append a byte i.e. 16 = 0x10 to a String, using escape sequence to do it in single line of code: String appendedString = new String('\16'+"String"); This results in a...
Jon Skeet
people
quotationmark

\10 is in octal which is why you're getting U+0008. I don't believe there are any escape formats which use decimal; I'd suggest using the \uxxxx format, or specific escape sequences for supported characters (\r, \n etc). So for the second... more 9/24/2013 4:30:04 PM

people

Initialize windows form object dynamically

How can I dynamically initialize a win form. In my application I am having numerous forms, like more than 50 and the below code is repeated as many times.. so I want to create...
Jon Skeet
people
quotationmark

If you know the Type to use, you can use Activator.CreateInstance: private void ShowForm(Form form, Type type) { if (form == null || form.IsDisposed) { form = (Form) Activator.CreateInstance(type); form.MdiParent =... more 9/24/2013 4:01:44 PM

people

How to store java.util.sql to XMLGregorian Calendar

I am trying to change the format of XMLGregorianCalendar date. The code in my schema file(.xsd) is this: <xs:element name="LatestSaleDate"> <xs:annotation> ...
Jon Skeet
people
quotationmark

Can anybody here help me how I can solve it and also format it so it only write to xml file in this format dd/MM/yyyy You can't and shouldn't - at least not without changing the schema. Your schema expressly specifies that it's an... more 9/24/2013 3:51:25 PM

people

Assigning a Readonly Field via Virtual Member in Abstract Class's Constructor

I know there are similar questions already asked here on SO, but specifically my question deals with scenarios involving setting a readonly field by calling a virtual member in an...
Jon Skeet
people
quotationmark

It's definitely something to be avoided if possible - calling virtual methods within a constructor is always a bit smelly, as you'll be executing code before the subclass gets to perform initialization - its constructor body won't have... more 9/24/2013 3:21:38 PM

people

C# Trying to create a singleton class

I've been reading about how to implement a thread safe singleton class. I've managed to find many different ways of creating one, however I have not managed to find information...
Jon Skeet
people
quotationmark

The methods would go in the outer class. The only purpose of the nested class is to enforce particular timing of the initialization of the singleton instance of the outer class. Don't forget that your GetInstance method returns a... more 9/24/2013 2:46:24 PM

people

Is it necessary to use a variable or exactly the property in IDisposable?

When I'm implemeting IDisposable interface, I've work with the bool disposed variable, which is in protected virtual void Dispose(bool disposing). But I wonder, what if I use not...
Jon Skeet
people
quotationmark

This property: bool disposed { get; set; } is almost equivalent to a field. It's effectively this: bool _disposed; bool disposed { get { return _disposed; } set { _disposed = value; } } Given that both the field and the property are... more 9/24/2013 2:34:58 PM

people

Need a LINQ code example to link two tables that have no foreign key

I need a code example please for the following scenario. Suppose I have 2 tables, "Order" and "Supplier", and "Order" has a field called "SupplierId", and each Supplier has a...
Jon Skeet
people
quotationmark

You should be able to do the join explicitly: from order in db.Orders join supplier in db.Suppliers on order.SupplierId equals supplier.Id where supplier.City == "London" select order; (You could filter on "only suppliers within London"... more 9/24/2013 1:45:41 PM

people

C# limit the hour with minute

i want to enable the checkbox only between 12:30 -- 14:00 and 18:00 -- 21:00. if (((DateTime.Now.Hour >= 12 || DateTime.Now.Minute >= 30) && DateTime.Now.Hour <...
Jon Skeet
people
quotationmark

I suggest you do it by actual times of day (represented as TimeSpan values) instead: var firstPeriodStart = new TimeSpan(12, 30, 0); var firstPeriodEnd = new TimeSpan(14, 0, 0); var secondPeriodStart = new TimeSpan(18, 0, 0); var... more 9/24/2013 7:36:49 AM

people

Why HashMap's get() returns null when it shouldn't?

I wrote a method to check if a string has only unique characters. I send it the obvious non-unique-characters string "11" and it returns true instead of false. It happens because...
Jon Skeet
people
quotationmark

You're fetching by character, but your map's key is Boolean. You want the key to be Character and the value to be Boolean: HashMap<Character, Boolean> tab = new HashMap<Character, Boolean>(); Character c; for (int i = 0; i... more 9/23/2013 8:15:40 PM

people