Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I do not know how to declare a public variable, for example I would like to declare the search for a file and keep for all my application the same value.
Here is my code below, how to always have the name (DBpath).
Thank you in advance.


What I have tried:

C#
<pre>          // search the disks if the folder exists
                        string[] drives = Directory.GetLogicalDrives().Where(d => Directory.Exists(Path.Combine(d, "Dossier1"))).ToArray();
                        string Résult = string.Join(", ", drives);


                        if (Résult != "")
                        {
    
                            if (Directory.Exists(Résult + @"Dossier1\"))
                            {

                                if (Directory.Exists(Résult + @"Dossier1\Dossier2"))
                                {

                                    if (File.Exists(Résult + @"Dossier1\Dossier2\BDD_SQLite.db3"))
                                    {
  
                                        string DBpath = Résult + @"Dossier1\Dossier2\BDD_SQLite.db3";


                                    }
                                }
                            }
                        }
Posted
Updated 19-May-19 3:42am
Comments
phil.o 19-May-19 4:09am    
In C#, you cannot declare a variable or method at namespace-level. All variables have to be contained in a class or struct.

Use a public static string, or a public static method that returns a constant string. Better still use an application settings entry and a static method that obtains the settings value. See How To: Read Settings at Run Time With C# | Microsoft Docs[^]
 
Share this answer
 
C# does not support global variables directly, but you can use a static class that contains the "global" variables.
See example here: C# How to use Global Variables, Fields and Functions[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900