New to OOP

Associate
Joined
3 Oct 2004
Posts
68
Location
Biggin Hill
Hi everyone,

I am starting to learn OOP, I am a C programmer by day, and wanted to start to learn about OOP.

One question I do have, is can an object be created dynamically, by this i mean, if I am reading in data from a database and for each unique reference number i want to create a new class with this reference number as the object name?

Is this possible? If I have not worded correctly or need more information, please ask.

Many Thanks for any help with this.

Dean
 
Probably a play in words but you only have one class but can have multiple objects.

You initialize the class when you import the data which creates an object, each new object can be unique (say defined/referenced by a name property) and be persistent but you only have one class.

Think of a class as a mold and an object the item what comes from the mold, you only have one mold but can create multiple castings from it.

Always the way I understood it anyway!
 
C# example:

Code:
public class SomeClass
{
  public string Name { get; set; }
}

var a = new SomeClass { Name = "Foo" };
var b = new SomeClass { Name = "Bar" };

Console.Out.WriteLine(a.Name);
Console.Out.WriteLine(b.Name);
Resulting output is:
Code:
Foo
Bar
 
I'm not quite sure what you mean by this.

An object is an instance of a class, so if you have a class that has members that correspond to what you want to retrieve from the database then you can create multiple instances each with their own unique values (e.g. the reference number)

Each of these instances is completely separate and the values in each one can be modified independently of the other instances.
Essentially the class is a template that is populated with different things for each object that is created.

(Note, static members complicate things a little, but as a beginner don't worry about that right now)
 
What does any of this have to do with reflection or generics?

Dynamically creating an object/class kind of gives it a point of reference.

is can an object be created dynamically, by this i mean, if I am reading in data from a database and for each unique reference number i want to create a new class with this reference number as the object name?

Means to me, I want to dynamically create an object during runtime with this unique reference number as its name.
 
Last edited:
Means to me, I want to dynamically create an object during runtime with this unique reference number as its name.

Dj_Jestar already posted a very simple example of how to do this in C# though.
I think you're needlessly over complicating things when all the OP seems to be asking is about constructing objects.
 
Dj_Jestar already posted a very simple example of how to do this in C# though.
I think you're needlessly over complicating things when all the OP seems to be asking is about constructing objects.

Oh, he doesn't really want to dynamically create objects on the fly. He just wants to do a simple class instantiation. I understand it now, thx.
 
Back
Top Bottom