artin es
artin es

Reputation: 141

Contructor is inaccessible due to its protection level

my error is:

Error 1 'aCI.CheckTexture.CheckTexture()' is inaccessible due to its protection level

and i use this code to check some files MD5/Hash :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace aCI
{
class CheckTexture
{   
    Thread Search;
    protected CheckTexture()
    {
        Search = new Thread(Scan);
        Search.Start();
    }

    protected void Scan()
    {
        if (GetMD5Hash("1.rar") != "9647997C556C5A37A63EFAFBCA4A40D0"
           || GetMD5Hash("2.rar") != "6626959A9099B4C6F5C755E0D2E57EF8"
           || GetMD5Hash("3.rar") != "4D6611A825F81024E0153E2753B8A27E")
        {
            System.Windows.Forms.MessageBox.Show(
            "Sorry come back and restor your orginal files.",
            "Error",
            System.Windows.Forms.MessageBoxButtons.OK,
            System.Windows.Forms.MessageBoxIcon.Error);
            return;
        }
    }

    #region Hash Calculator

    private static byte[] ConvertStringToByteArray(string data)
    {
        return (new System.Text.UnicodeEncoding()).GetBytes(data);
    }

    private static System.IO.FileStream GetFileStream(string pathName)
    {
        return (new System.IO.FileStream(pathName, System.IO.FileMode.Open,
             System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite));
    }

    public static string GetSHA1Hash(string pathName)
    {
        string strResult = "";
        string strHashData = "";

        byte[] arrbytHashValue;
        System.IO.FileStream oFileStream = null;

        System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher =
              new System.Security.Cryptography.SHA1CryptoServiceProvider();

        try
        {
            oFileStream = GetFileStream(pathName);
            arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
            oFileStream.Close();

            strHashData = System.BitConverter.ToString(arrbytHashValue);
            strHashData = strHashData.Replace("-", "");
            strResult = strHashData;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
              System.Windows.Forms.MessageBoxButtons.OK,
              System.Windows.Forms.MessageBoxIcon.Error,
              System.Windows.Forms.MessageBoxDefaultButton.Button1);
        }

        return (strResult);
    }

    public static string GetMD5Hash(string pathName)
    {
        string strResult = "";
        string strHashData = "";

        byte[] arrbytHashValue;
        System.IO.FileStream oFileStream = null;

        System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher =
              new System.Security.Cryptography.MD5CryptoServiceProvider();

        try
        {
            oFileStream = GetFileStream(pathName);
            arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
            oFileStream.Close();

            strHashData = System.BitConverter.ToString(arrbytHashValue);
            strHashData = strHashData.Replace("-", "");
            strResult = strHashData;
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message, "Error!",
           System.Windows.Forms.MessageBoxButtons.OK,
           System.Windows.Forms.MessageBoxIcon.Error,
           System.Windows.Forms.MessageBoxDefaultButton.Button1);
        }

        return (strResult);
    }
    #endregion
  }

}

Then i try Using class CheckTexture from the above code in here:

 private void BtnMpLanClick(object sender, RoutedEventArgs e)
    {
        if (!File.Exists("chrome.exe"))
        {
            MessageBox.Show("Cannot find chrome.exe");
            return;
        }

        else
        {
            //Process.Start("chrome.exe");
            this.StartTheProcess("chrome.exe", "");

            Thread.Sleep(10);

            try
            {
                // I have error on this line:

                CheckTexture Scan = new CheckTexture();

            }
            catch (Exception)

            { }

        }

    }

but I have that error on this line:

CheckTexture Scan = new CheckTexture();

SO pleas if possible someone tel me what is my mistake. Thank you for help

Upvotes: 0

Views: 4305

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98848

In C#, default access modifier of classes is internal.

So your CheckTexture class is internal. Change it to public like;

public class CheckTexture
{
 ...   
}

From Access Modifiers (C# Programming Guide)

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

But this doesn't enough. Because when you write;

CheckTexture Scan = new CheckTexture();

This calls parameterless constructor of CheckTexture class which its access modifier is protected. Make it public also.

public CheckTexture()
{
    Search = new Thread(Scan);
    Search.Start();
}

Upvotes: 5

razethestray
razethestray

Reputation: 676

The class isn't public. Change it to:

public class CheckTexture
{   
    Thread Search;
    public CheckTexture()
    {
        Search = new Thread(Scan);
        Search.Start();
    } 

Upvotes: 7

AlwaysAProgrammer
AlwaysAProgrammer

Reputation: 2919

by default the class in C# is internal. Mark it public. Based on @Caramiriel comment, the constructor also needs to be marked public

Upvotes: 2

Related Questions