an object reference is required for the nonstatic field method or property

10/30/2011 15:19 abdeen#1
hello guys , i am getting this error in :

PHP Code:
public static void WaitForWinner() 
and here is a pic of the error.

[Only registered and activated users can see links. Click Here To Register...]

and here is my class which have the 2 error's

PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Conquer_Online_Server.Game;
using Conquer_Online_Server.Network.GamePackets;
using Conquer_Online_Server.Network.Features.ClassPKWar;

namespace 
Conquer_Online_Server
{
    public 
enum BroadCastLoc
    
{
        
World,
        
Map
    
}
    public 
enum ElgahedStage
    
{
        
None,
        
Inviting,
        
Countdown,
        
Fighting,
        
Over
    
}
    
    public static class 
Elgahed
    
{
        public static 
ushort Map;
        public static 
ushort XY;
        public static 
ElgahedStage Stage ElgahedStage.None;
        public static 
Dictionary<uintClient.GameStatePKTHash2;
        public static 
Dictionary<uintClient.GameStatePKTHash;
        public static 
int CountDown;
        private static 
Thread PkThread;
        public 
Client.GameState pOnepTwo;
        public static 
Map dynamicMap;
        public 
void RandomSelect()
        {
            List<
Client.GameStateplayers = new List<Client.GameState>();
            foreach (
Client.GameState p in ServerBase.Kernel.GamePool.Values)
                
players.Add(p);
            
Random rand = new Random();
            
int selectedIndex rand.Next(players.Count);
            
pOne players[selectedIndex];
            
players.RemoveAt(selectedIndex);
            
selectedIndex rand.Next(players.Count);
            
pTwo players[selectedIndex];
            
players.RemoveAt(selectedIndex);
            if (
pOne.Map.ID == 1631 && pOne.Map.ID == pTwo.Map.ID)
                if (!
ServerBase.Kernel.Maps.ContainsKey(700))
                    new 
Map(700Database.DMaps.MapPaths[700]);
            
Map origMap ServerBase.Kernel.Maps[700];
            
dynamicMap origMap.MakeDynamicMap();
            
pOne.Entity.Teleport(origMap.IDdynamicMap.ID, (ushort)ServerBase.Kernel.Random.Next(3570), (ushort)ServerBase.Kernel.Random.Next(3570));
            
pTwo.Entity.Teleport(origMap.IDdynamicMap.ID, (ushort)ServerBase.Kernel.Random.Next(3570), (ushort)ServerBase.Kernel.Random.Next(3570));
            if (
pOne.Map.ID == pTwo.Map.ID)
            {
                
pOne.Entity.RemoveFlag(Network.GamePackets.Update.Flags.Ride);
                
pTwo.Entity.RemoveFlag(Network.GamePackets.Update.Flags.Ride);
                
pTwo.Entity.PKMode pOne.Entity.PKMode;
                
pOne.Entity.PKMode Conquer_Online_Server.Game.Enums.PKMode.PK;
                
pOne.Send(new Data(true) { UID pOne.Entity.UIDID Data.ChangePKModedwParam = (uint)pOne.Entity.PKMode });
                
pOne.Entity.PKMode pTwo.Entity.PKMode;
                
pTwo.Entity.PKMode Conquer_Online_Server.Game.Enums.PKMode.PK;
                
pTwo.Send(new Data(true) { UID pTwo.Entity.UIDID Data.ChangePKModedwParam = (uint)pTwo.Entity.PKMode });
                
Conquer_Online_Server.Network.PacketHandler.WorldMessage("" pOne.Entity.Name " Is Meeting " pTwo.Entity.Name " in the Elite Pk Tournament");
                
Client.GameState[] Clients Conquer_Online_Server.ServerBase.Kernel.GamePool.Values.ToArray();
                while (
CountDown 0)
                {
                    if (
CountDown == 0)
                        
Broadcast("60 seconds until start"BroadCastLoc.World);
                    else if (
CountDown == 0)
                    {
                        
Stage ElgahedStage.Countdown;
                        
//if (PKTHash.Count < 2)
                        //{
                        //    Broadcast("The tournament requires atleast 2 people to start, Tournament Cancelled.", BroadCastLoc.World);
                        //    Stage = ElgahedStage.None;
                        //    PKTHash = null;
                        //    return;
                        //}
                        
Broadcast("10 seconds until start"BroadCastLoc.World);
                    }
                    else if (
CountDown 0)
                        
Broadcast(CountDown " seconds until start"BroadCastLoc.World);

                    
CountDown--;
                    
Thread.Sleep(1000);
                }
                
Stage ElgahedStage.Fighting;
                
Broadcast("Fight!"BroadCastLoc.World);
                
WaitForWinner();
            }
        }
        public static 
void StartTournament(Client.GameState Started)
        {
            
PKTHash = new Dictionary<uintClient.GameState>();
            
CountDown 0;
            
Stage ElgahedStage.Inviting;
            
Map 1507;
            
100;
            
100;
            
PkThread = new Thread(new ThreadStart(BeginTournament));
            
PkThread.IsBackground true;
            
PkThread.Start();
        }
        public static 
void StartTournament()
        {
            
PKTHash = new Dictionary<uintClient.GameState>();
            
CountDown 0;
            
Stage ElgahedStage.Inviting;
            
Map 1507;
            
100;
            
100;

            
PkThread = new Thread(new ThreadStart(BeginTournament));
            
PkThread.IsBackground true;
            
PkThread.Start();
        }
        private static 
void Broadcast(string msgBroadCastLoc loc)
        {
            
//Console.WriteLine(msg);
            
if (loc == BroadCastLoc.World)
            {
                foreach (
Client.GameState Char in ServerBase.Kernel.GamePool.Values)
                {
                    
Char.Send(new Message(msgSystem.Drawing.Color.White2011));
                    
// Char.MyClient.EndSend();
                
}
            }
            else if (
loc == BroadCastLoc.Map)
            {
                foreach (
Client.GameState Char in PKTHash.Values)
                {
                    
Char.Send(new Message(msgSystem.Drawing.Color.White2011));
                    
// Char.EndSend();
                
}
            }
        }
        private static 
void AwardWinner(Client.GameState Winner)
        {
            
Broadcast(Winner.Entity.Name " has won the tournament !He WIn 10.000Cps and WeeklyPKChampion Top"BroadCastLoc.World);
            
Winner.Entity.WeeklyPKChampion += 1;
            
Winner.Entity.ConquerPoints += 10000;
            
Elgahed.Stage ElgahedStage.None;
            
PkThread.Abort();
            return;
        }
        public static 
void WaitForWinner()
        {
            
Elgahed.Stage ElgahedStage.Fighting;
            
uint Tick = (uint)Environment.TickCount;
            
int InMapAlive PKTHash.Count;
            while (
true)
            {
                
int alive 0;
                foreach (
Conquer_Online_Server.Client.GameState players in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values)
                    if (
pOne.Entity.Map.ID == pOne.Entity.Map.ID && (!players.Entity.Dead))
                        
alive++;

                foreach (
Client.GameState _GC in PKTHash.Values)
                {
                    if (
_GC.InPKT)
                        if (
_GC.Entity.Dead == true)
                        {
                        }
                        else if (!
ServerBase.Kernel.GamePool.ContainsKey(_GC.Entity.UID))
                        {
                            
InMapAlive--;
                            
_GC.Entity.Teleport(1002438382);
                            
_GC.InPKT false;
                        }
                }
                
System.Threading.Thread.Sleep(2000);
                foreach (
Client.GameState _GC in PKTHash.Values)
                {
                    if (
_GC.InPKT)
                        if (
alive == 1)
                    {
                        
_GC.Entity.Teleport(1002438382);
                        
AwardWinner(_GC);
                        
Stage ElgahedStage.Over;
                        return;
                    }
                }
                if (
InMapAlive != 1)
                {
                    
Broadcast("There are " InMapAlive " Alive "BroadCastLoc.Map);

                }
                
Thread.Sleep(1000);
            }
        }
        public static 
void BeginTournament()
        {
            
Client.GameState[] client Conquer_Online_Server.ServerBase.Kernel.GamePool.Values.ToArray();
            foreach (
Client.GameState clientss in client)
            {
                
Network.GamePackets.NpcReply npc = new Network.GamePackets.NpcReply(6"The WeeklyPK Champion Tournament has Started! You Wana Join?");
                
npc.OptionID 248;
                
clientss.Send(npc.ToArray());
            }

            
Stage ElgahedStage.Inviting;
            while (
CountDown 0)
            {
                if (
CountDown == 0)
                    
Broadcast("60 seconds until start"BroadCastLoc.World);
                else if (
CountDown == 0)
                {
                    
Stage ElgahedStage.Countdown;
                    
//if (PKTHash.Count < 2)
                    //{
                    //    Broadcast("The tournament requires atleast 2 people to start, Tournament Cancelled.", BroadCastLoc.World);
                    //    Stage = ElgahedStage.None;
                    //    PKTHash = null;
                    //    return;
                    //}
                    
Broadcast("10 seconds until start"BroadCastLoc.World);
                }
                else if (
CountDown 0)
                    
Broadcast(CountDown " seconds until start"BroadCastLoc.World);

                
CountDown--;
                
Thread.Sleep(1000);
            }
            
Stage ElgahedStage.Fighting;
            
Broadcast("Fight!"BroadCastLoc.World);
            
WaitForWinner();
        }
    }


and i don't get this error in :

PHP Code:
public void RandomSelect() 
so why i get it in :

PHP Code:
public static void WaitForWinner() 
10/30/2011 16:16 Korvacs#2
public Client.GameState pOne, pTwo;

These are never defined when they are used in that method.
10/30/2011 16:20 abdeen#3
Quote:
Originally Posted by Korvacs View Post
public Client.GameState pOne, pTwo;

These are never defined when they are used in that method.
look , i added :

PHP Code:
foreach (Conquer_Online_Server.Client.GameState pOne in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values)
                        foreach (
Conquer_Online_Server.Client.GameState pTwo in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values
and its looks like this :

PHP Code:
        public static void WaitForWinner()
        {
            
Elgahed.Stage ElgahedStage.Fighting;
            
uint Tick = (uint)Environment.TickCount;
            
int InMapAlive PKTHash.Count;
            while (
true)
            {
                
int alive 0;
                foreach (
Conquer_Online_Server.Client.GameState players in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values)
                    foreach (
Conquer_Online_Server.Client.GameState pOne in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values)
                        foreach (
Conquer_Online_Server.Client.GameState pTwo in Conquer_Online_Server.ServerBase.Kernel.GamePool.Values)
                    if (
pTwo.Entity.MapID == pOne.Entity.MapID && (!players.Entity.Dead))
                        
alive++;

                foreach (
Client.GameState _GC in PKTHash.Values)
                {
                    if (
_GC.InPKT)
                        if (
_GC.Entity.Dead == true)
                        {
                        }
                        else if (!
ServerBase.Kernel.GamePool.ContainsKey(_GC.Entity.UID))
                        {
                            
InMapAlive--;
                            
_GC.Entity.Teleport(1002438382);
                            
_GC.InPKT false;
                        }
                } 
but i got another 3 errors here at pOne , pTwo and RandomSelect :

PHP Code:
        public Client.GameState pOnepTwo;
        public 
void RandomSelect() 
the error says :

Code:
cannot declare instance members in a static class
10/30/2011 18:55 Drunk|Guy#4
Do not declare instance members in a static class .
10/31/2011 00:55 pro4never#5
.... you need to learn what static means.


I'm not sure if I can accurately describe object oriented programming in a sentence or two but lets just use a possibly less accurate but easier to understand version of it..

When you say something is static then you're saying that there can be only one of it.


Example...

static class FormMember
{
//Class variables/methods/etc
}

This means there can only EVER be one form member... You never need to use the 'new' statement to create a new instance of an object because well... you can't.

You use this when obviously there cannot be two of something. Examples would be methods which let you perform an action on input generally such as something like...


static void SetName(FormMember user, string name)
{
user.Name = name;
}

*Note* Doing something like this would be pointless but it should help you understand what I'm talking about.


Now you have to take into account what the reverse of this means... If you do NOT state that something is static then there can be as many objects of that type as you want and each one must be initiated at some point...


Example..


class FormMember
{
//Class variables/methods/etc
}

FormMember Pro4Never = new FormMember();


In this case I'm saying that I want to create a new instance of the class FormMember and assign it to the variable I'm calling Pro4Never.

Often times your classes will take parameters in the constructors you write for them. This doesn't change how we deal with the objects, simply the information they hold and require before they are initialized.

Eg:
class FormMember
{
public string Name;

public FormMember(string _name)
{
Name = _name;
}
}

FormMember Pro4Never = FormMember("Pro4Never");



In your example you're using a loop (which... looks really... really awkward/wrong to me...) to try to check for winners.

HELPFUL ADVICE: Learn how loops work... in your example players, pOne and pTwo are the loop values... you are not assigning them manually, they are just telling you what loop number you are on.



Screw it.. here's some code that will be more accurate and help you understand what you SHOULD be doing here....


public class TournamentMatch
{
public TournamentMatch(Player _one, Player _two)
{
PlayerOne = _one;
PlayerTwo = _two;
PlayerOne.Match = this;
PlayerTwo.Match = this;
}
public Player PlayerOne, PlayerTwo;
//Any extra information such as time the match started and the player's score!

//Any extra methods such as RoundStart, RoundEnd, CheckForWinner
}

In your player class you'd need to allow it to hold a TournamentMatch value. By default it would be null obviously if they are not in a match (null out on round end)


In your thread which is checking for winner (I told you already that's a poor way of doing this in the other 3 threads you made on this topic) you'd simply do something like...

foreach (Conquer_Online_Server.Client.GameState player in Conquer_Online_Server.ServerBase.Kernel.GamePool.V alues)
if(player.Match != null)
{
//Or w/e you want to call it when you check if both people are alive/on same map/etcetc to deal with round end conditions
player.Match.CheckMatchStatus();
}

DONE
10/31/2011 06:10 Lateralus#6
@ pro4never - Dude, major kudos to you and Korvacs. You both have mad patience.
10/31/2011 14:34 F i n c h i#7
Best & respected moderator is pro4never (Chris).
10/31/2011 17:37 abdeen#8
pro4never , You are the best , Thanks for your help i`ll try it :)