473,799 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Class once intialized from another form

20 New Member
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 1884
markmcgookin
648 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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 New Member
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 New Member
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 Recognized Expert Contributor
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
6336
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 pointer to pass to my sub-class. I tried to define a listbox in my new class and then code: class.listbox = this.listbox; The compiled said OK but I had an exception when I attempted to access the
2
1700
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 here; } public displaystuffinwindow() { display stuff here; } }
9
2683
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 type-stored on the stack Regards thomson
6
7767
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 MyNameSpace.Form1 cForm1; and I am trying to use it later as TextBox.Text = cForm1.TextBox.Text;
8
3561
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 application. How can I access the current instance of a specific Form object? For example: Class frmOne ....... Public Readonly Property XYZ() as String Get
9
4814
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 class in the same project? Thanks Blake
1
1552
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 namespace System::Collections;
1
1350
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 new form fine, but I am unsure as how to perform function calls from the new form once it has been shown. If anyone could point me in the right direction that would be grand. I have been googling around for about an hour now trying to figure this...
9
2652
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 to another location because it populates a Textbox in the user control page. Thanks Andrew
3
2218
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 where the form code is but how do i get to the controls themselves. newbie in the class world.. still trying to figure it all out :) Thanks for any help...
0
9686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.