Zheng Huang
Zheng Huang

Reputation: 5

How to return a value inside of while loop

I have a method, return type is List<User>. In this method I have infinite while loop to accept information from another client over socket. Once a new client accepts, I will add this user into list and continue to listen for new client. I give the construction of the method. It is suppose to accept multiple client. Right now, only allow me to accept one client. Before, we did not set the type as List<User>, then there is no return UserList and whole code works fine with multiple user. After I add a return type of Method, it doesn't works.

public List<User> accept() {
    List<User> userList = new List<User>();
    while (true) {   
        Command_Listening_Socket = server.Accept();

        int msgLenght = Command_Listening_Socket.Receive(msgFromMobile);// receive the byte array from mobile, and store into msgFormMobile
        string msg = System.Text.Encoding.ASCII.GetString(msgFromMobile, 0, msgLenght);// convert into string type

        if (msg == "setup") {
            my_user = new User();
            userList.Add(my_user);
        }
        return userList;
    }
}

Upvotes: 0

Views: 6125

Answers (2)

Y.S
Y.S

Reputation: 1862

Just wrap it up

public void accept()
{
    List<User> users = new List<User>();

    while (true)
    {
        var user = _accept()

        if(user != null)
        {
            users.Add(user)
        }
    }
}

public User _accept()
{
        User my_user = null;

        Command_Listening_Socket = server.Accept();

        int msgLenght = Command_Listening_Socket.Receive(msgFromMobile);// receive the byte array from mobile, and store into msgFormMobile
        string msg = System.Text.Encoding.ASCII.GetString(msgFromMobile, 0, msgLenght);// convert into string type

        if (msg == "setup")
        {
            my_user = new User();
        }

        return my_user;
}

Upvotes: -1

farid bekran
farid bekran

Reputation: 2754

If you want to use exactly this solution you can use yield return instead of return statement.

But you will need to iterate over the result of Accept() method outside of it.

But it is good to use event based solution for this type of code structure.

    public class Program
    {
        public static IEnumerable<object> Accept()
        {
            var userList = new List<object>();
            var index = 0;
            while (true)
            {
                var msg = "setup";
                if (msg == "setup")
                {
                    var returnUser = new
                    {
                        Name = "in method " + index

                    };
                    Thread.Sleep(300);
                    yield return returnUser;
                }
                index++;
            }
        }

        private static void Main(string[] args)
        {
            foreach (var acc in Accept())
            {
                Console.WriteLine(acc.ToString());
            }
            Console.WriteLine("Press any key to continue.");
            Console.ReadLine();
        }
    }

Upvotes: 2

Related Questions