user2190928
user2190928

Reputation: 329

Using string from method globally

Could someone give me a example if I wanted to use a string from a method throughout my program? I would like to be able to use the fullName value in other parts of my program.

Confused..

I guess I still don't understand how to call it correctly because I am getting 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);

        }


    }
}

Upvotes: 0

Views: 158

Answers (3)

Ed Chapel
Ed Chapel

Reputation: 6932

I think your confusion comes from the variable of the same name in the sourceFinder() method. Ensure you are correctly using the field, property, and variable. Further, are you sure you want this to be static?

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);
    }
}

Upvotes: 1

Nanz
Nanz

Reputation: 179

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;

Upvotes: 3

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Why don't you make that FullName a static property. Please note that you should write names of classes and properties with camel case

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

or

public static string FullName {get;set;}

but then you may access it even if it is not initialized, in that case, make your custom Exception and throw it if the fullName is 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) { }

}

So if you are getting those exceptions within your app, you should change the logic since you are accessing that value before it is initialized by calling method sourceFinder()

Upvotes: 1

Related Questions