Browsing 7239 questions and answers with Jon Skeet

Is it bad practice to embed method parameter expressions?

Is it generally considered bad practice to structure code with embedded expressions in method parameters? Should variables be declared instead? (Android code snippet for an...
Jon Skeet
people
quotationmark

I would almost certainly simplify that, in a number of ways: EditText editText = (EditText) view.findViewById(R.id.fooEditText); String resourceName = someExpression ? R.string.true_expression_text :... more 10/4/2013 2:53:22 PM

people

C# UInt vs Cpp DWORD conflict with negative values

C++: DWORD dwState = (DWORD)-1; Here there is no issues. C#: uint state = -1; Here, uint or UInt32 which is considered to be the equivalent of DWORD cannot have a negative...
Jon Skeet
people
quotationmark

How can I handle it from C# front!? You can use the unchecked operator for the constant assignment: uint state = unchecked((uint) -1); From the C# spec section 7.6.12: The checked and unchecked operators are used to control the... more 10/4/2013 2:42:50 PM

people

Needed string in program is "unused" C#

I've been making a text adventure game in C# (to learn more about strings and such). I have about 3 random scenarios that are triggered after the 10 basic ones where you start....
Jon Skeet
people
quotationmark

The problem here is the "string MainArgs;" line. I need it to call Menu() with "null" to start over. No, you really don't. You don't need takenDump either. You can just change the call to: TextAdventure1.AdventureTime.Menu("null",... more 10/4/2013 2:36:31 PM

people

How does params work in c# (One method allegedly hiding another)

How does c# make params work. When you have code like static string Concat(int arg) { return arg.ToString(); } static string Concat(int arg, params string[] p) { ...
Jon Skeet
people
quotationmark

I think calling it "hiding" is misleading here, to be honest. Both methods are definitely created, but if you call: Concat(5); then for that invocation both methods are applicable (in the terminology of the C# spec section 7.5.3.1) but... more 10/4/2013 9:07:00 AM

people

How to access to global variable whenever its name is same with local variable?

I have a global variable and a local variable with same name. how to access global variable. Code: String s = "Global"; private void mx() { String s = "Local"; ...
Jon Skeet
people
quotationmark

That's not a global variable - it's an instance variable. Just use this: String s = "Local"; lblB.setText(this.s); (See JLS section 15.8.3 for the meaning of this.) For static variables (which are what people normally mean when they... more 10/4/2013 6:24:38 AM

people

Method overloads object and unspecified type

I have the following method with an overload: public string GetName(object obj) { return obj.ToString(); } public string GetName(CustomClass cc) { return cc.Name + " - "...
Jon Skeet
people
quotationmark

Well, you could use dynamic typing. That will basically defer overload resolution until execution time: foreach (dynamic rawDataItem in rawData) { Debug.Print(GetName(rawDataItem)); } Note that there's potentially a performance cost... more 10/4/2013 6:05:09 AM

people

when is this anonymous class object created and accessible, and when is it garbage collected?

The code below is the instantiation of an object which is implemented with an anonymous class. what I am not so clear on is the exact life span of the object created by the...
Jon Skeet
people
quotationmark

what i don't understand is exactly when is this code executed Whenever an instance of MainClass is executed. Note that each instance of MainClass which is created will in turn create a new instance of the anonymous class. Because... more 10/4/2013 5:42:19 AM

people

SQL server express, cannot read or write in C#

I have a code written that automatically adds and reads information from my SQL Server 2012 Express table, Logins. But it wont work, here is the code: private void...
Jon Skeet
people
quotationmark

it skips over [...] Presumably that's because there's no data to read, so myReader.Read() just returns false. it doesnt write the values i told it to. You don't actually tell it to write anything. You create a SqlCommand to... more 10/3/2013 7:32:31 PM

people

System.Net.Webclient not working 'WebClient' could not be found

I am trying to work with WebClient but it is giving me errors so I check in several forums (included this one) and they where telling to put In the top of the file: using...
Jon Skeet
people
quotationmark

Given your tags, it sounds like you may be building a Windows Store app - in which case you need to use HttpClient instead of WebClient. more 10/3/2013 4:58:48 PM

people

Convert Windows 1252 xml file to UTF 8

Is there any approach to convert large XML file(500+MBs) from 'Windows-1252' encoding to 'UTF-8' encoding in java?
Jon Skeet
people
quotationmark

Sure: Open a FileInputStream wrapped in an InputStreamReader with the Windows-1252 for the input Open a FileOutputStream wrapped in an OutputStreamWriter with the UTF-8 encoding for the output Create a buffer char array (e.g.... more 10/3/2013 4:51:00 PM

people