Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple C# Question... :)
New Posts  All Forums:Forum Nav:

Simple C# Question... :)

post #1 of 4
Thread Starter 
private void button1_Click(object sender, EventArgs e)
{
List< string > Names = new List < string > ();
Names.Add("James");
MessageBox.Show(Names[0]);
}

How can i use the Names in other forms or buttons?
I have tried

private void button2_Click(object sender, EventArgs e)
{

MessageBox.Show(Names[0]);

}

But i does not work frown.gif
My Computer ...
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom ii x4 955 @4ghz M4A88T-M/USB3 ASUS HD6870 DirectCU 4GB ValueRam 
Hard DriveHard DriveOSMonitor
External 500GB for storage Crucial M4 128GB Windows 7  32* LCD Screen 
KeyboardPowerCase
Logitech G110 CX600 Cooler Master 
  hide details  
Reply
My Computer ...
(11 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom ii x4 955 @4ghz M4A88T-M/USB3 ASUS HD6870 DirectCU 4GB ValueRam 
Hard DriveHard DriveOSMonitor
External 500GB for storage Crucial M4 128GB Windows 7  32* LCD Screen 
KeyboardPowerCase
Logitech G110 CX600 Cooler Master 
  hide details  
Reply
post #2 of 4
I am new to programming so what I say is just based on my little experience.

I think you need to add your names to the list before your button call or to have that name be sent to a function outside the button call that will store them for other buttons to pull form. The names list will be individual for each button. So, for example, you have a class that lists the names and then the buttons inside the class to call. Not sure if this makes any sense.
Windfall
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 Asus P5E3 Premium Wifi XFX HD Radeon 6950 SuperTalent 
Hard DriveOptical DriveOSMonitor
WADFALS1001 WD Black Edition 1TB x 2 Raid 0 DVD-RW DIE VISTA, Using W7 24Inch 1920x1200 
PowerCase
Corsair 750TX Corsair 800D 
  hide details  
Reply
Windfall
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q9550 Asus P5E3 Premium Wifi XFX HD Radeon 6950 SuperTalent 
Hard DriveOptical DriveOSMonitor
WADFALS1001 WD Black Edition 1TB x 2 Raid 0 DVD-RW DIE VISTA, Using W7 24Inch 1920x1200 
PowerCase
Corsair 750TX Corsair 800D 
  hide details  
Reply
post #3 of 4

Here, "Names" is defined within the first method and, therefore, is local to it. It cannot be accessed from the second method.

 

To allow it to be accessed outside of just the first method, you'll have to declare and define "Names" outside of the first method.

 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
 
Desktop
(13 items)
 
MacBook Pro 13"
(6 items)
 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
CPUGraphicsRAMHard Drive
Intel i5 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Lion 13.3" @ 1280 x 800 
  hide details  
Reply
post #4 of 4
Names is defined as a local variable in the button1 function. Basically what Unregistered said.

Just bring the declaration of the Names list outside of the button1 function, but still within the main loop. That makes it a global variable so your button2 function can access it.
BlackDog
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q9300 MSI P7N-SLI EVGA GTS250 512mb 4GB OCZ SLI 1066mhz 
Hard DriveOSPowerCase
1 TB WD SATA Windows 7 Corsair 750HX Thermaltake M9 
  hide details  
Reply
BlackDog
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core 2 Quad Q9300 MSI P7N-SLI EVGA GTS250 512mb 4GB OCZ SLI 1066mhz 
Hard DriveOSPowerCase
1 TB WD SATA Windows 7 Corsair 750HX Thermaltake M9 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Simple C# Question... :)