Breaking up the code in a file using 'partial' classes

Trying it out, seems quirky, what am I doing wrong?

Page 1 of 1

1 Replies - 3115 Views - Last Post: 17 August 2010 - 11:46 AM Rate Topic: -----

#1 Choscura   User is offline

  • D.I.C Lover


Reputation: 470
  • View blog
  • Posts: 2,235
  • Joined: 18-October 08

Breaking up the code in a file using 'partial' classes

Posted 17 August 2010 - 11:30 AM

I'm experimenting with breaking my code into smaller (bite-size) chunks lately and a trip to MSDN told me about the possibility of using 'partial' classes, and so I thought I would give it a go. I created a new code file, put in the file headers, put in the namespace of the project, and put in a 'partial' class of the same name- into which I put a (single) void function, currently empty. the whole thing looks something like this:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace library_reader
{
public partial class Form1
{

void Redefine_List(string selected_list)
{}

}

}



Everything seemed to be working fine, that is, there weren't any syntax errors- but visual studio subsequently crashed. When I loaded it back up, I got an error that looks like this:

Quote

Error 1 The item "obj\x86\Debug\library_reader.Form1.resources" was specified more than once in the "Resources" parameter. Duplicate items are not supported by the "Resources" parameter.


I opened up the resource designer (which is what it's pointing to right?) and right there at the top is a little bit that says

Quote

// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>


This is about as far as I've gotten trying to fix this, a quick look through the resource generator file hasn't turned up any clues- I don't see anything being called twice and I'm not familiar with most of the calls being made (microsoft.performance? what?) I can post the contents of the whole file if anybody thinks it's necessary, but I'm kind of stumped about what to do now.

Thanks in advance for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: Breaking up the code in a file using 'partial' classes

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: Breaking up the code in a file using 'partial' classes

Posted 17 August 2010 - 11:46 AM

I don't recommend breaking up a Form class like that. And the sample you read and listed doesn't do it either.


Let's say you make a class for dealing with the serial port.
You want to have a couple files to physically break up code that logically is still all the same class.
// This file has all the properties
namespace Choscura
{
   namespace IO
   {
      public partial class Serial
      {
         public List<string> ValidSerialPorts
         {
              get
              {
                // get the list of ports, stick 'em in a list then return them
              }
         }
      }
   }
}

This will give you the property: Choscura.IO.Serial.ValidPorts

// This file has all the Methods
namespace Choscura
{
   namespace IO
   {
      public partial class Serial
      {
         public string ClosePort(string NameOfComPort)
         {
            string ErrMsg = string.empty;
            // close the port.  Put error in ErrMsg
            return ErrMsg;
         }
      }
   }
}

This will give you the method: Choscura.IO.Serial.ClosePort()

More commonly I break up things like custom event args to one file, sub-classes get their own file and so on. But I don't generally break up properties and methods.


projectname.classname
EventArguments.cs
UserObj.cs
TransactionObj.cs
ProductObj.cs

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1