Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [C#] Working with Program.cs and Form.cs
New Posts  All Forums:Forum Nav:

[C#] Working with Program.cs and Form.cs

post #1 of 10
Thread Starter 
Howdy guys, beginner programmer here, figured I'd run this past the pros while I was searching for the answer online. Hopefully this is just a simple question that I'm not thinking through correctly. Rep to anyone who can help.

Basically I'm working on a GUI program in C# using Visual Studio 2008, and I've got 10-15 Form windows that I've got to pass information between.

My theory was that I could put the variables and methods in the Program.cs, so that they could be accessed in the individual forms, that way everything would be centralized and I wouldn't need to worry about the code being spread out in each form window. This way each form would be able to call methods and access/modify variables.

However, I'm not sure if this is possible/how to go about enabling the forms to see what is written in the Program.cs.

How would I go about doing this? Or is there a more efficient way?

Thank you very much in advance, and if more information is necessary please let me know.
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
post #2 of 10
try Public int var; for example?

I`m also a c# beginner thats how I would do it in delphi smile.gif
(delphi has a special section for public and private var)
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
post #3 of 10
Thread Starter 
I've attempted that, thinking it would work as the forms would see it, but they don't seem to... Not sure why though.
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
post #4 of 10
then try in one of the forms?
then form1.integer = ?
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
post #5 of 10
Thread Starter 
Tried that as well... I'm not sure if every single thing I want to pass between them needs its own get/set or what exactly...
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
post #6 of 10
howbout a hidden text box on one of the forms? tongue.gif
not very good/efficient but might work
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
AzuraChan
(13 items)
 
Haruna
(7 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II x6 1055T Asus M4A88TD-V Evo/Usb3 EVGA GTX460 1GB SC + 9800GT Phsyx 2x2GB Corsair 1333MHZ 
Hard DriveOSMonitorPower
6.8TB Total w7 + ubuntu 19" something Seasonic S12II 520W 
Case
modded antec 902 
CPUGraphicsRAMHard Drive
I7 3630QM GTX660M 2GB + HD4000 8GB 1600Mhz 1TB  
Optical DriveOSCase
Blu Ray thing Win8 64bit Lenovo Y580 
  hide details  
Reply
post #7 of 10
Quote:
Originally Posted by Wesker;12790829 
Tried that as well... I'm not sure if every single thing I want to pass between them needs its own get/set or what exactly...

That would be the good practice thing to do in C#

Additionally, instead of using the Program.cs class, make a new one to handle all of the logic, it shouldn't affect the actual performance of the program, but its just more organized.
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #8 of 10
Thread Starter 
I've got a new class now that has the methods I want the different forms to use, however, the forms only see the class, and not the methods, despite them all being public.

Maybe I'm too much of a newbie/just not thinking this through logically, but why can't the forms access the methods in the new class?

Thanks,

W.
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
Dovahkiin
(16 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 2600k  ASUS P8P67 Motherboard EVGA GTX 560 Ti 2GB  EVGA GTX 560 Ti 2GB  
RAMHard DriveHard DriveHard Drive
8GB Ripjaws X Series DDR3 1600 (PC3 12800) Crucial M4 128 GB 1.5TB Western Digital Caviar Black  1.5TB Western Digital Caviar Black  
Optical DriveCoolingOSMonitor
LITEON Blu Ray Drive Corsair H80 Windows 7 Professional x64 ASUS 27 Inch  
KeyboardPowerCaseMouse
Razer BlackWidow Ultimate Corsair AX1200 Heavily Modified Diablotek EVO Razer Naga 
  hide details  
Reply
post #9 of 10
The easiest way would be to make the class static or you can declare a static instance of it in something like globals.cs (I tend to not like cluttering up Program.cs whenever possible).

If you are using WPF as your gui, you can bind the value of the controls onto each other. This is not something that is easy, but is very slick.
post #10 of 10
Don't know if its too late to respond to this, but if you have a choice, go WPF. Winforms is around, but WPF is the newest iteration.

Also, learn about MVC pattern, or with WPF MVVM. The idea is you want to separate your Model (what holds your data) from your View (what shows the data) and the Controller (what communicates between the view and the model). The controller in MVVM is the ViewModel.

All good design patterns.
My System
(13 items)
 
  
Reply
My System
(13 items)
 
  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › [C#] Working with Program.cs and Form.cs