Hi,
Trying hard to immerse myself in OOP but I must be missing some fundamental concept.
So I’ve added a Class called Product to a module with various properties (productID,price,description etc.)
I’ve then added a Property to the module called Products() - to store an array of products
I’ve added code to populate the array from database table… all good so far
I’ve then added a Method to the Product class called IndexByProductID which would return the array index of a product for a given ProductID. Seems perfectly logical to me but when I try to use it like this
dim p as integer = GeneralModule.Product.IndexbyProductID(me.ProductID)
I get the error Static reference to instance method: call this on an instance of class GeneralModule.Product
The error sort of makes sense but leaves me unsure how to use the method I’ve created. I assume I’m not quite getting the concept of how/when to use a method on a class. Tried a few searches but if anyone can point me in the right direction in this instance it would be appreciated.
Thanks
Without additional information, i’d say try the following please:
Add a Public Contructor Method to your Class which takes the ProductID as (an Optional?) Parameter and loads the Data into the Properties of the Product, base uppon this ProductID. Then…
Var prod As New GeneralModule.Product(me.ProductID)
Var p as integer = prod.Index
I’m not sure what you aim to achieve. It seems you have defined a normal method of a class and you can call this method with any instance of that class – like this:
var aProduct as new GeneralModule.Product
… Some code setting the properties of aProduct …
var p as integer = aProduct.IndexbyProductID(me.ProductID)
But maybe what you really want is a shared method that can be called with a class? To quote from the documentation: “Shared Methods (also called class methods) are methods that belong to the class rather than an instance. A shared method is like a “regular” method, except it belongs to the class, not an instance of the class. A shared method can be accessed from anywhere its scope allows.”
It sounds like you have created the class and method and all correctly but then you have to instantiate a “New” version of that class to use it. For example, a Button is a class that Xojo has created. When you put a button from the controls library into your project you create a “New” instance of that button. Same thing with classes that you create. You could have multiple copies of that class in your code each with different properties.
What you’ve done is create a blueprint, but haven’t built anything using it. Imagine you have drawn up the plans for a house. Then you come along with your power tool, and say, well the plans say there’s a power socket I can use, so where do I plug this tool in?
Answer: you have to build a house first - or, in Xojo terms, create an instance of the house:
Var myhouse as house
myhouse = new house
Only then does a socket exist to plug the tool into.
Actually here’s a related question. When an instance is created, it gets its own set of properties (if any) of the class. Does it get its own set of methods, or are these somehow shared amongst class instances to save memory?
Internally, in the binary, the code is just one, just the data gets a new copy at instantiation time (unless marked as “shared”, belongs to the class not the object, those are static).
it could be look like this but Find Method could also be part of module1
as already mentioned shared method or shared propertie means you can call
ProductClass.Find method without having a object of this class.
because you will find a id in a list of producs, find in the product class itself is the wrong place.