What is "CS0642 Possible mistaken empty statement"

BenTam 1,421 Reputation points
2024-02-11T13:57:16.9933333+00:00

Dear All,

using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace NewTims
{ 
    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
            string key, string def, StringBuilder retVal,
            int size, string filePath);

        public IniFile(string INIPath)
        {
            path = INIPath;
        }

        public void IniWriteValue(string Section, string Key, string Value)
        {
            if (!File.Exists(Directory.GetCurrentDirectory()+this.path));
            {
                File.Create(this.path);
            }
            WritePrivateProfileString(Section, Key, Value, this.path);
        }


        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
    }
}

I want to write a utility that can be called by the syntax as follows.

IniFile("NewTims");
IniWriteValue("History", "Prior Color");

I encounter an issue as follows. The "path" property should be on the type static.

  1. However, changing the word from "public" to "static", all the "this.path" are red-lined. Are properties can't be static? Could anybody help me modify the code?
  2. The line "if (!File.Exists(Directory.GetCurrentDirectoy()+this.path))" is marked "CS0642 Possible mistaken empty statement". Could anybody explain why?
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,938 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 24,201 Reputation points Microsoft Vendor
    2024-02-12T06:23:43.48+00:00

    Hi @BenTam ,

    The issue is with the semicolon at the end of the if statement, this semicolon terminates the if statement, making the subsequent block always execute, regardless of the condition. As a result, File.Create(this.path); will be executed every time, even if the file already exists.

    Check Compiler Warning (level 3) CS0642.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 52,486 Reputation points
    2024-02-11T17:31:20.3033333+00:00

    The “this” is used to access instance properties of the current instance from a class instance method. Static properties belong to a class not a class instance, so it <classname>.prop

    if if not ambiguous, when referenced from a class method, the prefix is optional.

    1 person found this answer helpful.