c# - 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.
解决方案
这段代码
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 方法工作正常!
推荐阅读
- c# - IEnumerable 给出错误异常处理无法读取列表
- merge - 有没有办法在 yaml 文件中制作 kustomize 合并标题,而不是用覆盖 yaml 完全替换该标题
- c - 错误:表达式不可赋值三元运算符
- python - 如何快速不返回加盐密文,而是返回密文的未加盐版本?
- java - 如何在 java 中使用 jdbc 编写选择查询需要检查 COLUMN 是否为 NULL?
- sql-server - 按名称删除分区 sql server
- ios - 处理多个任务时如何跟踪进度和增加进度视图的计数?
- python - 使用 matplotlib 增加绘图和标题之间的空间
- azure-cognitive-search - Azure 搜索排序,如 localeCompare javascript 功能
- javascript - 如何将图像和数据添加到 Firebase