J
JDeats
So I have a class that spans over two partial classes in code, here's
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).
// Inside SharedClassExample1.cs
public partial class SharedClassExample
{
public List<string> BooksOnShelf { get; set; }
public List<string> BooksOnDesk { get; set; }
// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}
public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}
}
// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}
}
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?
an example (do not read much into this, the code is of no practical
use, this is just a simple example of where my confusion occurs).
// Inside SharedClassExample1.cs
public partial class SharedClassExample
{
public List<string> BooksOnShelf { get; set; }
public List<string> BooksOnDesk { get; set; }
// constructor
public SharedClassExample()
{
BooksOnShelf = new List<string>();
BooksOnDesk = new List<string>();
}
public void SortBooksOnShelf()
{
BooksOnShelf.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnShef.Remove(bookTitle);
}
}
// inside SharedClassExample2.cs
public partial class SharedClassExample
{
public void SortBooksOnDesk()
{
BooksOnDesk.Sort();
}
// why isn't this possible?
partial void RemoveBooksFromShelfAndDesk(string bookTitle)
{
BooksOnDesk.Remove(bookTitle);
}
}
The above example generates a compile time error, after some research
on-line it appears partial methods do not work as I original
understood. Instead a "partial method" is simply defined in one file
(In context, "file" means one of the two files in which this class
lives, in two parts) and the implementation is provided in the other
file.
Some my question is this: If the implementation of the partial method
can not span across files, then what is the point? For example, in C#
2.0 with partial classes I could make a call SortBooksOnDesk from
inside SharedClassExample1.cs so why do I need this new mechanism of
seperating the implementation from the declaration?