Question about class

Question about class

Page 1 of 1

13 Replies - 920 Views - Last Post: 03 December 2010 - 03:56 PM Rate Topic: -----

#1 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Question about class

Posted 03 December 2010 - 06:43 AM

Hi : )

I'm new in C# and I want to ask something about class and objects.

For example:

public class example  // class example



In other class:
example a;  //at this point, what is A ?
a = new example(); //what is A here, object ?



Thank you and have a nice day :)

This post has been edited by Vac: 03 December 2010 - 06:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about class

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Question about class

Posted 03 December 2010 - 06:46 AM

public class example  // class example

In other class:

example a;  //at this point, what is A ?
a = new example(); //what is A here, object ?



You are actually CREATING the new example object on your last line of code.
Was This Post Helpful? 0
  • +
  • -

#3 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: Question about class

Posted 03 December 2010 - 06:51 AM

Would you like to explain a bit more these two lines?

example a;  //what is example, what is a ?
a = new example(); //what is A here, object ?


Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Question about class

Posted 03 December 2010 - 06:53 AM

example a;  //Not instantiated yet. It's null.
a = new example();   //Finally instantiated and it has an object.


Was This Post Helpful? 1
  • +
  • -

#5 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: Question about class

Posted 03 December 2010 - 06:58 AM

what about this one ?

int a;
a = new int(); 


Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Question about class

Posted 03 December 2010 - 07:06 AM

The same. In C# EVERYTHING is an Object. EVERYTHING.
Was This Post Helpful? 1
  • +
  • -

#7 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: Question about class

Posted 03 December 2010 - 07:10 AM

I thought so, but I was not sure. Thank you a lot buddy.
One more little question, and that's all ...

Why do we use constructors ? Can you give me some practical example ?
Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Question about class

Posted 03 December 2010 - 07:12 AM

Without getting into the technical details, you want to use constructors because in there you can do anything you want to an object the MINUTE it's created.

For example if you have a class called Book and you want to give it a name every time you create a new book, you'd have:

public class Book
{
    public string Title { get; set; }

    public Book(string title)
    {
        Title = title;
    }
}

//Then somewhere else outside this class:
Book harryPotter = new Book("Harry Potter");

Was This Post Helpful? 2
  • +
  • -

#9 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: Question about class

Posted 03 December 2010 - 07:19 AM

I see, now the things are clear. Thank you one more time.
Was This Post Helpful? 0
  • +
  • -

#10 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Question about class

Posted 03 December 2010 - 07:22 AM

Constructors are to init variables, likes classes or assign values.

public MyClassContructor(string text, int number) {
  Class2 myClass2 = new myClass2(text);
  int a = number;
}



But it's not the only way to do that :)

This post has been edited by mavarazo: 03 December 2010 - 07:23 AM

Was This Post Helpful? 1
  • +
  • -

#11 Vac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 10

Re: Question about class

Posted 03 December 2010 - 07:36 AM

@mavarazo please see this

  int a;
  a = new int(); 
  a = 5; // a assume value



Is it possible to make class that assume value, and if it is, how the class will look ?

  example b;  
  b = new example();
  b = 50;


Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Question about class

Posted 03 December 2010 - 08:37 AM

ints, along with chars, longs, doubles, strings, etc... are a special case. They're called "primitives" in other languages. In .NET, they're still classes, but they are representable by literal values. 5 is an int. 5.5 is a double. "Hello World" is a string. In fact, you'd almost never see a "new int()" ever, because you can initialize it with a literal.

But how do you literally represent an "Example" or a "Book" or a "MyClass3"? You can't. So there's no way to give these literal values. But you can define the constructor to take initial values, or define properties as public so you can set the things that make up a class.

Also, it's quite obvious you're very new to programming. I suggest you find yourself a good book on C#, or Object Oriented Programming. It'll answer these questions better and quicker than we can. This is a technical help forum, not a class lecture. We can't teach you C# from the ground up.

This post has been edited by insertAlias: 03 December 2010 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Question about class

Posted 03 December 2010 - 11:25 AM

Why would you do this??
example b;  
b = new example();
b = 50;



It's the same as this:
Car lamborgini;
lamborgini = new Car();
lamborgini = fa_18_hornet;



This will never work in life :). There will be a way, but if you know that, this will not help you getting the understand for programming.

Plz read a book to get some basics :) google --> c# for beginners
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,468
  • Joined: 29-May 08

Re: Question about class

Posted 03 December 2010 - 03:56 PM

Everything in.net derive from the base object Object
The next layer up are Structure and Class

Structure
treated a value, and such always has a value (ie it is never null)
Examples: Int, Double
struct MyStruct {
 int id;
 int count;
}



Class treated as a reference to where the actual data is stores.
That pointer could be to the null object, which indicate it has not got a reference.
Examples: String, MyClass
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1