• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

variable problem in c#

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

asusradeon

Member
Joined
Oct 25, 2004
Location
127.0.0.1
hi, im trying to create an integer based on the selected item in a list box. the two ways i have tried don't work.

Code:
string integer;
integer = listBox1.SelectedItem.ToString();
int integer;

Code:
int listBox1.SelectedItem.ToString();

how can i do this ?

thanks
 
I would shy away from using the word 'integer' as a variable name. Read up on reserved words in programming languages. Integer, being a datatype (int), could be used in several languages as a reserved word, or just somewhat confusing in some cases.
 
ShadowPho said:
or

or

Try to see if that works.

don't think that would do what i want. let me explain a little bit better.

for the selected item in a listbox i want an integer named the same as the listbox item to be declared.
 
Well, one thing I notice, just looking right off, is you are trying to create a variable which is already declared. You cannot create String with the name integer, then an int with the name integer. Doesn't work.
 
yeh i no this.... i was tryin to create an integer with the name that the string variable contained
 
It is possible to parse a double into an int, but it varies based on language. What language are you using?
 
asusradeon said:
yeh i no this.... i was tryin to create an integer with the name that the string variable contained

Yea, it won't work like that. The compiler sees it as you trying to create a new integer, with the same variable name. Anyways, as for the whole problem you are facing, as far as I know, it's not even possible. You are basically wanting to create a unique variable at runtime. The program doesn't know what it's going to be called, or anything of the sort.

You would probably have to edit the framework of the code for this to be even somewhat possible. Might want to get cracking :beer:

Seriously though, it would be nice, but I just don't think it's possible. Runtime modifiable code. If you want, feel free to PM me, and we can discuss your code to try and figure out what to do. You always do seem to have interesting questions, what are you working on?
suspect.gif
 
Well.. my dad is an estimator for an electrical company... he has to go through electrical frawings adding up each component, however keeping count can get confusing. I wanted to make an app so you can click each component that is in a list box or button and the total would add 1. Then when finished it would all be saved to a .txt file. My problem is he wil need to add new components to the list. so i thought the program could load the components from a txt file when the app loads and create an int for each one.

Any thoughts on a different way of doing it ?

Thanks for all your answears by the way.. they're helping me progress my c# knowledge (which is next to non existant atm :S lol)
 
How about this:

On load, open the txt file, count the number of items in it, incrementing an integer. Take that final total and create an array of that size. Reread the file, assigning the items to the array as it goes through.

Now you have just dynamically created an array of the needed size.

This is an interesting project, and its for a noble cause. If you want some help with it, I would be more than happy to assist.
 
That could be done with a 2 dimensional array. Essentially, you have an array setup like such:

[item name][item count]

When the user clicks on an item, it can increment its count by 1.
 
Are you using .Net 2.0? If you are then you may want to look at the Dictionary class. This class allows you to create something like an array, but indexed by a custom type, like a string.
Code:
using namespace System.Collections.Generic;
...

Dictionary<string, int> parts = new Dictionary<string, int>();
...

string partName = (string)(listBox1.SelectedItem);
parts[partName] = parts[partName] + 1;

This is dynamically sized so you can easily add a new entry.
Code:
string newPartName = textBox1.Text;
parts[newPartName] = 1;

You can also walk through all the entries of the dictionary.
Code:
foreach( KeyValuePair<string, int> kvp in parts )
{
  MessageBox.Show(string.Format("part[{0}] = {1}", kvp.Key, kvp.Value));
}
 
mccoyn said:
Are you using .Net 2.0? If you are then you may want to look at the Dictionary class. This class allows you to create something like an array, but indexed by a custom type, like a string.
Code:
using namespace System.Collections.Generic;
...
 
Dictionary<string, int> parts = new Dictionary<string, int>();
...
 
string partName = (string)(listBox1.SelectedItem);
parts[partName] = parts[partName] + 1;

This is dynamically sized so you can easily add a new entry.
Code:
string newPartName = textBox1.Text;
parts[newPartName] = 1;

You can also walk through all the entries of the dictionary.
Code:
foreach( KeyValuePair<string, int> kvp in parts )
{
  MessageBox.Show(string.Format("part[{0}] = {1}", kvp.Key, kvp.Value));
}

That looks interesting, thanks for that i'll look into it.
 
im still waiting to try that, my development rig has a knachered hdd in it so ive ordered another.. in the post :(
 
Back