如果我想使用方法中的字符串,有人可以给我一个例子 我的整个计划?我希望能够在其他部分使用fullName值 我的节目。
困惑..
我想我仍然不明白如何正确调用它,因为我收到的是FullNameNotinitialized。
public class FindFile
{
public static string fullName;
public static string FullName
{
get
{
if (fullName == null)
throw new FullNameNotInitialized();
return fullName;
}
set
{
fullName = value;
}
}
public class FullNameNotInitialized : Exception
{
public FullNameNotInitialized()
: base() { }
public FullNameNotInitialized(string message)
: base(message) { }
public FullNameNotInitialized(string format, params object[] args)
: base(string.Format(format, args)) { }
public FullNameNotInitialized(string message, Exception innerException)
: base(message, innerException) { }
public FullNameNotInitialized(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
}
public void sourceFinder()
{
string partialName = "APP";
DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"/");
FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");
foreach (FileInfo foundFile in filesInDir)
{
string fullName = foundFile.FullName;
System.Diagnostics.Debug.WriteLine(fullName);
}
MessageBox.Show(fullName);
}
public void show()
{
MessageBox.Show(fullName);
}
}
}
答案 0 :(得分:3)
您可以创建一个公共类并在整个应用程序中访问它:
public class Application
{
public string FullName
{
get;set;
}
}
然后从您的应用程序中调用它:
Application.FullName;
答案 1 :(得分:1)
为什么不将FullName
作为静态属性。请注意,您应该使用camel case
class FindFile
{
private static string fullName;
public static string FullName
{
get
{
return fullName;
}
set
{
fullName = value;
}
}
或
public static string FullName {get;set;}
但是,即使它未初始化,您也可以访问它,在这种情况下,如果fullName
为null
,请设置自定义异常并抛出它。
class FindFile
{
private static string fullName;
public static string FullName
{
get
{
if (fullName == null)
throw new FullNameNotInitialized();
return fullName;
}
set
{
fullName = value;
}
}
public class FullNameNotInitialized : Exception
{
public FullNameNotInitialized()
: base() { }
public FullNameNotInitialized(string message)
: base(message) { }
public FullNameNotInitialized(string format, params object[] args)
: base(string.Format(format, args)) { }
public FullNameNotInitialized(string message, Exception innerException)
: base(message, innerException) { }
public FullNameNotInitialized(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
protected FullNameNotInitialized(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
因此,如果您在应用程序中获得这些异常,则应更改逻辑,因为在通过调用方法sourceFinder()
答案 2 :(得分:1)
我认为您的混淆来自sourceFinder()
方法中同名变量。确保您正确使用字段,属性和变量。此外,你确定你想要这是静态的吗?
public class FindFile
{
public static string _fullName;
public static string FullName
{
get
{
if (_fullName == null)
throw new FullNameNotInitialized();
return _fullName;
}
set
{
_fullName = value;
}
}
public void sourceFinder()
{
string partialName = "APP";
DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"/");
FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");
foreach (FileInfo foundFile in filesInDir)
{
// Do not use a variable here, use the field
_fullName = foundFile.FullName;
System.Diagnostics.Debug.WriteLine(fullName);
}
// Use the property...
MessageBox.Show(FullName);
// ... or the field
MessageBox.Show(_fullName);
}
}