C# 全局使用方法中的字符串
如果我想使用方法中的字符串,有人能给我举个例子吗 在我的节目中?我希望能够在其他部分使用全名值 我的节目 困惑 我想我仍然不明白如何正确地调用它,因为我正在初始化FullNameNotinitializedC# 全局使用方法中的字符串,c#,.net,C#,.net,如果我想使用方法中的字符串,有人能给我举个例子吗 在我的节目中?我希望能够在其他部分使用全名值 我的节目 困惑 我想我仍然不明白如何正确地调用它,因为我正在初始化FullNameNotinitialized public class FindFile { public static string fullName; public static string FullName {
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);
}
}
}
您只需创建一个公共类并在整个应用程序中访问它:
public class Application
{
public string FullName
{
get;set;
}
}
Application.FullName;
然后从应用程序中调用此选项:
public class Application
{
public string FullName
{
get;set;
}
}
Application.FullName;
您只需创建一个公共类并在整个应用程序中访问它:
public class Application
{
public string FullName
{
get;set;
}
}
Application.FullName;
然后从应用程序中调用此选项:
public class Application
{
public string FullName
{
get;set;
}
}
Application.FullName;
为什么不把
FullName
作为一个静态属性呢。请注意,您应该用驼峰大小写类和属性的名称
class FindFile
{
private static string fullName;
public static string FullName
{
get
{
return fullName;
}
set
{
fullName = value;
}
}
或
但是,即使它没有初始化,您也可以访问它。在这种情况下,如果全名
为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()
初始化该值之前访问该值的,为什么不将该FullName
设为静态属性呢。请注意,您应该用驼峰大小写类和属性的名称
class FindFile
{
private static string fullName;
public static string FullName
{
get
{
return fullName;
}
set
{
fullName = value;
}
}
或
但是,即使它没有初始化,您也可以访问它。在这种情况下,如果全名
为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()
初始化该值之前访问该值的。我认为您的混淆来自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);
}
}
我认为您的困惑来自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);
}
}
将fullName设置为静态可能会对您有所帮助。这段代码有什么问题?如果您有多个搜索结果,那么您期望的输出是什么?如果将类、方法和属性名称设置为大写(按传统方式),则更容易发现fullName
属性引用的是自身,而不是支持字段。所以当你调用它时,你会得到一个堆栈溢出。您可以只使用一个自动属性:公共字符串FullName{get;set;}
。将FullName作为静态可能会有所帮助。这段代码有什么问题?如果您有多个搜索结果,那么您希望得到什么样的输出?如果您将类、方法和属性名称设置为大写(传统方式)更容易发现您的fullName
属性引用的是自身,而不是支持字段。所以当你调用它时,你会得到一个堆栈溢出。您只需使用一个自动属性:publicstringfullname{get;set;}
。我试图调用它,但得到的错误是它不能像方法一样使用。我试图调用它,但得到的错误是它不能像方法一样使用。我更新了代码,但现在得到了IOexception,所以我没有正确地调用它。我想?你从来没有设置全名,你正在生成一个本地值,也要更改它,当你多次设置文件名时,你只能有一个全局可用的值,你可能需要一个列表吗?实际上我有一个cab文件,文件名会改变。所以,程序的目的是在设备的根目录中搜索MYAPP,并通过使用部分名称来运行它。因此,我的下一步将调用wceload,使用fullname值启动安装;这将创建本地值。您应该更改并删除字符串声明。但是我不清楚为什么你需要全局可用的文件名你是对的,我真的不需要全局可用的文件名。我可以拥有SourceFinder类中的所有内容。现在似乎可以工作了,非常感谢。我更新了代码,但现在得到了IOexception,所以我想我没有正确调用它?您从未设置全名,您正在创建一个本地值,更改它,并且,在多次设置文件名时,您只能全局使用一个值,你可能需要一个列表吗?实际上我有一个cab文件,文件名会改变。所以,程序的目的是在设备的根目录中搜索MYAPP,并通过使用部分名称来运行它。因此,我的下一步将调用wceload,使用fullname值启动安装;这将创建本地值。您应该更改并删除字符串声明。但是我不清楚为什么你需要全局可用的文件名你是对的,我真的不需要全局可用的文件名。我可以拥有SourceFinder类中的所有内容。现在似乎正在工作,非常感谢。