0

I am currently trying to understand the purpose of partial methods as, the way in which i was hoping to use them was like an event.

Example.cs

public partial class Example 
{
    partial void LoadData();
    public void Example() 
    {
        LoadData();
    }

    public override void ToString() => (StringA + StringB);
}

Example.Hello.cs

public partial class Example
{
    public string StringA;
    partial void LoadData() 
    {
        StringA = "HELLO";
    }
}

Example.World.cs

public partial class Example
{
    public string StringB;
    partial void LoadData() 
    {
        StringB = "WORLD";
    }
}

public void Main 
{
    public void Main () 
    {
        Example ex = new Example();
        Console.Write(ex.ToString());
    }
}

Using the example pysudeo code above, I expect the Console to say "HELLOWORLD". This is how I originally thought partial methods should work, I believe I understand why it doesn't work this way, but I was hoping someone could help me come up with an alternative way of accomplishing this.

Essentially I only want to call one method and have that one method be able to lead all the data that I need for my Example.cs object.

TO BE CLEAR, i get that the application won't know how to order the function calls. but I honestly do not care for the order in my example. It could say "WORLDHELLO" for all i care.

A.Mills
  • 357
  • 3
  • 16
  • 1
    Possible duplicate of [Splitting/Combining Partial Methods](https://stackoverflow.com/questions/2088265/splitting-combining-partial-methods) – Travis J Oct 25 '18 at 21:09
  • My understanding of what partial methods do is provide a slot in the function call table for the class that may or may not be filled in. If it's not filled in, and someone calls the method, nothing happens. If it is filled in, then the method is called. What you are trying to do is to fill in that one slot with two method implementations (I'm assuming you get an appropriate error when you try to do this). The purpose of partial methods is allow boilerplate code to possibly call optional non-boilerplate code (like `BeforeBigOperation` and `AfterBigOperation`) – Flydog57 Oct 25 '18 at 21:23
  • 1
    If you want it to function like an event, even though it doesn't function like an event at all, then *use an event*, which naturally functions like an event. – Servy Oct 25 '18 at 21:31
  • aye, I wasn't sure if event is what I wanted here. – A.Mills Oct 25 '18 at 21:37

0 Answers0