C # error1: the name "DateTimeStyles" does not exist in the current context. error2: Type or namespace name 'CultureInfo' not found

Hi, I searched Google best, but could not repair these errors:

The name 'DateTimeStyles' does not exist in the current context

...

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

.. could you help me if I missed something .. Here is my code ..

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Security.Permissions; using System.Threading; namespace ConsoleApplication21 { class Program { static void Main(string[] args) { string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"}; string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", "5/1/2009 6:32:00", "05/01/2009 06:32", "05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}; DateTime dateValue; foreach (string dateString in dateStrings) { if (DateTime.TryParseExact(dateString, formats, new CultureInfo("en-US"), DateTimeStyles.None, out dateValue)) Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue); else Console.WriteLine("Unable to convert '{0}' to a date.", dateString); } } } } 

I am using visual studio 2005.

+4
source share
3 answers

It looks like your use statements are ok. There must be a link missing. Both types mentioned in the error message live in mscorlib.dll .

I believe the C # compiler always refers to mscorlib unless you ask it not to use it using the /nostdlib command line /nostdlib .

How do you compile your program? If you are using Visual Studio, check the project settings for the option β€œDo not use stdlib” or something like that (another answer explains where to find this option). If you use a command line compiler, make sure that you do not pass the /nostdlib .

+3
source

It’s strange. Make sure your project is referencing mscorlib.dll :

Right-click on the project, select "Properties", select "Assembly", then "Advanced ..."; make sure the option "Do not reference mscorlib.dll" is disabled.

+2
source

Hm. Both DataTimeStyles and CultureInfo are in the DataTimeStyles namespace, so

 using System.Globalization; 

Should be enough. What version of .NET are you using?

+1
source

All Articles