7 Replies - 3123 Views - Last Post: 18 August 2010 - 09:48 AM Rate Topic: -----

#1 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Why create objects in a partial class?

Posted 18 August 2010 - 09:38 AM

Why do you create objects in the partial class and not the main?

For example
namespace project1
{
  public partial class Form1: Form
  {
      Object newObject;
    
    
    public Form1()
    {
      InitializeComponent();
      newObject = new Object() {fields};
    }
  }
}



Why not

namespace project1
{
  public partial class Form1: Form
  {
    public Form1()
    {
      InitializeComponent();
      Object newObject = new Object() {fields};
    }
  }
}



and create and initialize/reference it in the main?

This post has been edited by D.Mulroy: 18 August 2010 - 09:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why create objects in a partial class?

#2 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:41 AM

Neither of those examples will compile. I think you've confused yourself somewhere, because you're putting a constructor outside the class, in the namespace, and that simply won't work.

edit: looks like you fixed it.

This post has been edited by insertAlias: 18 August 2010 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:42 AM

Quote

Why do you create objects in the partial class and not the main?


Huh? They are all parts of the same class. Hence 'partial'.

You can have 10 different files that are all parts of the same class. Its just for organization.

One file might have all the inventory objects, while another file has all the sale clerk objects.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:43 AM

In your second code snippet, newObject's scope is only for the constructor. Therefore, it couldn't be used in another method...say the Form_Load method.

Partial classes are used so that you can write code in multiple files for the same class. For example, when using LINQ-To-SQL, if you wanted to add something to the generated class, you create your own file and make it a partial class of the generated class. This is because when the generated class is regenerated, it won't delete all of your custom code.
Was This Post Helpful? 2
  • +
  • -

#5 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:44 AM

View PostinsertAlias, on 18 August 2010 - 08:41 AM, said:

Neither of those examples will compile. I think you've confused yourself somewhere, because you're putting a constructor outside the class, in the namespace, and that simply won't work.

edit: looks like you fixed it.


Edit: Looks like you saw my edit :P

This post has been edited by D.Mulroy: 18 August 2010 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:45 AM

public Elephant_Exercise() is not the "main class". It is the constructor of the class that it's defined in.

So essentially, the difference between those two samples you've provided is, the first makes "lucinda" and "Lloyd" available to the entire class. The second makes them available only to the constructor.

A constructor is just a special method. So declaring a variable inside a method means that when the method exits, the variable is gone. Declaring it at class level means that it can be used anywhere inside the class.

Read up on scope.

This post has been edited by insertAlias: 18 August 2010 - 09:47 AM

Was This Post Helpful? 2
  • +
  • -

#7 D.Mulroy   User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:47 AM

View Posteclipsed4utoo, on 18 August 2010 - 08:43 AM, said:

In your second code snippet, newObject's scope is only for the constructor. Therefore, it couldn't be used in another method...say the Form_Load method.

Partial classes are used so that you can write code in multiple files for the same class. For example, when using LINQ-To-SQL, if you wanted to add something to the generated class, you create your own file and make it a partial class of the generated class. This is because when the generated class is regenerated, it won't delete all of your custom code.



Thank you, cleared things up for me :]
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Why create objects in a partial class?

Posted 18 August 2010 - 09:48 AM

Yeah, we have a bit of an edit-snafu. I edit in response to your edit, and so forth :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1