'Cannot figure out 'The name does not exist in the current context; Error
I want to assign my program a Guid and use it to pull config data from the SQL server. However, I keep getting the error:
The name 'ApplicationID' does not exist in the current context
I've always struggled with variable declaration. I can usually figure it out, but I'm stuck on this one....
Program.cs creates the ApplicationID, then I call LoadConfig to grab and set the rest of the variables based on that ApplicationID. However when I go to use the ApplicationID in LoadConfig it gives me that error.
I confirmed that if I replaced the ApplicationID with the Guid string it pulls everything as expected.
I also tried adding using ABBAS.ProgC to Config, but that didn't help either.
using System;
using System.Collections.Generic;
using ABBAS.sFTP;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using System.Reflection;
namespace ABBAS.ProgC {
class Program {
static private Guid processid;
static private SftpClient sftpc;
static private sFTPFiles sFTPFiles;
static public Guid ApplicationID = new Guid("d71b536f-1428-4797-95e6-3948e736fcca");
static async Task Main() {
ManualResetEvent exitEvent = new(false);
TaskCompletionSource tcs = new();
Console.WriteLine($"Program Starting at {DateTime.Now}");
Console.WriteLine($"Process id: {Environment.ProcessId}");
programversion = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion;
Console.WriteLine($"Version: {programversion}");
Config.LoadConfig();
...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thinktecture.IdentityModel.Client;
using System.Data.SqlClient;
using Newtonsoft.Json.Linq;
using ABBASLibrary.Configuration;
using System.Text.Json;
using System.Data;
namespace ABBAS {
static public partial class Config {
// Environmental Variables
static public int ConfigurationID;
//Global Variables
static public string sFTPServer;
static public string sFTPServerUID;
static public string sFTPServerPWD;
static public int sFTPServerPort;
static public int sFTPRetryMax;
static public int sFTPConnectDelayMin;
static public int sFTPConnectDelayMax;
static public void LoadConfig() {
using (SqlConnection _sqlconn = new SqlConnection())
{
_sqlconn.ConnectionString = sqlconnstring;
_sqlconn.Open();
SqlDataAdapter da;
DataSet ds = new DataSet();
string varName;
string varValue;
int newNum;
bool isParsable;
try
{
String query = "SELECT * FROM [ConfigurationDetails] WHERE [ApplicationID] = " + ApplicationID;
da = new SqlDataAdapter(query, _sqlconn);
da.Fill(ds);
for (int cnt = 0; cnt < ds.Tables[0].Rows.Count; cnt++)
{
varName = "";
varValue = "";
varName = ds.Tables[0].Rows[cnt][4].ToString(); // VariableName
varValue = ds.Tables[0].Rows[cnt][5].ToString(); // VariableValue
if (varName.ToString() == "sFTPServer")
sFTPServer = varValue;
if (varName.ToString() == "sFTPServerUID")
sFTPServerUID = varValue;
if (varName.ToString() == "sFTPServerPWD")
sFTPServerPWD = varValue;
if (varName.ToString() == "P21CompanyID")
P21CompanyID = varValue;
if (varName.ToString() == "sFTPServerPort")
{
isParsable = Int32.TryParse(varValue, out newNum);
if (isParsable)
sFTPServerPort = newNum;
}
if (varName.ToString() == "sFTPRetryMax")
{
isParsable = Int32.TryParse(varValue, out newNum);
if (isParsable)
sFTPRetryMax = newNum;
}
if (varName.ToString() == "sFTPConnectDelayMin")
{
isParsable = Int32.TryParse(varValue, out newNum);
if (isParsable)
sFTPConnectDelayMin = newNum;
}
if (varName.ToString() == "sFTPConnectDelayMax")
{
isParsable = Int32.TryParse(varValue, out newNum);
if (isParsable)
sFTPConnectDelayMax = newNum;
}
}
}
catch (Exception ex)
{
_sqlconn.Close();
}
finally
{
if (_sqlconn != null)
_sqlconn.Dispose();
if (ds != null)
ds.Dispose();
}
}
}
}
}
The original idea (not mine!) was to create the variable in Config, and they set it in Program.cs, but since we are calling LoadConfig and need the variable set to get the rest of our variables that clearly won't work.
Solution 1:[1]
Because it doesn't exist in that context. It's declared as a static member of the Program class:
class Program {
//...
static public Guid ApplicationID = new Guid("d71b536f-1428-4797-95e6-3948e736fcca");
To access it you'd need to reference it statically from that class:
String query = "SELECT * FROM [ConfigurationDetails] WHERE [ApplicationID] = " + Program.ApplicationID;
Note that there are a few anti-patterns in your code. Including, but not limited to:
- Making everything
staticis going to drastically increase complexity and the potential for bugs. (You've already encountered a problem with it here in this question.) This is generally the approach when developers want "global variables" for everything. It sounds nice at first, because that way you always have access to everything and can use what you want when you want. But it quickly becomes problematic because you always have access to everything and can't effectively isolate any of the code into meaningful components. - Silently ignoring exceptions is going to make it very difficult to identify, diagnose, and correct problems. At the very least there should be some logging or output of exception information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
