“Too many characters in character literal error”

I’m struggling with a piece of code and getting the error: Too many characters in character literal error Using C# and switch statement to iterate through a string buffer and reading tokens, but getting the error in this line: case ‘&&’: case ‘||’: case ‘==’: How can I keep the == and && as a … Read more

How to do constructor chaining in C#

You use standard syntax (using this like a method) to pick the overload, inside the class: then: Note also: you can chain to constructors on the base-type using base(…) you can put extra code into each constructor the default (if you don’t specify anything) is base() For “why?”: code reduction (always a good thing) necessary … Read more

Get int value from enum in C#

Just cast the enum, e.g. The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, or ulong, it should be … Read more

Specified cast is not valid?

From your comment: this line DateTime Date = reader.GetDateTime(0); was throwing the exception The first column is not a valid DateTime. Most likely, you have multiple columns in your table, and you’re retrieving them all by running this query: Replace it with a query that retrieves only the two columns you’re interested in: Then try … Read more

Escape double quotes in a string

No. Either use verbatim string literals as you have, or escape the ” using backslash. The string has not changed in either case – there is a single escaped ” in it. This is just a way to tell C# that the character is part of the string and not a string terminator.

How to declare session variable in C#?

newSession is a poor name for a Session variable. However, you just have to use the indexer as you’ve already done. If you want to improve readability you could use a property instead which can even be static. Then you can access it on the first page from the second page without an instance of it. page 1 … Read more

The calling thread must be STA, because many UI components require this in WPF

Normally, the entry point method for threads for WPF have the [STAThreadAttribute] set for the ThreadMethod, or have the apartment state set to STA when creating the thread using Thread.SetApartmentState(). However, this can only be set before the thread is started. If you cannot apply this attribute to the entry point of the application of thread you are performing … Read more

How to open a new form from another form

In my opinion the main form should be responsible for opening both child form. Here is some pseudo that explains what I would do: You will just need to create a simple event MoreClick in the first child. The main benefit of this approach is that you can replicate it as needed and you can … Read more