User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,961 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,731 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 2517 | Replies: 7
Reply
Join Date: Nov 2006
Posts: 8
Reputation: foo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
foo foo is offline Offline
Newbie Poster

Help I want to learn - How to create and use objects.

  #1  
Nov 8th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I want to learn - How to create and use objects.

  #2  
Nov 8th, 2006
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.
Last edited by tgreer : Nov 8th, 2006 at 10:54 am.
Reply With Quote  
Join Date: Nov 2006
Posts: 8
Reputation: foo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
foo foo is offline Offline
Newbie Poster

Re: I want to learn - How to create and use objects.

  #3  
Nov 8th, 2006
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.
Last edited by foo : Nov 8th, 2006 at 11:02 am.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I want to learn - How to create and use objects.

  #4  
Nov 8th, 2006
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.
Reply With Quote  
Join Date: Nov 2006
Posts: 8
Reputation: foo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
foo foo is offline Offline
Newbie Poster

Re: I want to learn - How to create and use objects.

  #5  
Nov 8th, 2006
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.
Last edited by foo : Nov 8th, 2006 at 1:02 pm.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I want to learn - How to create and use objects.

  #6  
Nov 8th, 2006
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.
Last edited by tgreer : Nov 8th, 2006 at 1:21 pm.
Reply With Quote  
Join Date: Nov 2006
Posts: 8
Reputation: foo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
foo foo is offline Offline
Newbie Poster

Re: I want to learn - How to create and use objects.

  #7  
Nov 8th, 2006
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...
Last edited by foo : Nov 8th, 2006 at 2:27 pm.
Reply With Quote  
Join Date: Dec 2004
Posts: 1,590
Reputation: tgreer is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 34
Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: I want to learn - How to create and use objects.

  #8  
Nov 8th, 2006
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).
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 7:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC