I am doing a project in Visual Studio, In one of the forms I have a button that every time it is clicked, the program must save an integer and character in two separate variables. If the button is clicked again, the program will do the same but in different variables (i.e. not replacing).
Now my problem is: lets say the user presses this button for 30 times, does that mean that I have to initialize 30 INT variables and 20 CHAR variables ???
Is there another method please?
I'm not sure what you're trying to do. The variable name will be "name" for the string, and you'll get an error when you try to declare the List<string> with the same name "name"
You can use two lists (i.e. List<int> and List<char>) or you can hold both values in an appropriate object, (KeyValuePair<int,char> will also work), though you can just declare the new class:
Code:
class MyPair{
public int intValue;
public char charValue;
}
I'm not sure what you're trying to do. The variable name will be "name" for the string, and you'll get an error when you try to declare the List<string> with the same name "name"
What I am trying to do is to name the list according to whats input in textbox1. So lets assume the user types "list1" in textbox1, hence the name of the list will be listl. Is that possible?
The user shouldn't have access to the list variable name. If you want to store the variables in a named list so that you can access the list by name, then put the list in a dictionary.
Code:
Dictionary<string, List<MyPair>> allMyLists = new Dictionary<string, List<MyPair>>();
allMyLists["NameOfMyFirstList"] = /* some list that you've populated */
You can then access your list by name later on using allMyLists["NameOfMyFirstList"]
Thanks for the help. I finally did manage to do something using the dictionary. The only problem that I am encountering is that I cannot access the directory from other forms. Below is how I did the coding:
In the "public partial class" I included the coding below so i can access the directory from all functions:
Code:
int a = 0;
Dictionary<int, string> dir = new Dictionary<int, string>();
@ misterT , basically I am going to build a robotic arm for my school final year project. Instead of using hardware controls I am going to control the arm using Visual Studio C#. To save the arm positions I am going to use dictionaries. The problem is that I want to access these positions (which are saved in the dictionary) from different forms.
The part where the positions are saved is basically ready, so all I have to do now is to set the dictionaries common for all forms. I have tried different methods but non of them is working.
If you want all forms to have access to the dictionary object, then you have to pass a reference of your dictionary object to each of the forms, or if you will only have a single instance, you can declare the object as public static in some class. e.g.
Code:
public class MyDictionaryHolder{
public static Dictionary<string, List<KeyValuePair<int, char>>> dic = new Dictionary<string, List<KeyValuePair<int, char>>>();
}
Any other code in the project can then access the dictionary, by accessing the static member thus: MyDictionaryHolder.dic