我正在使用MultiSpeak API,对此我并不熟悉。功能如下:
public meter[] GetMeterByAccountNumber(string accountNumber) {
meter myMeter = new meter();//IS this declaration right?
//some query work and next is sql data reader
int i = 0;
while (rdr.Read())
{
myMeter[i].deviceClass = rdr["deviceClass"].ToString();//error: Cannot apply indexing with [] to type 'meter'
i++;
}
return myMeter[]; //generates ERROR: Value expected.
}
我不知道这种返回类型'GetMeterByAccountNumber
'是什么,但是它确实期望返回meter[]
数组。
答案 0 :(得分:2)
GetMeterByAccountNumber
不是返回类型,它是函数名称。
您可以做这样的事情,但是我将其称为 GetMetersByAccountNumber ,因为它返回一个数组/ IEnumerable
我也不知道deviceClass与帐号有关。
using System.Linq;
using System.Collections.Generic
public IEnumerable<meter> GetMetersByAccountNumber(string accountNumber) {
var items = new List<meter>();
//some query work and next is sql data reader
while (rdr.Read())
{
var deviceClass = rdr["deviceClass"].ToString();
var meter = new meter();
//Im guessing meter has some properties to set ?
meter.deviceClass = deviceClass;
items.Add(meter);
}
return items.AsReadOnly();
}
答案 1 :(得分:0)
根据@Richard Friend的回答,这对我有用。谢谢理查德!
public meter[] GetMetersByAccountNumber(string accountNumber)
{
meter[] final_return;
var items = new List<meter>();
while (rdr.Read())
{
var meter = new meter();
meter.deviceClass = rdr["deviceClass"].ToString();
items.Add(meter);
}
final_return = items.ToArray();
return final_return;
}