none
An object reference is required for the non-static field, method, or property RRS feed

  • Question

  •         public void Memo4Change(Object Sender)
            {
    
                int i;
                int k;
                bool b;
                IsClientPacket = true;
                StringBuilder sb = new StringBuilder(Temp);
                b = false;
                for (k = 0; k <= richTextBox1.Lines.Count() - 1; k++)
                {
                    Temp = richTextBox1.Lines[k];
                    for (i = 1; i <= Temp.Length; i++)
                    {
                        if (!(new ArrayList(new char[] { '0', 'a', 'A', ' ' }).Contains(Temp[i])))
                        {
                            sb[i] = ' ';
                            b = true;
                        }
                    }
                    if (b)
                    {
                        
                        richTextBox1.Lines[k] = Temp;
                    }
                }
                TPacketViewFrame.ParsePacket("This", IsClientPacket);
            }
    

    And in another class.cs

           public void ParsePacket(string pkt, bool isClientPacket)
            {
                if (pkt is null)
                {
                    throw new System.ArgumentNullException(nameof(pkt));
                }
    
                TPacketReader Reader;
                int i;
                byte ID1;
                byte ID2;
                string PacketName;
                string PacketStuctFullStr;
                TReadType ReadType;
                string StructName;
                // pkt types
                long aInt64Q;
                int aCardinalD;
                ushort aWordH;
                byte aByteC;
                double aDoubleF;
                string aStringS;
                itemTag = 2;
                t3.Items.Clear();
                t.Items.Clear();
    
                Reader = new TPacketReader(pkt);
                try
                {
                    uGlobalFunctionsUnit.GetPacketID(pkt, out ID1, out ID2);
                    if ((ID1 == 0xD0) || (ID1 == 0xFE))
                    {
                        if (isClientPacket)
                        {
                            PacketStuctFullStr = READ_VALUE("client", ID1.ToString("x") + ID2.ToString("x"), FileLocation);
                        }
                        else
                        {
                            PacketStuctFullStr = READ_VALUE("server", ID1.ToString("x") + ID2.ToString("x"), FileLocation);
                        }
                    }
                    else 
                    {
                        if (isClientPacket)
                        {
                            PacketStuctFullStr = READ_VALUE("client", ID1.ToString("x"), "FileLocation");
                        }
                        else
                        {
                            PacketStuctFullStr = READ_VALUE("server", ID1.ToString("x"), FileLocation);
                        }
                    }
                    PacketName = uGlobalFunctionsUnit.GetPacketName(PacketStuctFullStr);
                    if (PacketName == "")
                    {
                        PacketName = "Unknown" + ID1.ToString("x");
                    }
                    // packet ID
                    if (!Reader.isValidC())
                    {
                        return;
                    }
                    aByteC = Reader.ReadC();
                    AddPacketType(PacketName, aByteC);
    
                    uGlobalFunctionsUnit.ExtractPacketStruct(PacketStuctFullStr);
                    if (uGlobalFunctionsUnit.CurPktStuct.Length == 0)
                    {
                        //AddRestHex(Reader.ToHex());
                    }
                    else
                    {
                        i = 0;
                        while (i < uGlobalFunctionsUnit.CurPktStuct.Length)
                        {
                            if (uGlobalFunctionsUnit.CurPktStuct.Length == 0)
                            {
                                return;
                            }
                            if (uGlobalFunctionsUnit.pktIsLoop(uGlobalFunctionsUnit.CurPktStuct[i]))
                            {
                            }
                            else
                            {
                                uGlobalFunctionsUnit.GetReadType(uGlobalFunctionsUnit.CurPktStuct[i], out ReadType);
                                StructName = uGlobalFunctionsUnit.GetPacketStructName(uGlobalFunctionsUnit.CurPktStuct[i]);
                                if ((ReadType == ((TReadType)(uGlobalFunctions.TReadType.d))))
                                {
                                    aCardinalD = Reader.ReadD();
                                    AddPacketTypeD(StructName, aCardinalD);
                                }
                                else if ((ReadType == ((TReadType)(uGlobalFunctions.TReadType.h))))
                                {
                                    aWordH = Reader.ReadH();
                                    AddPacketTypeH(StructName, aWordH);
                                }
                                else if ((ReadType == ((TReadType)(uGlobalFunctions.TReadType.q))))
                                {
                                    aInt64Q = Reader.ReadQ();
                                    AddPacketTypeQ(StructName, aInt64Q);
                                }
                                else if ((ReadType == ((TReadType)(uGlobalFunctions.TReadType.s))))
                                {
                                    if (Reader.isValidS())
                                    {
                                        aStringS = Reader.ReadS();
                                        AddPacketTypeS(StructName, aStringS);
                                    }
                                }
                                else if ((ReadType == ((TReadType)(uGlobalFunctions.TReadType.c))))
                                {
                                    if (Reader.isValidC())
                                    {
                                        aByteC = Reader.ReadC();
                                        AddPacketTypeC(StructName, aByteC);
                                    }
                                }
                                i++;
                            }
                        }
                    }
                }
                finally
                {
                }
            }

    An object reference is required for the non-static field, method, or property 'TPacketViewFrame.ParsePacket(string, bool)'

    TIA

    Tuesday, December 31, 2019 10:15 AM

Answers

  • Hello,

    The error indicates you need to create a new instance of the class to access ParsePacket.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 31, 2019 10:37 AM
    Moderator

All replies

  • Hello,

    The error indicates you need to create a new instance of the class to access ParsePacket.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 31, 2019 10:37 AM
    Moderator
  • Do i put it in the same class.cs where ParsePacket is located or where im calling it from?

    Edit: never mind, got it

    tyvm

    • Edited by caveman404 Tuesday, December 31, 2019 11:10 AM
    Tuesday, December 31, 2019 11:07 AM
  • Do i put it in the same class.cs where ParsePacket is located or where im calling it from?

    Edit: never mind, got it

    tyvm

    Determining where it goes is called scope. Delare in one class, pass it via a method to another class would be the first thing to do rather than declaring it publically. Bottom line is I don't know the logic of your code so the best option is to narrow the scope rather than broaden the scope.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, December 31, 2019 11:43 AM
    Moderator
  • Hi MigrationUser 1, 

    Thank you for posting here.

    Call a non-static method from a static method will cause this error.

    We usually set ‘non-static’ element ‘static’ or create a new instance of class to solve this problem.

    Besides, Could you provide some related code about ‘TPacketViewFrame’ or a complete sample here? It will help us to reproduce your problem.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 1, 2020 7:08 AM
    Moderator
  • Hi MigrationUser 1,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 7, 2020 6:47 AM
    Moderator