Hi everyone, I have asked something similar to this before.

I understand how to use objects and what they do etc...

What I am struggling to get my head around is WHAT an object actually is. I keep reading and reading but do not seem to be able to get my head around that last piece of what an object actually is!

My understanding of an object now:

When an object of a class is made, that object can then have access to that classes methods and properties?

Can anyone give me a nice simple explanation of this or point me to an exact definition of an object..Thanks

I think that the main thing I am struggling with is the new keyword!!

James

Hi, If I have a simple Form...

there is a textbox and a button...

If the text in the textbox is "James", when the button is clicked, is there a way to create an object called James(or whatever text is in the textbox) so that then other information about James can be added, height, weight etc.

I suppose another way I could explain it is...when a company has a order system, when they type in the customers name, they can enter all the other attributes belonging to the customer and bring them up at a later date..

Is this getting into using databases?

Yes, you'll need some form of datastore so you can recall the information later.

>What I am struggling to get my head around is WHAT an object actually is

You can say - An object is a collection of bytes or partitioned area of memory.
Some systems (Garbage collection for .net app) allocates areas of storage in the heap. These areas of storage define "Objects".

Read - Memory and Object Management, an article from - The C/C++ Users Journal.

An object is the kay to open or access anything in the form or in the code. Whereas in the form , if you have used textbox, than the id will be a objec (textbox) access key form which you can access the textbox.

In the class or c# class, the object will be as below
> Form objects
> Local Variables
> Parent Class , and parent class accessible objects
> Local, Global Declared ADO Controls, which will be used as needs


Regards,
jay

In general, object is a thing which you can realize, which you can identify, which you can able to call with a specific name.
Ex: Book, Mobile, Vehicle, Water, Human etc...

Lets consider that a bundle of pages are there in a table. That is neatly arranged by putting 2 pins at the left end. Now, if you see that thing - your mind will tell you that "It is a BOOK".

Think the same to other examples also(how we identify that "It is a Mobile" and so on).

This is what you can say the real world object.

If you are coming to technical, the concept of object is same but you need be aware of what type of object it is?

Ex: Book b = new Book();
Here 'b' is an object of type Book.
More specifically, 'b' is a reference type variable which points to an instance of type Book.

Thanks for all the answers everybody!!

So an object could be a integer with a value, it could be a string variable it could be a whole class??

And when you create an object of type whatever this does not hold the value of the object but it holds a reference to the object, and this object is on the heap while it still has instances that point to it?

Thanks for all the answers everybody!!

So an object could be a integer with a value, it could be a string variable it could be a whole class??

And when you create an object of type whatever this does not hold the value of the object but it holds a reference to the object, and this object is on the heap while it still has instances that point to it?

Yes and no. C# has two different types: Value types and Reference types. What you have said is true for (most) reference types. Value types are generally stored on the stack (which is faster) and hold the actual value (thus the name) rather than a reference to an object. Of course, sometimes the system 'boxes' value types when it needs to treat them as objects.

So some examples of value types might be...

bool myBool = true;

int myInt = 10;

this is because they do not create an object and are put on to the stack while they are needed. The stack is constantly changing in size as and when variables are needed and not needed.

But a reference type is one that uses the new keyword to create a new object that is stored on the heap. The object then holds a reference to the object that is on the heap.

I am thinking that variables are objects, methods are variables, properties are objects etc...I think this is confusing me the most...

Could anyone clarify if the above are actually classed as objects or do you only class the objects as instances you create with the new keyword>?

I hope that makes sense :-p

Objects have variables, properties and methods.

Variables can be objects or values
Properties can refer to objects or values.
Methods are not objects, but you can have objects that reference methods (events and delegates).

Again, your last statement is almost true :) There are other methods that create objects other than using new to instantiate them in the .NET libraries.

Thanks Momerath...

What would be an example of a object variable?

int myInt = new int();

Is this an object variable?

No, int is a value type.

Form f = new MyForm();

There is an object :)

One way to tell if something is going to be an object is to ask yourself "Can it be set to null?" Value types can't be set to null (though there are nullable value types which are objects that act like value types).

Variables can be objects or values

I am confused by what you say...so even though "f" is an object..it is still a variable?

Yes, it's still a variable.

Variables are things that can hold different values (like the 'f' in the example).
Constants are things that can't change (the number 5, a string "This is a test").

We won't get into the debate whether myConst is a variable or a constant in this snippet const int myConst = 5; One test you can use is "Can it be on the left side of an equal sign?" If the answer is yes, then it is a variable (Properties are a special case as they are methods to access variables).

Ok thanks...so what would my earlier example be?

int myInt = new int();

myInt is a variable of type Int32.
Because Int32 is a value type, it is stored on the stack.
It is not an object because it can't be set to null.

Right, so if it cannot be set to null, it is not an object?

Generally, as there are specific objects in the C# language than can't be set to null.

Maybe this will help:

  • Value Types
    • Numeric
      • Signed Integers
        • sbyte
        • short
        • int
        • long
      • Unsigned Integers
        • byte
        • ushort
        • uint
        • ulong
      • Real Number
        • float
        • double
        • decimal
    • Logical - bool
    • Character - char
  • Reference Types
    • String
    • Object

These are the types built into the C# language itself. The .NET library defines many more Value (DateTime for example) and Reference (Form for example) types.

You can create your own Value types by using the struct keyword. You can create your own Reference types by using the class keyword.

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.