0

如果我想在整个程序中使用方法中的字符串,有人可以给我一个例子吗?我希望能够在程序的其他部分使用 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);

        }


    }
}
4

3 回答 3

3

You can just create a public class and access that throughout your application:

public class Application
{
    public string FullName
    {
        get;set;
    }
}

Then call this from your application:

Application.FullName;
于 2013-08-29T11:11:13.700 回答
1

为什么不将FullName其设为静态属性。请注意,你应该用驼峰式写出类和属性的名称

    class FindFile
    {
        private static string fullName;
        public static string FullName
        {
            get 
            {
                return fullName; 
            }
            set 
            { 
                fullName = value; 
            }
        }

或者

public static string FullName {get;set;}

但是即使它没有被初始化,你也可以访问它,在这种情况下,制作你的自定义异常并在 is 时抛出fullNamenull

        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()

于 2013-08-29T11:19:57.757 回答
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);
    }
}
于 2013-08-29T12:14:12.297 回答