Reputation: 1
So ive got a base class which requires a Socket:
class Sock
{
public Socket s;
public Sock(Socket s)
{
this.s = s;
}
public virtual void Process(byte[] data) { }
...
}
then ive got another class. if a new socket gets accepted a new instance of this class will be created:
class Game : Sock
{
public Random Random = new Random();
public Timerr Timers;
public Test Test;
public Game(Socket s) : base(s) { }
public static void ReceiveNewSocket(object s)
{
Game Client = new Game((Socket)s);
Client.Start();
}
public override void Process(byte[] buf)
{
Timers = new Timerr(s);
Test = new Test(s);
Test.T();
}
}
in the Sock class ive got a virtual function that gets overwritten by the Game class.(Process function) in this function im calling a function from the Test Class(Test+ Timerr Class:
class Test : Game
{
public Test(Socket s) : base(s) { }
public void T()
{
Console.WriteLine(Random.Next(0, 10));
Timers.Start();
}
}
class Timerr : Game
{
public Timerr(Socket s) : base(s) { }
public void Start()
{
Console.WriteLine("test");
}
}
So in the Process function im calling a function in Test. And in this function(T) i need to call a function from the Timerr Class.But the problem is its always NULL , although the constructor is called in Process. And e.g. the Random Class can be called, i guess its because its defined with the constructor.:
public Random Random = new Random();
and thats why the other classes(without a constructor):
public Timerr Timers;
public Test Test;
are always null in the inherited class Test.But its essentiel that i call other Methods of other classes in this function.How could i solve that?
Upvotes: 0
Views: 595
Reputation: 1
oh i totally forgot that i could just one big partial class. sorry guys, sometimes i just dont think about some things
Upvotes: 0
Reputation: 942267
You are getting in trouble because your object design is wrong. You've got two choices to use existing classes: inheritance and encapsulation. To choose between them, you can use the is-a and has-a test. Pick the right choice from these tests:
You picked 2 and 3, you should have picked 1 and 4. The rest of your classes are wrong the same way too. Once you make these corrections, you'll find it a lot easier going.
Upvotes: 3
Reputation: 2522
I am sure that there is a valid reason for this complex inheritance, but why on earth would you have this many levels of inheritance? Does that really make sense in your solution? I would advise you to reconsider this and instead create a proper base class (Sock) and inherit just from that class and then use these classes when you need it.
Upvotes: 0
Reputation: 23788
Why do you need Test
to be a subclass of Game
? You have null
because Test
object created in Process
of Game
is different instance than the Game
object. Probably what you need is
class Test
{
Game Game;
public Test(Game g)
{
Game = g;
}
public void T()
{
Console.WriteLine(Random.Next(0, 10));
Game.Timers.Start();
}
}
and
public override void Process(byte[] buf)
{
Timers = new Timerr(s);
Test = new Test(this);
Test.T();
}
However it is hard to guess what you are actually doing and give you a good advice.
Upvotes: 6
Reputation: 3215
You have Test
, which inherits from Game
, that has a field called Timers
. In the Game class, you initialize Timers for that Game object as part of your Process method. But you also initialize a separate Test object. Now, Test, by inheritting from Game, has its own Timers field, but this isn't the same as the Timers in the Game that created the Test. This is why the Timers in your Test is always null, because that one is never initialized. You only initialized the Timers in the Game that also created the Test.
Upvotes: 2
Reputation: 46148
Timers
and Test
are only assigned by the Process
method override on Game
. You don't seem to be calling this method anywhere, therefore Timers
and Test
will always be null.
Upvotes: 0