Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在类WriteToFile中使用类CButtonCreate中的属性newFileName?_C#_Vb.net_Visual Studio 2010 - Fatal编程技术网

C# 如何在类WriteToFile中使用类CButtonCreate中的属性newFileName?

C# 如何在类WriteToFile中使用类CButtonCreate中的属性newFileName?,c#,vb.net,visual-studio-2010,C#,Vb.net,Visual Studio 2010,我有一个没有get&set方法的CButtonCreate类。但是我想在另一个类WriteToFile中使用CButtonCreate类的属性“string newFileName”。如何在WriteToFile类中使用属性newFileName class CButtonCreate { // Create a Folder && SubFolder from the tbProject public void CreateFolder(string MyFol

我有一个没有get&set方法的CButtonCreate类。但是我想在另一个类WriteToFile中使用CButtonCreate类的属性“string newFileName”。如何在WriteToFile类中使用属性newFileName

class CButtonCreate
{
    // Create a Folder && SubFolder from the tbProject
    public void CreateFolder(string MyFolderName, string Mytbs, string MytbRevision, string MyTestSystem)
    {
        // Open the Directory path
        string activeDir = @"Y:\temp";

        //Combine the current active folder with the tbProject
        string newPath = Path.Combine(activeDir, MyFolderName);

        // Create a folder in the active Directory
        Directory.CreateDirectory(newPath);

        // Create a new SubFolder name. This example generates a random string. "tbs"
        string newSubPath = Path.Combine(newPath, Mytbs);

        //Create a new Subfolder under the current active folder
        Directory.CreateDirectory(newSubPath);

        // Create a new file name.
        string newFileName = "Project_" + MyFolderName + "_" + MytbR + ".txt";

        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(newPath, newFileName);

        if (!System.IO.File.Exists(newPath))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
               // Call the Function WriteInFile
            }
        }
    }

class WriteToFile
{
    public void WriteInFile(string MyFolderName, string MytextBox, string MytbR, string MycbTest)
    {
        CButtonCreate myFile = new CButtonCreate();
        StreamWriter sw = new StreamWriter(newFileName);
    }
}

WriteInFile方法需要将
newFileName
值作为该方法的参数,例如:

class WriteToFile
{
    public void WriteInFile(string newFileName, string MyFolderName, string MytextBox, string MytbR, string MycbTest)
    {
        CButtonCreate myFile = new CButtonCreate();
        StreamWriter sw = new StreamWriter(newFileName);
    }
}
class WriteToFile
{
    public WriteToFile(string newFileName)
    {
        _newFileName = newFileName;
    }

    private string _newFileName;

    public void WriteInFile(string MyFolderName, string MytextBox, string MytbR, string MycbTest)
    {
        CButtonCreate myFile = new CButtonCreate();
        StreamWriter sw = new StreamWriter(_newFileName);
    }
}
然后,从
CButtonCreate.CreateFolder
中,您可以这样调用它:

WriteToFile w = new WriteToFile();
w.WriteInFile(newFileName, ...);
WriteToFile w = new WriteToFile(newFileName);
w.WriteInFile(...);
或者,如果愿意,可以将该值传递给WriteToFile构造函数中的参数,例如:

class WriteToFile
{
    public void WriteInFile(string newFileName, string MyFolderName, string MytextBox, string MytbR, string MycbTest)
    {
        CButtonCreate myFile = new CButtonCreate();
        StreamWriter sw = new StreamWriter(newFileName);
    }
}
class WriteToFile
{
    public WriteToFile(string newFileName)
    {
        _newFileName = newFileName;
    }

    private string _newFileName;

    public void WriteInFile(string MyFolderName, string MytextBox, string MytbR, string MycbTest)
    {
        CButtonCreate myFile = new CButtonCreate();
        StreamWriter sw = new StreamWriter(_newFileName);
    }
}
然后,在
CButtonCreate.CreateFolder
中,您可以这样称呼它:

WriteToFile w = new WriteToFile();
w.WriteInFile(newFileName, ...);
WriteToFile w = new WriteToFile(newFileName);
w.WriteInFile(...);
使用属性也有其他方法,通过将
CButtonCreate
对象传递给
WriteToFile
对象,或者通过使用静态属性,但我给出的两个示例似乎是最有可能也是最好的解决方案

此外,我应该提到,您误用了“属性”一词。在.NET语言中,属性是一个非常特殊的东西,它与您所谈论的完全不同。属性提供了一种使用描述代码的元数据“修饰”代码的方法。例如,有一些属性指示调试器是否跟踪到您的方法,或者在调试时跳过它。还有其他属性指示编译器如何正确构建程序集。例如,属性用于指定程序集的版本号

我相信你的意思是“财产”或“田地”。但是,这两个术语都不正确,因为
newFileName
变量当前是
CreateFolder
方法的局部变量。因此,无法从其他类外部访问它,甚至无法从同一类中的其他方法访问它,因为它的作用域是
CreateFolder
方法。它仅在执行该方法时存在。该方法一结束,该字符串就消失了。如果出于某种原因,您希望使整个类都可以访问它,那么您需要将其上移到类级别,并使其成为类的字段:

class CButtonCreate
{
    private string newFileName;

    public void CreateFolder(string MyFolderName, string Mytbs, string MytbRevision, string MyTestSystem)
    {
        ...
    }
}
然后,如果希望其他类可以访问它,可以为它创建公共get/set属性