I have a Class CButtonCreate without get & set Methode. But i wanna use the Attribute “string newFileName” from the Class CButtonCreate in a other Class thats Class WriteToFile. How can use the Attribute newFileName in the Class WriteToFile?
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);
}
}
Your WriteInFile method needs to take the
newFileNamevalue as an argument to the method, such as:Then, from
CButtonCreate.CreateFolderyou would call it like this:Or, if you’d rather, you could pass the value to a parameter in the WriteToFile constructor, such as:
And then, from
CButtonCreate.CreateFolderyou would call it like this:There are other ways to do it as well using a property, by passing the
CButtonCreateobject to theWriteToFileobject, or by using static properties, but those two options I gave examples of would seem to be the most likely and best solutions.Also, I should mention that you are misusing the term “Attribute”. In .NET languages, attributes are a very specific thing which is entirely different than what you are talking about. Attributes provide a way to “decorate” your code with metadata that describes your code. For instance, there are some attributes that instruct the debugger whether or not to trace into your method or just skip over it while debugging. There are other attributes that instruct the compiler how to properly build your assembly. Attributes are used, for instance, to specify the version numbers for your assemblies.
I believe the term you meant to use was either “property” or “field”. However, neither of those terms are correct either, because the
newFileNamevariable is currently a local variable to theCreateFoldermethod. Therefore, there is no way to access it externally from another class, or even from another method within the same class because it’s scope is thatCreateFoldermethod. It only exists while that method is being executed. As soon as that method ends, that string is gone. If, for some reason, you wanted to make it accessible to the whole class, you would need to move it up to the class level and make it a field for the class:Then, if you wanted to make it accessible to other classes, you could create a public get/set property for it.