Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# error1:The name 'DateTimeStyles' does not exist in the current context. error2:The type or namespace name 'CultureInfo' could not be found

Tags:

c#

.net

Hi I have searched Google my best but couldn't recover from 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?)

.. can you please help me out with, if something I have missed .. 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.

like image 484
InfantPro'Aravind' Avatar asked Dec 11 '09 12:12

InfantPro'Aravind'


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

Looks like your using statements are OK. There must be a reference missing. Both types mentioned in the error msg live in mscorlib.dll.

I believe that the C# compiler always references mscorlib, unless you ask it not to, using the /nostdlib command line option.

How do you compile your program? If using Visual Studio, check your project's settings for an option 'do not use stdlib' or something like that (another answer explains where to find the option). If using the command line compiler, make sure you are not passing the /nostdlib option.

like image 178
codeape Avatar answered Sep 22 '22 16:09

codeape


Hmm. Both DataTimeStyles and CultureInfo are in the System.Globalization namespace, so the

using System.Globalization;

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

like image 29
Rik Avatar answered Sep 23 '22 16:09

Rik


Strange. Check that your project references mscorlib.dll:

Right click on the project, choose Properties, choose Build, then click Advanced...; make sure that the option to "Do not reference mscorlib.dll" is set off.

like image 39
Jeremy McGee Avatar answered Sep 23 '22 16:09

Jeremy McGee