Just Started learning Visual Studio and C#. I am at the point in the book where it discusses the use of objects.
This is a really confusing subject.
What I want to understand is after you've used a class to create multiple objects, how do you make sure that you are using the right ones to do things with? I mean, for example; I create and object, how do I pass that object as information for another object? Like if it was to become the property of some other object. If I had a region object and the region objects had store objects. How does something like that translate into code?

Any examples or even small challenges on creating an object and then so on would be very appreciated. I need some kind of little project that someone can give me and help me through with it to understand how this all works.
I hope someone has the time. Thank you in advance for looking and/or any considerations.

Recommended Answers

All 7 Replies

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.

myCustomObject.fileToActOn = myStreamReader;

So
fileToActOn
is a property that myStreamReader is going into.

So now, myCustomObject has it's own specific myStreamReader.
What if you wanted to have an array of objects in myCustomObject.
How would that look?

EDIT:
Actually, how about this; make up a class that will handle array objects. I would like to see that if you don't mind.

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.

int[] myInt;
myInt = new int[3] {0, 1, 2};

myCustomObject X = new myCustomObject()
myCustomObject.intArray = myInt;


Ah, ok, assign the new array you create within main to the object's array. Very cool.
And this same array could not only hold primitive types like int and double but it could hold objects too?
So the object constructed from the class can now have this array and it can hold objects within that objects array.

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.

Customer[] myCustomerArray = new Customer[30];

wow

Ok, this is wild. We make a Customer class. It has it's properties and methods.
We create an object, say walmartCust which we could then create an array of walmart customers. So walmartCust[] jb117 = new walmartCust[0];
Each index in the array would be a customer and all the properties and methods associated with that customer object, right?
And then accessing each individual customer's properties would be grabbing the object from that index of the array. I guess processing all that info back out would be hard to do?
Actually, the array should be a higher number than 0. But we would never know how many customers they might end up with. So how can the array be made up to be filled later?
Thanks a lot for explaining these things. I hope to get on track with further discussion.

EDIT:
nah, something's wrong with walmartCust[] jb117 = new walmartCust[0];
Just looking at it, it doesn't seem right. I dont want to make an array jb117(the customers name).
ok, back to your example. Customer[] walmartCusmrs = new Customer[10]; would be more likely I think...

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.