0

so I am trying to make a loop in which I create a new instance of an object every time it loops: something like this.

Client client = new Client(session);
client.Connect(server);

while (true) {
    client.SendPacket(new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));

    client.Disconnect ("Generic");

    System.Threading.Thread.Sleep(3000);

    //obviously I cannot do this, because there is already an object named client.
    Client client = new Client(session);

    client.Connect(server);
}

How would I go about doing this? I need to make a NEW instance of MinecraftClient, I cannot reuse the old one after I do client.Disconnect.

3 Answers 3

4

The simplest thing that works is to change

Client client = new Client(session);

to

client = new Client(session);

That said, I think there's a cleaner approach. Start with:

while(true) {
    Client client = new Client(session);
    client.Connect(server);
    client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
    client.Disconnect ("Generic");
    System.Threading.Thread.Sleep(3000);
}

Now factor that out into a method:

private void ConnectAndDoStuff() {
    Client client = new Client(session);
    client.Connect(server);
    client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
    client.Disconnect ("Generic");
    System.Threading.Thread.Sleep(3000); 
}

And then:

while(true) { this.ConnectAndDoStuff() }
Sign up to request clarification or add additional context in comments.

11 Comments

+1, but of course you need a way to break out of the while loop, which I presume is being handled separately?
I'm not sure that factoring out a 5-line method (that presumably has one caller) is ipso facto an improvement.
@KirkWoll Actually, it's a great improvement. The number of lines are irrelevant, I'd probably split it even more -> One method for connecting and one for DoingStuff
@DimitarDimitrov, I don't think splitting functionality because of the ability to name discrete ideas necessarily outweighs the cost of obscuring the implementation of the logic.
@KirkWoll Not sure exactly what you mean, but if anything encapsulating the implementation in a method (preferably smaller methods) increases the readability and maintainability of the code. That's just in principle, not necessary in this particular scenario. Also I should include a big "IMHO" somewhere there.
|
1

Create the instance in the loop every time you start again.

while (true) {
    Client client = new Client(session);
    client.Connect(server);
    client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
    client.Disconnect ("Generic");

    System.Threading.Thread.Sleep(3000);
}

Comments

0

If you just want to recreate the client object just remove the Type definition

    Client client = new Client(session);
    client.Connect(server);
    while (true)
    {
        client.SendPacket (new Craft.Net.CreativeInventoryActionPacket(-1, new Craft.Net.ItemStack((short)19, (sbyte)1)));
        client.Disconnect ("Generic");
        System.Threading.Thread.Sleep(3000);
        client = new Client(session); 
        client.Connect(server);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.