Browsing 7239 questions and answers with Jon Skeet

Iterate over values in Flags Enum but keep the order

I have the following enum in my code: [Flags] public enum Column { FirstName = 1 << 0, LastName = 1 << 1, Address = 1 << 2, } Now I have a method...
Jon Skeet
people
quotationmark

I now need to iterate through the values but keep the order in which the enum was passed to the methods. There's no such concept. The | operator doesn't have any way of preserving ordering. To put it another way, imagine we... more 10/31/2014 2:07:54 PM

people

How to access arraylist from an other void then it is generated in in Java?

Is it possible to access an arraylist from another void then it is generated in in Java? (I'm making an android app, i did cut down the code massively) package...
Jon Skeet
people
quotationmark

You've currently declared questionid as a local variable in the onCreate method. That variable only exists for the duration of that method call. You want an instance variable - i.e. something which is part of the state of the object, and... more 10/31/2014 11:11:24 AM

people

Get the reference of a specific class object in memory

Is there a way to retrieve a reference to the instance of a specific class? I know the name of the class and I know that there is only one instance of it in memory. I need to do...
Jon Skeet
people
quotationmark

No, there's no way of doing that. You could have a static variable in the class, to keep a reference to the "most recently created instance" available - this will prevent garbage collection, of course. Or you could make the class a... more 10/31/2014 10:19:07 AM

people

hashCode(): Objects.hash() And Base Class?

Let's say I have two classes class A { private String aStr; @Override public int hashCode() { return Objects.hash(aStr); } } and class B extends A { private...
Jon Skeet
people
quotationmark

I'd probably use Objects.hash(bStr, super.hashCode()) You definitely want to combine the state you know about with the superclass implementation of hashCode(), so any solution would want to use bStr and super.hashCode()... your original... more 10/31/2014 9:05:05 AM

people

What is the difference between MinusEqual ( =) and PlusEqual (+=) as used in event declaration c#

Please I want to know the difference between these two. I often use this += for events like this.btnExport.Click += new System.EventHandler(this.btnExport_Click); From time to...
Jon Skeet
people
quotationmark

Simply put, += subscribe a handler to the event, and -= unsubscribes a handler from the event. (If the specified handler isn't an existing subscriber, the attempt is ignored.) Note that you can use significantly simpler syntax as of C#... more 10/31/2014 8:53:26 AM

people

Class With The Same Name Already Exists

I've created a class named "Group" in my .NET C# web-application. When I try to access this class within other classes I get into no troubles, however, in certain points within...
Jon Skeet
people
quotationmark

How can I specify directly that I am trying to access a different class You could: Use the fully-qualified name, e.g. MyNamespace.Group x = new MyNamespace.Group(); Avoid adding a using directive for System.Text.RegularExpressions... more 10/31/2014 7:54:15 AM

people

Insert quote in mysql using c#

Can anyone help me to insert single quote in mysql using c#. I know how to achieve that, but i don't know the syntax, so far this is my code if (txtcode.Text.Contains("'") ==...
Jon Skeet
people
quotationmark

You don't need to perform any string replacements - just use parameterized SQL and it should be fine. So you mustn't do this: // BAD CODE - DO NOT USE string sql = "INSERT INTO TABLE Person (ID, NAME) VALUES ('" + id + "', '" +... more 10/31/2014 7:28:26 AM

people

How to stop Java from truncating 32bit int to single byte over java.net.Socket ? Or maybe use something else?

So the code is really as simple as it gets int len = 0x00000097; Socket sock = new Socket(host, port); DataOutputStream out = new...
Jon Skeet
people
quotationmark

You're calling DataOutput.write(int) on the DataOutputStream, which is documented as: Writes to the output stream the eight low-order bits of the argument b. The 24 high-order bits of b are ignored. I suspect you really want... more 10/31/2014 7:24:48 AM

people

Why can't I access a constant from an object?

public class Foo { public const int type = 1; } Why can't i do this? Is there a reason behind it or am I trying to access the constant in a wrong way? new Foo().type; I...
Jon Skeet
people
quotationmark

Yes, you're trying to access it in the wrong way. A constant isn't associated with an instance of a type - it's associated with the type itself. So you want: int x = Foo.type; Basically, const members are implicitly static, and C#... more 10/30/2014 11:21:41 PM

people

String.contains always appears false

public final void nameErrorLoop () { while (error) { System.out.println("Enter the employee's name."); setName(kb.next()); if...
Jon Skeet
people
quotationmark

String.contains doesn't use regular expressions - it just checks whether one string contains another, in the way that "foobar" contains "oob". It sounds like you want to check that name only contains letters, in which case you should be... more 10/30/2014 10:59:01 PM

people