Browsing 7239 questions and answers with Jon Skeet

Error when raising custom event

I have a class that will write a log. The class needs to raise an event (under specific circumstances not indicated below), that will be comsumed by a class to react on it. I...
Jon Skeet
people
quotationmark

Two issues: You're raising the event whether or not anyone has subscribed to it. Don't do that - you will get a NullReferenceException if you call WriteLog(this, e) when WriteLog is null. In C# 6 it's easy to avoid... more 12/15/2016 12:55:24 PM

people

Is there a C# equivalent to Delphi's assignable const?

In Delphi you can declare a constant in a method and assign it a value in that same method. The idea is that next time you are in this method, the const will still have the same...
Jon Skeet
people
quotationmark

I don't want a solution that involves private variables in the class, it has to stay inside the method. But the value is part of the state of the object (or type, for a static method) - so it makes sense for it to be a field declared... more 12/15/2016 12:21:15 PM

people

Error converting data type varchar to float. c# webservice

I am creating a web app using c#, Here is my webservice for saving a record [WebMethod] [ScriptMethod(UseHttpGet = true)] public void saverecd(string id, string...
Jon Skeet
people
quotationmark

The problem is with your SQL, which I've reformatted here to avoid a single huge line: SqlCommand cmd = new SQlCommand( @"insert into finalinstructoreexpense (sonvinid,particulars,amount,totalamt,date, ... more 12/15/2016 9:56:33 AM

people

Exception thrown in finally and catch block

have question on exception thrown in catch and finally block: class MyExc1 extends Exception {} class MyExc2 extends Exception {} class MyExc3 extends MyExc2 {} public class C1...
Jon Skeet
people
quotationmark

All you're seeing is a race condition between writing to System.out and System.err. You're explicitly calling System.out.print with 1, then 3, then 4, then 2, and the exception is being thrown and dumped to System.err automatically. So... more 12/15/2016 9:41:42 AM

people

Do nothing when "other side" of ternary operator is reached?

Note: I've seen this question asked sometimes before (a, b, c), but neither of these was in C#, nor helpful. Assume I'm using the ? : ternary operator like this (to do nothing...
Jon Skeet
people
quotationmark

You can't. The whole point of the conditional ?: operator is that it evaluates an expression. You can't even just use: Foo() ? Bar() : Baz(); ... because that isn't a statement. You have to do something with the result... just like when... more 12/14/2016 3:59:31 PM

people

Call base constructor with method as argument

I am a beginner in C# and cannot find out how to call a base constructor from within a subclass: Base class: public class LookupScript { protected Func<IEnumerable>...
Jon Skeet
people
quotationmark

You can't, as myMethod is an instance method, and you can't access anything to do with the instance being created within the constructor initializer. This would work though: public class PresenceLookup : LookupScript { public... more 12/14/2016 8:23:22 AM

people

How to add namespace to xml using linq xml

Question update: im very sorry if my question is not clear here is the code im using right now XDocument doc = XDocument.Parse(framedoc.ToString()); foreach (var node in...
Jon Skeet
people
quotationmark

When you write XNamespace ns = "xsi"; That's creating an XNamespace with a URI of just "xsi". That's not what you want. You want a namespace alias of xsi... with the appropriate URI via an xmlns attribute. So you want: XDocument doc =... more 12/14/2016 7:25:39 AM

people

Scanner throws FileNotFoundException but using bufferedreader and inputstream doesn't?

I am trying to write code for a word guessing game, and it works well when I use bufferedreader and inputstream combined. But when I try it using scanner, it cannot find the file,...
Jon Skeet
people
quotationmark

Scanner is trying to load a file - and you're providing an absolute filename, /res/words.txt. In order to create an InputStream, you're loading a resource, giving it an absolute resource name, even though you've called the variable... more 12/13/2016 3:23:36 PM

people

Get get part of list after certain value

I have got a simple question I am having a list: List<string> test = new List<string> {"one", "two", "three", "four"} Now I want to take for example value "three"...
Jon Skeet
people
quotationmark

It sounds like you're looking for SkipWhile from LINQ: test = test.SkipWhile(x => x != "three").ToList(); That will skip everything until (but not including) the "three" value, then include everything else. It then converts it to a... more 12/13/2016 9:15:11 AM

people

C#, .NET, String class

Can anyone explain me , how it's possible. string str = "test"; str.Concat<> I invoke Concat method, but String hasn't Concat Method with non-static keyword. public...
Jon Skeet
people
quotationmark

You haven't provided full code, but I suspect you're invoking Enumerable.Concat<char>, a generic extension method provided by System.Linq.Enumerable, extending IEnumerable<T>. This is valid because string implements... more 12/10/2016 5:35:29 PM

people