首页 > 解决方案 > 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.

标签: c#arrayswinformswcf

解决方案


这段代码

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

仅将数组的第一个位置设置为标签。

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

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

这将返回类似 "3 , 45 , 6 , 54 , 56 , 7, 45 "

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

GenerateLottoDrawNums 和 IsDuplicate 方法工作正常!


推荐阅读