473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Object and objects

Hello,

I still don't get it!

Let's say that we have some persons.... some persons have a car.
The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???

I hope that I get it the basics of OO then.
Thanks!

Nov 15 '05 #1
10 1251
This sounds more like information that should be stored in a database so it
can be easily queried.

I don't think what you are asking is a good example of what OO programming
is about.

"Arjen" <bo*****@hotmail.com> wrote in message
news:bt**********@news4.tilbu1.nb.home.nl...
Hello,

I still don't get it!

Let's say that we have some persons.... some persons have a car.
The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???

I hope that I get it the basics of OO then.
Thanks!

Nov 15 '05 #2
Arjen wrote:
How can I see (save) which persons have which cars?
The Person class has a Car property which will return null if they do not
have a car.
I hope that I get it the basics of OO then.


I think you're hoping for a bit much from such a simple example.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #3
On the internet I cannot find exactly what I want to know.
I want to save thing inside the application inside arrays.

After that I want to go to serialization.
Nov 15 '05 #4
The Person class has a Car property which will return null if they do not
have a car.

And thats the sample where I am looking for.
Nov 15 '05 #5
"Arjen" <bo*****@hotmail.com> wrote in message
news:bt**********@news4.tilbu1.nb.home.nl...
Hello,

I still don't get it!

May I respectively suggest that you first complete a basic tutorial in OOD
[Object Oriented Design] rather than trying to concurrently master both OO
and C# syntax / pragmatics. Without a little background in OOD, an area
which deals with modelling objects and object relationships in an abstract,
language-independant way, you will find it difficult to make rapid learning
progress with a language such as C#.

One such tutorial you might like to consider:

http://www.juicystudio.com/tutorial/ooad/

If this is not suitable for you, a Google search for OOD Tutorials will turn
up many links, one or more of which will certainly be useful to you.

Let's say that we have some persons.... some persons
have a car. The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???

I hope that I get it the basics of OO then.
Thanks!


You first need to model the relevant entities, then the relevant
relationships between those entities.

One possibility is to have the Person class contain a collection [modelled
by an array, or a Collection-type class] of the Car objects 'owned' by it.
Of course you'll have to expand your class to allow for adding, removing,
and querying of Car objects.

Another is to create a separate class, say OwnedCars, which specifically
links Car objects with Person objects [this can be seen as an in-memory
database]. This approach is more elaborate, and more general.

There are, needless to say, many other possible approaches, all having some
merit, and each being more or less suitable depending entirely on what the
intended task will actually be. The C# language will certainly allow you to
model any one of these, but the key is to first design a suitable OOD for
such implementation.

I hope this helps.

Anthony Borla
Nov 15 '05 #6
"Arjen" <bo*****@hotmail.com> wrote in message news:bt**********@news4.tilbu1.nb.home.nl...

I still don't get it!

Let's say that we have some persons.... some persons have a car.
The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???


You should consider the limitations on the relationships first.
Can a person have more than one car?
Can a car be owned by more than one person?

"Which persons have which cars" is also quite impresize.
What exactly do you have on input and what do you have on output?
You obviously want to find owned car(s) when presented with instance of a person.
Do you want to be able to find owner(s) when given instance of a car?

You should also decide what attributes a car and a person have.

I will give a most trivial example with the following assumptions:

1. Car has 3 attributes - make, model and year.
2. Person has 2 attributes - name and car (which can be null).
3. Thus, a person can have at most one car.
4. It is possible for several persons to own the same car.
5. Car object does not know which person(s) own it.

using System;
using System.Collections;

namespace PersonsAndCars
{
class Car
{
public readonly string Make;
public readonly string Model;
public readonly int Year;

public Car( string Make_, string Model_, int Year_ )
{
Make = Make_;
Model = Model_;
Year = Year_;
}
}

class Person
{
public readonly string Name;
public Car Car;

public Person( string Name_, Car Car_ )
{
Name = Name_;
Car = Car_;
}
}

class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
ArrayList People = new ArrayList();

// add person named Test Testov who has a 1999 Toyota Corolla
People.Add(new Person("Test Testov", new Car("Toyota", "Corolla", 1999) ) );

// add person named Poor Darling, who does not have a car
People.Add(new Person("Poor Darling", null) );

// add person named Qwerty Uiopov, who owns 2004 Cadillac DeVille
Car FamilyCadillcac = new Car("Cadillac", "DeVille", 2004);
People.Add( new Person( "Qwerty Uiuopov", FamilyCadillcac ) );

// add person named "Testina Uiuopova" who owns the same car as Qwerty
People.Add( new Person( "Testina Uiopova", FamilyCadillcac ) );

// now print who has what
foreach (Person P in People)
{
if (P.Car != null)
{
Console.WriteLine("{0} has {1} {2} {3}", P.Name, P.Car.Year, P.Car.Make, P.Car.Model );
}
else
{
Console.WriteLine("{0} has no car", P.Name);
}
}
}
}
}
Nov 15 '05 #7
"Arjen" <bo*****@hotmail.com> wrote in message news:bt**********@news4.tilbu1.nb.home.nl...

I still don't get it!

Let's say that we have some persons.... some persons have a car.
The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???


You should consider the limitations on the relationships first.
Can a person have more than one car?
Can a car be owned by more than one person?

"Which persons have which cars" is also quite impresize.
What exactly do you have on input and what do you have on output?
You obviously want to find owned car(s) when presented with instance of a person.
Do you want to be able to find owner(s) when given instance of a car?

You should also decide what attributes a car and a person have.

I will give a most trivial example with the following assumptions:

1. Car has 3 attributes - make, model and year.
2. Person has 2 attributes - name and car (which can be null).
3. Thus, a person can have at most one car.
4. It is possible for several persons to own the same car.
5. Car object does not know which person(s) own it.

using System;
using System.Collections;

namespace PersonsAndCars
{
class Car
{
public readonly string Make;
public readonly string Model;
public readonly int Year;

public Car( string Make_, string Model_, int Year_ )
{
Make = Make_;
Model = Model_;
Year = Year_;
}
}

class Person
{
public readonly string Name;
public Car Car;

public Person( string Name_, Car Car_ )
{
Name = Name_;
Car = Car_;
}
}

class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
ArrayList People = new ArrayList();

// add person named Test Testov who has a 1999 Toyota Corolla
People.Add(new Person("Test Testov", new Car("Toyota", "Corolla", 1999) ) );

// add person named Poor Darling, who does not have a car
People.Add(new Person("Poor Darling", null) );

// add person named Qwerty Uiopov, who owns 2004 Cadillac DeVille
Car FamilyCadillcac = new Car("Cadillac", "DeVille", 2004);
People.Add( new Person( "Qwerty Uiuopov", FamilyCadillcac ) );

// add person named "Testina Uiuopova" who owns the same car as Qwerty
People.Add( new Person( "Testina Uiopova", FamilyCadillcac ) );

// now print who has what
foreach (Person P in People)
{
if (P.Car != null)
{
Console.WriteLine("{0} has {1} {2} {3}", P.Name, P.Car.Year, P.Car.Make, P.Car.Model );
}
else
{
Console.WriteLine("{0} has no car", P.Name);
}
}
}
}
}
Nov 15 '05 #8
Ivan,

All right, this looks pretty cool!

I want to go to this line:
// add person named Poor Darling, who does not have a car
People.Add(new Person("Poor Darling", null) );

At the end you show who has what.
We can see there that Poor Darling doesn't have a car.

How can we, select "Poor Darling" and give him a car... before we show who
has what?
Now we have to work with a indexes???

Thanks!


"Ivan Krivyakov" <i.***@verizon.net> schreef in bericht
news:Of****************@TK2MSFTNGP11.phx.gbl...
"Arjen" <bo*****@hotmail.com> wrote in message news:bt**********@news4.tilbu1.nb.home.nl...

