[RESOLVED] A Public object is not accessible from another class.
I'll try to explain this the simplest way I can, and I'll be happy to provide more elaboration as needed.
I've created a class, class playership, as part of a little strategy game for my own practice.
I've created the instance of a playership object in my main form using the following code:
Code:
Public Class Form1
Public mainship As New playership
End Class
I need to access this object from another windows form, a dialog box to be exact, to set its properties.
However, when I attempt to set the properties of the object in the dialog box's code, as such:
Code:
Public Class dialog1
Sub whatever() 'Stats are set within an event in the code
mainship.setstats(attacktoset, defensetoset)
End Sub
End Class
I'm told that: "mainship is not declared. It may be inaccessible due to its protection level."
I'm a little confused about this because I thought a public object or variable could be accessed from anywhere within a project. So, my question is, what can I do to make this object accessible from anywhere in my project?
If you need more elaboration, please let me know. I greatly appreciate the help.
Re: A Public object is not accessible from another class.
Would you try to call SetStats without referencing the class instance (mainship)? If not, then why would you try to reference mainship, which is a member of class Form1, without referencing the class instance that contains it (your instance of Form1)?
That's the whole problem.
Re: A Public object is not accessible from another class.
When you use a variable, it's like writing an address. First you describe the object the variable belongs to, then you describe the variable. So an employee's name might be "employee.Name". However, if that employee belongs to a Roster object named "roster", you have to tell VB to use the employee that belongs to the roster: "roster.employee.Name".
If you don't specify an object, "Me" is assumed. Since Dialog1 doesn't have a mainship variable, you get the error. You have two possible solutions, both very similar:
- Let the dialog and the form share a reference to the ship.
- Let the dialog have a reference to the form.
Either way, the dialog needs to know the "address" of the ship.
The technique I prefer is to let the two forms share the object. Your dialog would look like this:
Code:
Public Class Dialog1
Public Property MainShip As PlayerShip
Sub Whatever()
MainShip.SetStats(...)
End Sub
End Class
So long as whoever creates the dialog sets that property, this will work. For example, your Form1 might look like this:
Code:
Public Class Form1
Public MainShip As New PlayerShip()
Sub DisplayDialog()
Dim newDialog As New Dialog1()
newDialog.MainShip = MainShip
newDialog.ShowDialog()
End Sub
End Class
It is very important to remember to set that property! If you do not, the dialog's MainShip property will be Nothing and you'll get a NullReferenceException. You might think this won't work; this would be based on a belief that when you assign Form1's MainShip to Dialog's MainShip, a copy is made. This is incorrect so long as MainShip is a class. Let's look at this line:
Code:
newDialog.MainShip = MainShip
Inside the program, there is a memory address that describes where the ship exists. We'll call this address a pointer, since it points to the ship. Form1's MainShip variable doesn't contain a ship. Instead, it contains a pointer to a ship. So when you set Form1's MainShip and Dialog1's MainShip to the same thing, what you end up with is two copies of the pointer to the single ship. It helps to think of it like real addresses. When you write the address to my house, there is one copy of the address and one house. If you give my address to a friend, now there are two copies of the address but still one house, and no matter whether you or your friend uses the address the letter will end up in the same place. So when you assign variables like this, keep in mind that both forms share the same object.
Re: A Public object is not accessible from another class.
Here is a thread on various ways that could be used to solve this, as you are passing some information between classes, whether you see it that way or not.
http://www.vbforums.com/showthread.php?t=466253
Re: A Public object is not accessible from another class.
Thanks guys!
I understand now, that an object belongs to a class that it is created in. I used the first method, making the object mainship a public property in the dialog1 class. It now works :)