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.
7 1884
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. -
public partial class frmForm2 : Form
-
{
-
Players pMyPlayersClass; //This is a players class belonging to THIS form
-
-
public frmForm2(Players myPassedPlayersClass)
-
{
-
//Here we are accepting the class from Form1
-
//and filling this form's players class with the info
-
//so it can be accessed once this constructor is finished
-
-
pMyPlayersClass = myPassedPlayersClass;
-
}
-
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: -
//Somewhere on Form1
-
frmForm2 myForm2 = new frmForm2(myOriginalPlayersClass);
-
myForm2.Show();
-
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
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
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
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
Actually, I quickly tried it, but failed.
The code -
namespace GolfCaddy
-
{
-
public static class MPlayers
-
{
-
private Players[] player;
-
-
public Players[] MyPlayer
-
{
-
get { return player; }
-
set { player = value; }
-
}
-
-
}
-
}
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 - Players newplayer = new Players();
then attach is to the Static Class such as - 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
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. -
namespace GolfCaddy
-
{
-
public static class MPlayers
-
{
-
private Players player;
-
-
public Players MyPlayer
-
{
-
get { return player; }
-
set { player = value; }
-
}
-
-
}
-
}
I also answered my own question. Yes, you need to initiate the class Players then assign it to the static class.
Thanks,
Rob
Great work Rob!
Keep us posted with your progress
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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; }
}
|
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
|
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;
|
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
| |
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
|
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;
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |