Solved C# Need help to write cleaner code.

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

velreine

Newbie
Full Member
Aug 27, 2012
36
504
0
Hi, i have a project in C#.

The basic idea is to be able to run a master, and another program as slaves,
having the master control the slaves.

It can send packets and control the slaves from the master, the program is right now very simple with basic functionality.
I don't have any problems making the program work, my problem is i want to make sure the code is clean and more object-oriented.

Here's an example of some code i would write, making a textbox act as a command-prompt.

C++:
private void HandleCommand(string cmd)
        {
            textBox3.Text = "";

            if (cmd.Length <= 0) { return; }

            string[] line = cmd.Split(' ');

            switch (line[0])
            {
                case "text":
                    textBox2.Text += "\r\nTEST!!!";
                    break;
                case "send": // Enter send function..
                    HandleSend(line);
                    break;

                default:
                    // Unknown command
                    textBox2.Text += "\r\n[!]Unknown command: " + line[0] +"[!]";
                    break;
            }

            textBox2.Text += "\r\n# ";
        }
C++:
        private void HandleSend(string[] input)
        {
            
            if(input.Length < 2) // Make sure all arguments are accounted for.
            {
                textBox2.Text += "\r\n---- command: send ----";
                textBox2.Text += "\r\n>>Usage: send [type]";
                textBox2.Text += "\r\nTypes:";
                textBox2.Text += "\r\npacket";
                textBox2.Text += "\r\nfile";
                textBox2.Text += "\r\n>>Example: send packet";

                return;
            }


            switch (input[1]) // First argument should be type to send.
            {
                case "packet":
                
                if(input.Length < 5)
                    {
                        textBox2.Text += "\r\nSend packet takes 3 arguments";
                        textBox2.Text += "\r\nExample: send packet 8 100 192.168.1.3";
                        return;
                    }

                    int iOP = Convert.ToInt32(input[2]);

                    OpcodeList op = (OpcodeList)iOP;
                    byte[] data = Encoding.ASCII.GetBytes(input[3]); //This might not work.
                    string ip = input[4];

                    SendPacket(op, data, ip);
                    // SendPacket(OpcodeList.SMSG_SET_VOLUME, BitConverter.GetBytes((Int32)100), "127.0.0.1");
                    break;


                case "file":
                    //NYI
                    break;
                default:

                    textBox2.Text += "\r\nUnknown type??";
                    break;
            }


        }
But I'm sure there's cleaner ways of making a fuck ton of switches, calling functions inside them.

Any tips/help or pointers are very much appreciated.

I can upload the entire project and the client version as the source code is in no way secret or anything.

Thanks in advance,

Velreine.
 

Boboo99

Scrub
Dank Tier VIP
Fleep Tier Donator
Feb 20, 2016
469
12,178
44
C#:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ShellWrapper
{
    public delegate void FunctionPrototype(params object[] parameters);
    
    
    internal class Command
    {
        public string Name;
        public FunctionPrototype FunctionToExecute;
        
        public Command(string name,FunctionPrototype functionPrototype)
        {
            Name = name;
            FunctionToExecute = functionPrototype;
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            List<Command> _cmdList = new List<Command>();
            _cmdList.Add(new Command("Hello",(parameters => Console.WriteLine(parameters[0]))));
            
            string input = Console.ReadLine();
            _cmdList.First(cmd => cmd.Name == input.Split((' '))[0]).FunctionToExecute(input.Split(' ')[1]);
        }
    }
}
This should give you an idea how to do it ^^
 
Last edited:
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods