Object and objects

  • Thread starter Thread starter Arjen
  • Start date Start date
A

Arjen

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!
 
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 said:
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)
 
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.
 
Arjen said:
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
 
Arjen said:
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);
}
}
}
}
}
 
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 said:
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 );
 
Arjen said:
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
 
Thanks!

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

Thanks for your support!
 
Back
Top