New Posts  All Forums:Forum Nav:

C# variable passing help

post #1 of 6
Thread Starter 
So to cut a long story short I need to instantiate a new form and pass it a variable when a button is clicked, but I can't see a way of doing this and I've been trying to work out a way for ages. The bold variable near the top is the one I want to pass to a new form and at the bottom that's where it needs to go. (apologies for noob terminology tongue.gif )

public partial class Games : Form
{

public Games(string playerName, string word)
{

InitializeComponent();
label1.Text = "Hello " + playerName;
label2.Text = "Word: " + word; // Remove in final program
}

private void button2_Click(object sender, EventArgs e)
{
Guess_the_Word guessForm = new Guess_the_Word();
guessForm.Show();
}

private void button1_Click(object sender, EventArgs e)
{
ConundrumC CC = new ConundrumC();
}
}

This form itself is instantiated by another class (which is instantiated by the first form) and that passes the playerName and word variables to this form.

Any help is appreciated, this has been bugging me for too long rolleyes.gif
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
post #2 of 6
Please use code tags.

But anyway. You should be storing that variable as a private field in the class.
Code:
public partial class Games : Form
{
        private readonly string _word;

        public Games(string playerName, string word)
        {
                InitializeComponent();
                label1.Text = "Hello " + playerName;
                label2.Text = "Word: " + word; // Remove in final program
                this._word = word;
        }

        private void button1_Click(object sender, EventArgs e)
        {
                ConundrumC CC = new ConundrumC( this._word );
        }
}
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #3 of 6
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

Please use code tags.

But anyway. You should be storing that variable as a private field in the class.
Code:
public partial class Games : Form
{
        private readonly string _word;

        public Games(string playerName, string word)
        {
                InitializeComponent();
                label1.Text = "Hello " + playerName;
                label2.Text = "Word: " + word; // Remove in final program
                this._word = word;
        }

        private void button1_Click(object sender, EventArgs e)
        {
                ConundrumC CC = new ConundrumC( this._word );
        }
}

I was going to use code tags, but then I couldn't make stuff bold tongue.gif I'll try that now and report back smile.gif
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
post #4 of 6
Quote:
Originally Posted by Scott1541 View Post

I was going to use code tags, but then I couldn't make stuff bold tongue.gif I'll try that now and report back smile.gif

No need for the bold tags, I'd have figured it out smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #5 of 6
Thread Starter 
Quote:
Originally Posted by tompsonn View Post

No need for the bold tags, I'd have figured it out smile.gif

I don't come in this section of OCN much so I don't know how you guys work tongue.gif

Anyway, that code seems to be working alright, but I can't test properly right now since I need to work on something else first.

Thanks for the help smile.gif
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
     
CPUMotherboardGraphicsRAM
i5-4278U @ 2.6GHz Apple Logic Board Intel Iris 5100 8GB 1600MHz DDR3L 
Hard DriveOSOSMonitor
128GB SSD Mac OS X 10.11 El Capitan Windows 10 13" IPS @ 2560 x 1600 
KeyboardPowerCaseMouse
Apple British Keyboard 60W Magsafe 2 Apple Aluminium Unibody Apple Trackpad 
  hide details  
Reply
post #6 of 6
No problem, just remember there's various scopes at which you can store data. We're using class scope, which is useful when you need to share data between methods of the same class, or other classes if you change their visibility smile.gif
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming