Variables in Visual Studio c#

Status
Not open for further replies.

adrianvon

Member
Hi,

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?
 
If you want to keep a list of values, then use a List<> object (e.g. a List<int> to store int values)
 
Thanks for the reply. Will definitely reduce some coding this way . Can I name the list according to the characters input in a textbox?

I tried it this way but it is not working:

Code:
string name = textBox1.Text;

            List<string> name = new List<string>();
 
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"
Also, if in a list I have both Characters and Integers, how can I declare that in the list please?
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;
}

Your list is then declared using
Code:
List<MyPair> myList = new List<MyPair>();
 
Code:
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?
 
You shouldn't need to do that. Do you know why you are trying to do that?
 
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>();

Code:
a++;
 dir.Add(a, textBox1.Text);
 label12.Text = (dir[a]);

This way it worked but as I sad, I cannot access it from a separate form. Any idea of how I can solve this please?
 
What is this partial class that you are extending? Could you tell more about the program structure.
 
@ 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.

Thanks in advance and sorry for my poor English.
 
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
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…