Browsing 7239 questions and answers with Jon Skeet

Tricky Java: boolean assignment in if condition

I'm trying to get prepared for the OCA Java certification and got stuck in some questions dealing assignment in if conditions. Can anyone explain me the reason of the different...
Jon Skeet
people
quotationmark

The difference is in precedence. | has higher precedence ("binds tighter") than =. So this: if(b=true|_false()) is effectively: if (b = (true | _false())) ... Likewise this: if(b=true|(b=false)) is effectively: if (b = (true... more 1/16/2014 11:08:33 PM

people

Replace ' with \' for some reason puts \\'

I have the following code which worked fine up to today: string aName = dr["Name"].ToString(); if (!string.IsNullOrEmpty(aName)) aName = aName.Replace("'", @"\'"); For some...
Jon Skeet
people
quotationmark

Any ideas on how the 2 backslashes are appearing? Yes. You're almost certainly looking at the string in the debugger. The actual string only has a single backslash. Log it, write it out to the console, display it in a form or whatever... more 1/16/2014 11:01:51 PM

people

Array Casting Issue

I have the following code: String []d=new String[]{"y","z"}; Object []ab=d; d=(String[]) ab; System.out.println(d[0]); System.out.println(ab[0]); as expected returns y...
Jon Skeet
people
quotationmark

So why can't I modify the array type by casting? Casting never changes the type of an actual object. It just checks that the reference in question is a reference to an object of an appropriate type, and lets you use that reference... more 1/16/2014 7:24:58 PM

people

How do i create a List of all colors there are from Color?

Tried this now: KnownColor[] colors = Enum.GetValues(typeof(KnownColor)); foreach (KnownColor knowColor in colors) { Color color =...
Jon Skeet
people
quotationmark

p.s.w.g has explained what's wrong with the existing code, but I'd probably just use LINQ to do it all in one go: var colors = Enum.GetValues(typeof(KnownColor)) .Cast<KnownColor>() // Or cast the array ... more 1/16/2014 5:38:11 PM

people

How to protect string from hacking

Can somebody tell me the changes should be made in String class API (we can't do that I know) so that following program should print "Earth" ? OR How can we stop printing "Sun"...
Jon Skeet
people
quotationmark

Just launch the JVM with an appropriate security manager which prevents reflection. You should run code you don't trust under a pretty stringent security manager. You don't need to change the String class - just run in a tighter... more 1/16/2014 5:16:42 PM

people

c# dispose is this correct?

Is this correct way of disposing and using public partial class Form1 : Form { winappContext _context; public Form1() { InitializeComponent(); } ...
Jon Skeet
people
quotationmark

Neither. Assuming you don't need _context to be a field (there's no indication that you do) you should make it a local variable: private void Form1_Load(object sender, EventArgs e) { using (winappContext _context = new... more 1/16/2014 4:08:09 PM

people

Is there a difference between these two methods to convert a double to a negative value?

// Value is double Is any difference? What is fast way for compiler to do this calculation? value = System.Math.Abs(55.55); this.Value = -value; // or this.Value =...
Jon Skeet
people
quotationmark

Is any diference? Absolutely. Consider: double value = -5; this.Value = -value; // Sets Value to 5 this.Value = Math.Abs(value) * -1; // Sets Value to -5 Focus on correctness and simplicity before performance. What are you actually... more 1/16/2014 3:19:48 PM

people

Updating DateTime in access through C#. Data mismatch

I have a date and time loaded into a textbox for editing, but I need to store it as a datetime in my access database not a string and cannot remember or find the syntax to parse...
Jon Skeet
people
quotationmark

You're specifying a string as the deadline date value. You should specify a DateTime instead. You can use DateTime.Parse (or DateTime.ParseExact, or DateTime.TryParseExact) to parse the text representation if you really have to - but it... more 1/16/2014 3:17:24 PM

people

How to use Linq instead of foreach to Group and Count

I have 2 Lists, one for Players and another for Coaches. To count number of Players belong to a Coach, I used: var g = AllePlayer.GroupBy(play=> play.coachNumer); foreach...
Jon Skeet
people
quotationmark

This would be a simpler approach: var query = AllePlayer.GroupBy(player => player.coachNumer, (coach, players => new { Coach = coach, ... more 1/16/2014 2:31:41 PM

people

Variance when creating interfaces

public interface SomeInterfaceName<out T> where T : struct { T? SomePropertyName { get; } } The error I get is: error CS1961: Invalid variance: The type...
Jon Skeet
people
quotationmark

It's pointless to make a type parameter with a struct constraint covariant. Generic variance isn't supported for value type type arguments at all - so for example, there's no conversion from IEnumerable<int> to either... more 1/16/2014 2:09:13 PM

people