473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Accessing Class once intialized from another form

20
I have one form that initializes a class, called Players.

But, I need to access these initialized values in another form.

Obiously, if I do Players player = new Players(); it creates a new instance of the Players class, but has all variables set to defaults, not the previous values when the first form was run.

I have tried just Players player; the compile compiles OK, but then I get an NULL REFERENCE error when trying to access the class..

Can I access this initialized class information from another form? I have read a bit, but I think I am confusing myself.
Jan 21 '10 #1
7 1866
markmcgookin
648 Expert 512MB
Hey Rob,

No worries, glad to hear everything is going well.

When you have created your Players class on Form1, it's just like creating any class, whether it be a string, an int, or anything else, it now "belongs" to Form1, so if you create Form2, it doesn't know anything about your players class.

There are a few ways of solving this, here would be my two options:

1) Create a static class that contains your Players class variable.. this way this class can be accessed from anywhere in the application and updated/read from whenever you want... we can chat about this a bit more if you decide to go down that route

2) This is the simpler option, however it has it's limits. Which is to pass your existing players class into your new form. This really only suits if you want to read from this class, as if you want to change/update it's values and then use those again in other places, it can be a bit trickey.

to do this, go to the constructor of your "Form2", add a players variable for this form and allow it to accept a Players class, i.e.

Expand|Select|Wrap|Line Numbers
  1. public partial class frmForm2 : Form
  2.     {
  3.         Players pMyPlayersClass; //This is a players class belonging to THIS form
  4.  
  5.         public frmForm2(Players myPassedPlayersClass)       
  6.         {
  7.               //Here we are accepting the class from Form1
  8.               //and filling this form's players class with the info
  9.               //so it can be accessed once this constructor is finished
  10.  
  11.               pMyPlayersClass = myPassedPlayersClass;
  12.         }
  13.  
So now your second form has a copy of the first players class, all it's properties and methods

You simply pass the value into it as follows:

Expand|Select|Wrap|Line Numbers
  1. //Somewhere on Form1
  2. frmForm2 myForm2 = new frmForm2(myOriginalPlayersClass);
  3. myForm2.Show();
  4.  
Hope that helps,

Just remember that this is a COPY of the original Players, and not the original, so if you change something in the Form2 one (like delete a player) then close that form, it will not be deleted in the Players on Form1. If you want to reflect changes like that, we need to use Option 1.

Let me know if you want to try option 1 and need help with it.

Mark
Jan 21 '10 #2
rb0135
20
Thanks Mark

The good thing in my case, is that the first form setups the Players info. The form I need access the info on, only needs to READ it (and there are no other forms that need this info) but the first form DOESNT call the second form where I need the data... it is the form after which is called by the second form. This may sound strange, but the steps are Select Player and that form shows the players stats/info, then select Course (another form showing course info) then select the Hole you are playing (another form showing hazards, hole info, calculates distances, calculates club slection which is why I need the players info as this contains the clubs for the player, etc).

I do prefer your first option though (because that is what I would do in VB.net).

I tried doing the STATIC class but got a compiler error "access modifiers are not allowed on static constructors".

I suppose you need some code to show how I have constructed the Players class, but it has a lot in it.

I'll have another go at your option 1 suggestion.

Thanks,
Rob
Jan 21 '10 #3
markmcgookin
648 Expert 512MB
Hi Rob,

Well you could pass the Players variable multiple times with no issue, but yeah sorting out the static class would be less hassle. Just give the static class a private variable of Players and give it some get{} set{} methods and it should be ok

Mark
Jan 21 '10 #4
rb0135
20
Thanks for the tip.

I think I did something similar with the CLUBS class within the Players class (but not static).

Will give it a go soon. Probably post back tomorrow.

Thanks,
Rob
Jan 21 '10 #5
rb0135
20
Actually, I quickly tried it, but failed.

The code
Expand|Select|Wrap|Line Numbers
  1. namespace GolfCaddy
  2. {
  3.     public static class MPlayers
  4.     {
  5.         private Players[] player;
  6.  
  7.         public Players[] MyPlayer
  8.         {
  9.             get { return player; }
  10.             set { player = value; }
  11.         }
  12.  
  13.     }
  14. }
I get a compiler error on the private Players[] player stating "cannot declare instance members in a static class"

Realised I needed static after the private and public Players[]

This compiled OK, but trying to use it, do i need to initiate the Players class
Expand|Select|Wrap|Line Numbers
  1. Players newplayer = new Players();
then attach is to the Static Class such as

Expand|Select|Wrap|Line Numbers
  1. MyPlayer.player = newplayer;
Sorry if this is obvious to other people.. I am learning and feel I have done pretty good so far.. Just cant get my head around it at the moment.. Well into the evening here.

Thanks,
Rob
Jan 21 '10 #6
rb0135
20
I Got it... Amazing what a sleep does.

In my code in the Static Class, for some reason I was thinking my Players was an array of players, when in fact, this was the single class after a player is selected elsewhere in the program.

So, I removed the array references, and everything just clicked in.

Expand|Select|Wrap|Line Numbers
  1. namespace GolfCaddy 
  2.     public static class MPlayers 
  3.     { 
  4.         private Players player; 
  5.  
  6.         public Players MyPlayer 
  7.         { 
  8.             get { return player; } 
  9.             set { player = value; } 
  10.         } 
  11.  
  12.     } 
I also answered my own question. Yes, you need to initiate the class Players then assign it to the static class.

Thanks,
Rob
Jan 21 '10 #7
markmcgookin
648 Expert 512MB
Great work Rob!

Keep us posted with your progress
Jan 22 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
I have a windows form listbox in one class and I need to reference it in another class, eg. I need to add an item to the listbox. What is the correct way to setup the listbox and then set a...
2
by: Greg Merideth | last post by:
Using Visual C# I created two forms such as namespace test { public class SystemTray : System.Windows.Forms.Form { public createwindow() { stuff here; } public fadewindow() { stuff to fade...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
6
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private...
8
by: Mike Caputo | last post by:
In VB.NET, need to be able to access certain properties on my main form from other forms. These are properties that may be changed by the user, so I have to be able to get to them throughout the...
9
by: Blake Weaver | last post by:
Ok, this is probably a no-brainer for most of you but its escaping me and I can't even seem to find the answer in any documentation. How do you access a friend variable of one class, from another...
1
by: Asfar | last post by:
Here is my problem: In file form1.h I have the following code: #pragma once #include "Test.h" namespace AccessCheck { using namespace System; using namespace System::ComponentModel; using...
1
by: Xarious | last post by:
Hi all, I need to bring up a form off of my base win form and then, depending on which button the user clicks, perform a function that is in my base form. I can create an instance and show the...
9
by: J055 | last post by:
Hi I have a standard asp page which uses a MasterPage. The MasterPage contains a User control. How can I access a public method in the User control from my WebForm page? I can't move the method...
3
by: M K | last post by:
I have 2 classes. One where the form resides and I created another one for all the database stuff. after i get data from the db i want to be able to update the form. I have the namespace of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.