Browsing 7239 questions and answers with Jon Skeet

explain inheritance method override

Here are two classes : public class ClassA { public string result() { return "ClassA_Result"; } } and public class ClassB : ClassA { public string...
Jon Skeet
people
quotationmark

Firstly, you're not overriding the result() method - and indeed you can't, because it's not virtual. The compiler should be giving you a warning like this: warning CS0108: 'ClassB.result()' hides inherited member 'ClassA.result()'.... more 12/29/2014 9:44:26 AM

people

ObjectOutputStream/Input Serializable in JAVA

i am working with ObjectOutputStream to save my object to a .dat file. My problem is, that if i CHANGE source code of my object (for example i add ONE method(getter)) the input...
Jon Skeet
people
quotationmark

It is possible to solve this problem? Yes. The reason the serialization breaks is because the generated version number changes. If you specify this explicitly, it won't be generated, and you'll be in control of versioning. That means... more 12/29/2014 8:37:47 AM

people

How to create global constant variables in C# for the entire application

I have a table that contains system configuration (ie. system_name, system_time_zone.....) I am trying to identify the best approach to read the configuration automaticly and...
Jon Skeet
people
quotationmark

Well, you could create a singleton class which would read the configuration on startup. Personally, however, I would investigate dependency injection. There are lots of DI frameworks in .NET, and that way you'll have code which is easier... more 12/28/2014 9:31:07 PM

people

C# 6 safe navigation not working in VS2015 preview

I have the following property in my code public float X { get { if (parent != null) return parent.X + position.X; return position.X; } set...
Jon Skeet
people
quotationmark

The type of parent?.X is float?, which you're adding to a float - resulting in another float?. That can't be implicitly converted to float. While Yuval's answer should work, I would personally use something like: get { return... more 12/28/2014 4:54:52 PM

people

Exit C# Console application when an exception is thrown

My code reads text from a file. I need to add a method where if the file is not in the correct location, the program will exit. try { TextReader tr = new...
Jon Skeet
people
quotationmark

Three options spring to mind. Firstly, you can just structure your code to return from the Main method at that point. Unless you've got other (non-background) threads running, the application will then terminate. Alternatively, you can... more 12/28/2014 4:00:55 PM

people

Why Java complains "No Valid Constructor" even when default constructor is present?

public class ExternalizableClass implements Externalizable { public static ExternalizableClass CACHE = new ExternalizableClass(-1); int id; public ExternalizableClass() ...
Jon Skeet
people
quotationmark

I suspect the problem is that it's an inner class... so it doesn't actually have a parameterless constructor. Instead, it has two constructors, one of which takes a reference to an instance of the enclosing class, and one of which takes a... more 12/27/2014 8:47:35 PM

people

The type or namespace name 'dbConnection' could not be found (are you missing a using directive or an assembly reference?)

I am new to C# and Visual Studio. I am trying to write a small application where I can Insert/Update/Delete and Select records from MySQL Database. I do have programing...
Jon Skeet
people
quotationmark

There are multiple problems here: You're trying to use a using directive for a class. You can't do that (before C# 6, anyway) - you either specify an alias for a class (which you don't want here) or you just specify a namespace in order... more 12/27/2014 8:42:29 PM

people

Comparing two dates (Formatted Date as a String and Current Time) using before method

I need to use the before method in Java. So I can do date compare code like this: if (storedDate.before(currentMonth)) { } Where currentMonth is set like this: int thisMonth...
Jon Skeet
people
quotationmark

It's not entirely clear what value you want to compare. It's quite easy to see whether "now" is later than the start of the 1st of a particular month: // TODO: For testing purposes, you'd want a Clock abstraction to be injected. Date now... more 12/27/2014 9:00:56 AM

people

data not being written to local SQL Server database file

I have written that gets data from a sensor, I am trying to write this data to a local SQL Server database file. Code doesn't produce any errors but the data is not being written...
Jon Skeet
people
quotationmark

You're calling ExecuteNonQueryAsync to asynchronously insert the record - but you're then closing the connection immediately, while the insert has almost certainly not yet completed. You should either use the synchronous call instead, or... more 12/26/2014 10:00:05 PM

people

BufferedReader Initialization hangs?

I'm trying to read content from the web(more specifically wikipedia) and whenever it's a wiki page that doesn't exist(ie. http://en.wikipedia.org/wiki/Asdfasdfasdfasdf) my...
Jon Skeet
people
quotationmark

I strongly suspect that it's not hanging - it's just throwing an exception without you noticing. I'd expect it to throw a FileNotFoundException, which is what happens when I tried it. It's happening before readLine() because... more 12/26/2014 11:32:41 AM

people