Search code examples
c#arrayswinformswcf

How do I print a value of an array from WCF to form label?


WCF generates 6 random lottery numbers, it returns them in an array on the wcf service, however I can't get it to print to a label on the form.

if I try to print it to a message box I get "Value cannot be parsed as type int32"

I tried creating a new array on the form the logic being like, form array = service array, since its returning an array the service should be an array shouldnt it? with that I get Cannot implicitly convert type int to int[]

here's where im at:

IService

public interface ILottoService
{

    [OperationContract]

    int[] GenerateLottoDrawNums();

    [OperationContract]

    int[] GenerateIrishLottoNums();

}

Service

 public int[] GenerateLottoDrawNums()
    {

        int min = 1;
        int max = 59;


        int[] randomNums = new int[6];


        Random rand = new Random();


        for (int i = 0; i < randomNums.Length; i++)
        {

            int tempNum = rand.Next(min, max); 

            while (IsDuplicate(tempNum, randomNums))
            {
                tempNum = rand.Next(7);
            }
            randomNums[i] = tempNum;

        }


        return randomNums;

    }

    public Boolean IsDuplicate(int tempNum, int[]randomNums)
    {
        foreach (var item in randomNums)
        {
            if (item == tempNum)
            {
                return true;
            }
        }
        return false;
    }
}
}

Form

    public partial class FrontEnd : Form
{
    LottoServiceReference.LottoServiceClient ws = null;
    public FrontEnd()
    {
        InitializeComponent();
    }

    private void FrontEnd_Load(object sender, EventArgs e)
    {
        ws = new LottoServiceReference.LottoServiceClient();
    }




    private void btnLottoDraw_Click(object sender, EventArgs e)
    {
        try
        {
            int[] LottoDrawNums = new int[6];

            for (int i = 0; i < LottoDrawNums.Length; i++)
            {
                LottoDrawNums[i] = ws.GenerateLottoDrawNums();

                lblNum1.Text = String.Join(",", LottoDrawNums.ToString());

                MessageBox.Show(String.Join(",", ws.GenerateLottoDrawNums()));

                Console.WriteLine(String.Join(",", ws.GenerateLottoDrawNums()));
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }


    }

    }
}

}

Guessing I'm missing some [] or int[]?

My college tutor could not help, she referred me to you guys. Saying "It thinks its an int and it isn't. Try converting to String or List then printing that. She Googled and found a Stack Overflow question about converting but I didn't save it and can't find it at home.

Thank you.


Solution

  • this code

    for(int i = 0; i < LottoDrawNums.Length; i++)
    {
          lblNum1.Text = LottoDrawNums[0].ToString();
    }
    

    Set's only the first position of the array to the label.

    Try String.Join https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=netframework-4.7.2

    LblNum1.Text = String.Join(" , ", LottoDrawNums);
    

    This will return somethingh like "3 , 45 , 6 , 54 , 56 , 7, 45 "

    Also use it in MessageBox.Show(String.Join(" , ", ws.GenerateLottoDrawNums()));

    The GenerateLottoDrawNums and IsDuplicate methods work fine!