I still don't get it!

Let's say that we have some persons.... some persons have a car.
The objects that we have are "person" and "car".

"Person"
-- Create person
-- Haves car

"Car"
-- "Create car"

How can I see (save) which persons have which cars?
Can you please give me an working example???


You should consider the limitations on the relationships first.
Can a person have more than one car?
Can a car be owned by more than one person?

"Which persons have which cars" is also quite impresize.
What exactly do you have on input and what do you have on output?
You obviously want to find owned car(s) when presented with instance of a

person. Do you want to be able to find owner(s) when given instance of a car?

You should also decide what attributes a car and a person have.

I will give a most trivial example with the following assumptions:

1. Car has 3 attributes - make, model and year.
2. Person has 2 attributes - name and car (which can be null).
3. Thus, a person can have at most one car.
4. It is possible for several persons to own the same car.
5. Car object does not know which person(s) own it.

using System;
using System.Collections;

namespace PersonsAndCars
{
class Car
{
public readonly string Make;
public readonly string Model;
public readonly int Year;

public Car( string Make_, string Model_, int Year_ )
{
Make = Make_;
Model = Model_;
Year = Year_;
}
}

class Person
{
public readonly string Name;
public Car Car;

public Person( string Name_, Car Car_ )
{
Name = Name_;
Car = Car_;
}
}

class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
ArrayList People = new ArrayList();

// add person named Test Testov who has a 1999 Toyota Corolla
People.Add(new Person("Test Testov", new Car("Toyota", "Corolla", 1999) ) );
// add person named Poor Darling, who does not have a car
People.Add(new Person("Poor Darling", null) );

// add person named Qwerty Uiopov, who owns 2004 Cadillac DeVille Car FamilyCadillcac = new Car("Cadillac", "DeVille", 2004);
People.Add( new Person( "Qwerty Uiuopov", FamilyCadillcac ) );

// add person named "Testina Uiuopova" who owns the same car as Qwerty People.Add( new Person( "Testina Uiopova", FamilyCadillcac ) );
// now print who has what
foreach (Person P in People)
{
if (P.Car != null)
{
Console.WriteLine("{0} has {1} {2} {3}", P.Name, P.Car.Year, P.Car.Make, P.Car.Model ); }
else
{
Console.WriteLine("{0} has no car", P.Name);
}
}
}
}
}

Nov 15 '05 #9
"Arjen" <bo*****@hotmail.com> wrote in message news:bt**********@news4.tilbu1.nb.home.nl...

How can we, select "Poor Darling" and give him a car...
before we show who has what?
Now we have to work with a indexes???


It depends on what exactly you want to do.
If you want to select a specific person named "Poor Darling" and give him a car,
you can do something like

foreach (Person P in People)
{
if (P.Name == "Poor Darling")
P.Car = new Car( some_parameters );
}

If you want to give a car to everybody without a car, you should do something like

foreach (Person P in People)
{
if (P.Car == null)
P.Car = new Car( some_parameters );
}

Ivan
Nov 15 '05 #10
Thanks!

I have test it... and it works.
But the best thing is is that I understand it.

Thanks for your support!

"Ivan Krivyakov" <i.***@verizon.net> schreef in bericht
news:uo**************@TK2MSFTNGP10.phx.gbl...
"Arjen" <bo*****@hotmail.com> wrote in message news:bt**********@news4.tilbu1.nb.home.nl...

How can we, select "Poor Darling" and give him a car...
before we show who has what?
Now we have to work with a indexes???


It depends on what exactly you want to do.
If you want to select a specific person named "Poor Darling" and give him

a car, you can do something like

foreach (Person P in People)
{
if (P.Name == "Poor Darling")
P.Car = new Car( some_parameters );
}

If you want to give a car to everybody without a car, you should do something like
foreach (Person P in People)
{
if (P.Car == null)
P.Car = new Car( some_parameters );
}

Ivan

Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
100
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.