The concept of "structure" or "record" was created before the concept of "class". And sometimes they can be changed.
When to use a variable, field or instance of a structure:
(1) When only data members are needed, without methods. Or has several methods that are barely used, such as constructors.
Example:
struct CoordinateStruct { int X; int Y; }
(2) When a large number of elements are required, and since objects use additional memory and are sometimes not visible, methods are transferred to the container element, such as another object, an object (namespace), such as an object, or a singleton object (Flyweight Design Template).
Before:
class PointClass { int X; int Y;
After:
struct PointStruct { int X; int Y; // its a low memory integer int Color; } // multiple element container class CADAppClass { List<Point> Points; public void assignPoint (ref PointStruct Point, int NewX, int NewY, Color NewColor) { ... } public void movePoint (ref PointStruct Point, int addX, int addNewY) { ... } // ... }
(3) When you need to encapsulate data elements, even if it is a small number of elements in a variable, even if there are methods, operators, events, or "messages". Encapsulation, possibly for serializacion as an example (loading from DLL, DCOM, JSON, files with ordinary data).
Before:
class CustomerClass { String FirstName; String LastName; public void Store(Stream Destination) { ... } public void Load(Stream Source) { ... } public void CopyTo(CustomerClass Destination) { ... } public void CopyFrom(CustomerClass Source) { ... } }
After:
struct CustomerStruct { char[100] FirstName; char[100] LastName; }
I used this one, with enumerations instead of structures, in programming languages oriented to objects and classes, but which do not treat enumerations as objects (like object oriented PHP and Object Pascal) like Java or C # does.
(4) A mixture of previous cases or scenario is not considered.
Greetings.