Where is the error in this method?

Asked

Viewed 101 times

0

public DateTime HexToDateTime(String hexDate)
{
    long intDate = long.Parse(hexDate, System.Globalization.NumberStyles.HexNumber);
    DateTime date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(intDate);

    return date;
}

Error : A namespace cannot directly contain members, such as fields or methods.

  • This is the complete code?

  • @Maury Developer only the method with problem. I must put the whole code ?

  • No, it was just a question.

  • About the error: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/compiler-messages/cs0116 Similar question: https://answall.com/questions/373527/cs0116-c-um-namespace-não-pode-conter-diretamentos-fields-ou-métod

  • 1

    by error vc must have placed the method directly after the namespace, a method must be within a class, check the structure of your code

  • 1

    Everything within a project must be contained in a class. Seen, maybe that’s the problem. Involve your methods, properties and variables all in one class. From: Augusto Henrique

  • He really was out of class. Thanks for the time

Show 2 more comments

1 answer

1


Everything within a project must be contained in a class. Seen, maybe that’s the problem. Involve your methods, properties and variables all in one class. From: Augusto Henrique

The following example will cause Visual Studio to signal parts of the code as in violation of CS0116. The attempt to compile this code will result in the compilation failing:

// CS0116.cs
namespace x
{
    // A namespace can be placed within another namespace
    using System;

    // These variables trigger the CS0116 error as they are declared outside of a struct or class
    public int latitude;
    public int longitude;
    Coordinate coord;

    // Autoproperties also fall under the definition of this rule
    public string LocationName { get; set; } 

    // This method as well: if it isn't in a class or a struct, it's violating CS0116
    public void DisplayLatitude()
    {
        Console.WriteLine($"Lat: {latitude}");
    }

    public struct Coordinate 
    {

    }

    public class CoordinatePrinter
    {
        public void DisplayLongitude()
        {
            Console.WriteLine($"Longitude: {longitude}");
        }

        public void DisplayLocation()
        {
            Console.WriteLine($"Location: {LocationName}");
        }
    }
}

Note that in C#, methods and variables must be declared and defined within a class or struct. To learn more about the structure of the program in C#, see the article General structure of a program in C#. To fix this error, rewrite the code so that all methods and fields are contained within a struct or class:

namespace x
{
    // A namespace can be placed within another namespace
    using System;     

    // These variables are now placed within a struct, so CS0116 is no longer violated
    public struct Coordinate
    {
        public int Latitude;
        public int Longitude;
    }

    // The methods and fields are now placed within a class, and the compiler is satisfied
    public class CoordinatePrinter
    {
        Coordinate coord;
        public string LocationName { get; set; }

        public void DisplayLatitude()
        {
            Console.WriteLine($"Lat: {coord.Latitude}");
        }

        public void DisplayLongitude()
        {
            Console.WriteLine($"Longitude: {coord.Longitude}");
        }

        public void DisplayLocation()
        {
            Console.WriteLine($"Location: {LocationName}");
        }
    }
}

About the error: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/compiler-messages/cs0116

Similar question: CS0116 C# A namespace cannot directly contain members, such as fields or methods

Browser other questions tagged

You are not signed in. Login or sign up in order to post.