开发者

Packet does not exist in current context

开发者 https://www.devze.com 2023-02-24 16:44 出处:网络
After asking my first question and reading some links provided I decided to just and port the communication layer from the open source vb.net project I used for a few months to C#. I started off good.

After asking my first question and reading some links provided I decided to just and port the communication layer from the open source vb.net project I used for a few months to C#. I started off good. Now I do have a problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Eclipse_5._0开发者_JAVA技巧
{
    class PacketHandler
    {
        #region Packet Constructors
        private delegate void PacketDelegate(int Index, byte[] Data);
        private PacketDelegate[] Packet;

        public PacketHandler()
        {
            Packet(Enumerations.ClientPackets.CAddChar) = HandleAddChar;
        }
        #endregion

        #region Packet Methods
        public void Handledata(int Index, byte[] Data, int MsgType)
        {
            ByteBuffer Buff = new ByteBuffer();
            Buff.WriteBytes(Data);
            if (MsgType < 0)
            {
                return;
            }
            if (MsgType >= (int)Enumerations.ClientPackets.CQuit)
            {
                return;
            }
            Packet(MsgType).Invoke(Index, Buff.ReadBytes(Buff.Length()));
            Buff.Dispose();
        }

        public void HandleNewAccount(int Index, byte[] Data)
        {
            //TODO: Add New Player Account File.
        }

        public void HandleAddChar(int Index, byte[] Data)
        {
            //TODO: Add New Character to Player Account File.
        }
        #endregion
    }
}

The following line has the error

Packet(Enumerations.ClientPackets.CAddChar) = HandleAddChar;

Any help would be great.


Overall, looks like quite a few problems with this code, but if you can post the error details then it might help us help you along a little sooner. Lets looks at maybe getting you passed this hurdle:

Packet is an array, so it looks like you want access by index like this:

Packet[Enumerations.ClientPackets.CAddChar] = HandleAddChar;

But an array is a reference type, and so needs to be instantiated - meaning even with the above in place you'll get a NullReferenceException. Furthermore, HandleAddChar is a method and requires arguments as per the parameters as part of its definition; and what you're intending to do is not what you would be expressing even with passing the appropriate values. So:

public PacketHandler()
{
    Packet = new PacketDelegate[1];        
    Packet[0] = new PacketDelegate(HandleAddChar);
}

Note that I've removed use of Enumerations.ClientPackets.CAddChar as it is meaningless in this context, the idea is that the array must be instantiated and to an appropriate capacity. Lastly, you don't want to call HandleAddChar here directly, we need a delegate reference, so that's what we create and insert into the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号