0

Basically what I want is to divide a class into multiple classes. I've tried using the partial function but it seems that I can't get it to work. What I want to do is to make multiple classes for the 100+ Regions under case Action.Answer: as you can see:

namespace My.Network
{
public unsafe class MsgDialog : Msg
{
    public const Int16 Id = _MSG_DIALOG;
    public enum Action
    {
         //Actions used
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct MsgInfo
    {
         //MsgInfo
    }
    public static Byte[] Create(Int32 TaskId, Int16 Param, Byte IdxTask, Action Action, String Text)
    {
         //act
    }
    public static void Process(Client Client, Byte[] Buffer)
    {
        try
        {
            if (Client == null || Buffer == null || Client.User == null)
                return;

            Int16 MsgLength = (Int16)((Buffer[0x01] << 8) + Buffer[0x00]);
            Int16 MsgId = (Int16)((Buffer[0x03] << 8) + Buffer[0x02]);
            Int32 TaskId = (Int32)((Buffer[0x07] << 24) + (Buffer[0x06] << 16) + (Buffer[0x05] << 8) + Buffer[0x04]);
            Int16 Param = (Int16)((Buffer[0x09] << 8) + Buffer[0x08]);
            Byte IdxTask = (Byte)Buffer[0x0A];
            Action Action = (Action)Buffer[0x0B];
            String Text = Program.Encoding.GetString(Buffer, 0x0E, Buffer[0x0D]);

            Player Player = Client.User;

            switch (Action)
            {
                case Action.Answer:
                    {
                        100 + Regions
                    }
            }
        }
        catch (Exception Exc) { Program.WriteLine(Exc); }
    }
}
}

As requested by @Enigmativity here follows an example of one of those regions:

case 945: 
    {
        Position += ScriptHandler.SendText("Your inventory is full!", Client, ref Data, Position);
        Position += ScriptHandler.SendOption(255, "I see.", Client, ref Data, Position);
        Position += ScriptHandler.SendFace(102, Client, ref Data, Position);
        Position += ScriptHandler.SendEnd(Client, ref Data, Position);
        ScriptHandler.SendData(Client, Data, Position);
        break;
    }
Noobzilla
  • 97
  • 1
  • 1
  • 6
  • 1
    If you have 100+ regions in your class it's not partial classes but serious refactoring that you need – dotnetom Dec 21 '15 at 05:21
  • You need to show us what the code in the `100 + Regions` actually is. Perhaps a typical subset would suffice. – Enigmativity Dec 21 '15 at 05:53
  • @Enigmativity I just added it to the main post – Noobzilla Dec 21 '15 at 06:11
  • @Noobzilla - That's good, but for us to see a pattern we would need to see more than one. I agree that one is a subset of all, but I kind of meant a substantive subset. But also, we'd need to see the types for `Position`, `ScriptHandler`, etc. You really need a [mcve]. – Enigmativity Dec 21 '15 at 07:27

1 Answers1

2

If you have 100 #regions then probably you don't need to split it to partial classes. Do some REFACTORING! You should extract all the regions as methods. If you are using Visual Studio, you just need to select the code and right click. Go to Refactor -> Extract -> Method and you can give it a meaningful name after that.

Now, you can move all these methods to another utility class or utility classes.

Then your code will look like this:

XXXX ();
XXXX ();
  .
  .
  .
XXXX ();

Where XXXX are all abstract names of methods. When people read your code and they don't want to know that much detail of the method, they can just read the method call abstractly.

Sweeper
  • 213,210
  • 22
  • 193
  • 313