A class is a recipe for an object. A class is a "design time" object, and when the code runs, the class creates an instance of an object.
Think of a class as a blueprint. A single blueprint can be used to build multiple houses. Each house is unique and distinct, and can even have different characteristics (properties!) such as paint color, brick vs. stone, etc.
An object can have properties, and methods. It can also respond to or create events.
Most of the examples in books aren't very clear, and aren't good examples of classes or objects. No one writes a "shoe" class.
As for how objects relate to each other: I often have to write programs that work with truly huge files. Now, C# provides a StreamReader class. It does buffered file IO.
In my applications, I'll write a class that does some specific operations on a file. However, I don't want to write all the underlying buffered reads and so on. I write my class to require that the "user" give my class/object an already built, instantiated StreamReader object. In my class, this is a simple property. To use it, the programmer/user would have to do something like:
StreamReader myStreamReader = new StreamReader();
// set the streamreader properties, etc. Open a file, and so on.
tgreerFileClass myCustomObject = new tgreerFileClass();
myCustomObject.fileToActOn = myStreamReader;
myCustomObject.doSomethingToTheFile();
This code has two objects: a StreamReader, and a "tgreerFileClass". The StreamReader object is created, and then passed "into" the custom object as a property. Then, I call a method of the tgreerFileClass. Internally, the tgreerFileClass will call the methods of the StreamReader object to do basic file IO tasks.
tgreer
Made Her Cry
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
It depends on what you mean by "handle" array objects, but if you want to create a class which uses an array as a property, you would define the class like so:
public class myCustomObject
{
// define a basic constructor method
public myCustomObject()
{
}
// define a property
private int[] _intArray; // an integer array object
// make it public
public string intArray
{
get
{
return _intArray;
}
set
{
_intArray = value;
}
}
}
To use it, in your main program class, you would:
int[] myInt;
myInt = new int[3] {0, 1, 2};
myCustomObject X = new myCustomObject()
myCustomObject.intArray = myInt;
Now, you could create methods inside myCustomObject that "handle" the array, doing anything you want with it.
There is no substantial difference for any object... everything in C# is an object.
tgreer
Made Her Cry
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
C# arrays are homogenous (meaning, all elements of the array are of the same basic type). You can't have a string and an integer in the same array. C# also provides an "ArrayList" object, which is more robust.
But again, you could create a property in your class that is of type ArrayList, and pass in an ArrayList object.
An array "of objects" is a bit vague. EVERYTHING is an object, so any array is technically an "array of objects".
But you can make an array out of any object. If you created your own class, called "Customer", that had a lot of properties and methods, you could create an array of customers:
Customer[] myCustomerArray = new Customer[30];
That would be an "array of objects", specifically, an array of Customer objects. That array could also be used as a property of any other object, if the object defined a "Customer[]" property.
tgreer
Made Her Cry
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
Yes. Once an array is initialized, though, that's it. If you make a 10-element array, you can't add an 11th element. That's what the ArrayList object is for, a more robust array class. There is also a List object. You just pick what you need for the task at hand.
If you're just learning, though, I wouldn't get too wrapped up in or distracted by arrays. Work on classes, constructor methods, private and public properites, and methods. Study the modifers (static, private, public). Learn how to assign methods as event handlers (delegates).
tgreer
Made Her Cry
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37