I'm attempting to construct a database using the C# Console, but I'm getting an error thrown that I can't seem to find the problem with. The problem is in my Query function, when I attempt to define the path based off of user input. Essentially, it looks into a given folder in the user folder, and within it another subfolder containing the files wherein is the database information. Im using the Path.Combine function to dynamically define the path throughout my code, but I'm getting the following error;
ArgumentNullException: Value cannot be null. (Parameter 'path3')
Even though all strings are defined and not null.
this is what the line of code looks like:
path = Path.Combine(Info.path, "infolder", classrooms.inventory, classrooms.geninv, userinput)
And here is all of the code that the strings in the path arguement are referencing:
class classrooms
{
public static string Teacher;
public static string Roomnumber;
public static string Changes;
public static string inventory = "inventory";
public static string geninv = "GenInv";
public static string classinv = "ClassInv";
}
and
class Info
{
public static string path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
if necessary I can attach my full code.
Screenshot of Error
I figured it out.
It was an improperly defined string that the pathcombine function was using in another instance of the code, and it was just getting caught up once i reused the path.combine function. Thanks for all the answers, I wouldn't have thought to look at that without all the input.
Related
I have static class as container for some static strings:
public static class Constants
{
public static string ERROR_MESSAGE_LOGIN= "Unable to login with {0}";
public static string ERROR_MESSAGE_SEND_MAIL= "Unagle to send mail to {0}";
public static string GetFormatedString(string constantName, object [] params)
{
return
string.Format((string)typeof(Constants).GetField(constantName).GetValue(null),params);
}
}
As you can see I want to return formatted string, that based on name provided. And it is seems to work. However, when I writing method call in some other class I want that it give me list of suggestion for first parameter that contains "ERROR_MESSAGE_LOGIN" and "ERROR_MESSAGE_SEND_MAIL".
I know that I can create enum with those name or just use it like
Constants.GetFormatedString(Constants.ERROR_MESSAGE_SEND_MAIL, someparams);
But is there any way to tell method there is list of string parameters that you accept and show it to user in Visual Studio IntelliSense?
Thank you.
Assuming I will need to access the values contained within a given file a small number of times, from different methods, can I include some sort of boolean value within a method to determine whether or not it is appropriate to call the file?
Lets say I have the file config.cfg. In that file, there are three values:
string/name>max|
bool/adult>yes|
int/age>20|
The method getUserName() returns the value of "max". It does this by calling the file:
using (StreamReader reader = new StreamReader(path))
{
//get line beginning with string/name here
return //string value containing name
}
Let's assume I need to use the value of name several times, as well as the values isAdult and clientAge. Rather than accessing the file over and over again, it could be much easier to save the requested value in some form of static variable. However, this variable still needs to be changed in value at least once, when the method is first called.
Can I do this inside the method getUserName()?
Furthermore, is this idea even possible within the bounds of OOP? Is it a similar concept to Prefetch?
It really looks to me that you need to access a field in a lazy way (i.e. only if needed, when needed). If so .NET has Lazy class for such cases which also provides thread safety out of the box:
public static Lazy<string> Name { get; } = new Lazy<string>(() => ReadNameFromFile());
Lazy will also ensure that you only create value once (i.e. call initiailization method) and on later calls it will simply return already retrieved value.
Create a static class. Something like this:
public static class ClientConfig{
public static string Name{get;set;}
public static bool IsAdult{get;set;}
public static int Age{get;set;}
public static void Load(){
// load your values
// ClientConfig.Name = name from file etc.
}
public static void Save(string newName, int age, bool value){
// save your values to the config file
}
}
And call ClientConfig.Load() first time when your app starts, for example (or whenever you need to retrieve config data)
I have a group of session keys which I declare in a Global class file, these get set properly upon login.
public static class SessionKeys
{
public static string memberToken = "token";
public static string memberFirstName = "first_name";
public static string role_pmn = "PMNInstructor";
public static string role_superUser = "";
public static string role_chapterAdmin = "ChapterMember";
public static string role_registeredUser = "RegisteredUser";
}
I have a need to later access some of these session vars in an XSLT Macro for navigation. I'm having trouble finding the correct syntax for accessing these variables in the XSLT Macro.
This syntax works in my .NET Macros:
Session[Globals.SessionKeys.role_pmn]
The correct syntax should be
umbraco.library:Session
This link should provide more information.
Alternatively, you could create an extension library, get the reference in .net, and then just reference your library in the xslt (umbraco.tv video for reference, or text-only instructions)
Syntax for accessing the global session keys in XSLT is different than what is used to access them in C#. You merely have to use the string value you set the key to.
umbraco.library:Session(PMNInstructor)
I have a class called LocalConstants....
public static class LocalConstants {
public static string DM_PATH = ConfigurationManager.AppSettings["DMQueue"];
public static string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"];
}
When trying to access this class in my main program I am getting a null reference exception. Anything from ConfigurationManager.AppSettings[ is always null. But if I write
//The value is returned fine
string bo=ConfigurationManager.AppSettings["MSQueue"];
this compiles fine but is always null and throws a NullRefexception
string moomoo = LocalConstants.PROJECT_PATH;
The exception is
The type initializer for 'TestCodeOutOnSide.LocalConstants' threw an exception.
The innerException is the basic
Object reference not set to an instance of an object.
Even if I change the PROJECT_PATH to
public static readonly string PROJECT_PATH = #"FORMATNAME:DIRECT=OS:serus-nickl\RMQDEV";
I get the same exception
Any ideas?
To begin with, if you are doing this to provide some sort of performance benefit then you should know that these are cached. See ConfigurationManager.AppSettings Caching, to remove any.
Second the issue is most likely that Static field initialization does not work how you expect it to. So your code as written provides no guarantee of that ConfigurationManager.AppSettings has been run. From the linked article sample code:
might produce either the output:
Init A
Init B
1 1
or the output:
Init B
Init A
1 1
[EDIT per OP comment]
There must be something else involved as:
public static class LocalConstants
{
public static string DM_PATH = "DMQueue";
public static string PROJECT_PATH = "MSQueue";
}
class Program
{
static void Main(string[] args)
{
string moomoo = LocalConstants.PROJECT_PATH;
Console.WriteLine(moomoo);
}
}
works for me.
[Edit 2 - Fro those who come after]
It looks like The type initializer for ‘SomeClass’ threw an exception can be a case where
But when it's called by the WPF designer, the "application" is Visual Studio, which (presumably) doesn't have the appropriate connection strings in its .config file;
The fix for that author was:
moving the instantiation of my Entity Data Model into a property
Why don't try something like:
public static string ProjectPath
{
get
{
return ConfigurationManager.AppSettings["MSQueue"];
}
}
I called this
public static string Environment = AppEnvironmentVariable.ToUpper() != "PROD" ? "***FROM " + AppEnvironmentVariable.ToUpper() + "** " : "";
Before this
public static string AppEnvironmentVariable = "DEV";
In the LocalConstants file which broke it because of what Josh said about Static field initialization
You could try making them readonly
public static readonly string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"];
readonly fields can be lazy-loaded
I am writing a class. I have encountered the problem in the title.
Here is the code:
class delivery
{
private string strDeliveryName;
private string strDeliveryAddress;
private string strDeliveryDay;
private string strDeliveryTime;
private string strDeliveryMeal;
private string strDeliveryInstructions;
private string strDeliveryStatus;
}
public delivery(string deliveryName, string deliveryAddress, string deliveryDay, string deliveryTime, string deliveryMeal, string deliveryInstructions, string deliveryStatus)
{
strDeliveryName = deliveryName;
strDeliveryAddress = deliveryAddress;
strDeliveryDay = deliveryDay;
strDeliveryTime = deliveryTime;
strDeliveryMeal = deliveryMeal;
strDeliveryInstructions = deliveryInstructions;
strDeliveryStatus = deliveryStatus;
}
I get the error on the public delivery, any idea why?
Your constructor should be within the brackets of the class definition. On an unrelated note, the convention is to capitalize the first letter of class names.
Your constructor code is not inside the class. Move it inside and all should be fine. :-)
To answer your second question (in the comment), you need to change the name of the constructor to match the name of the class.
This error is because you have declared the function outside of the main class. You should insert your code inside the main class.
I received this error because I accidentally missed an open brace in code above where the error occurred. This meant the class ended prematurely. So if you get this error maybe check that your braces are correct.