如何将值从foreach循环传递给另一个方法

时间:2012-11-22 13:35:54

标签: c# methods foreach unity3d

我需要做的是从一个名为private void ReceiveData()的方法中获取foreach值,并将它们发送到另一个方法void Update()。如何在c#中做到这一点?

private void ReceiveData() 
    {

       IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("10.0.2.217"), port);
       client = new UdpClient(remoteIP);
        while (true) 
        {
           try 
            {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                data = client.Receive(ref anyIP);

                int nIndex = 0;
                foreach(SignalIndex si in xmlreader.cdpSignals)
                {
                     x= ReadSingleBigEndian(data, si.index + 0);
                     y= ReadSingleBigEndian(data, si.index + 4);
                     z= ReadSingleBigEndian(data, si.index + 8);
                     alpha= ReadSingleBigEndian(data, si.index + 12);
                     theta= ReadSingleBigEndian(data, si.index + 16);
                     phi= ReadSingleBigEndian(data, si.index + 20);

                //  xmlreader.unityGameObjects[nIndex].transform.localPosition = new Vector3(x,y,z);
                //  xmlreader.unityGameObjects[nIndex].transform.Rotate(alpha,theta,phi);   
                }

            }
            catch (Exception err) 
            {
               print(err.ToString());
            }

        }

        client.Close();
    }


    void Update() 
    {
        foreach(GameObject go in xmlreader.unityGameObjects)
        {
            go.transform.localPosition = new Vector3(x,y,z);
            go.transform.transform.Rotate(alpha,theta,phi);
        }
    }

有两种方法,我需要从ReceiveData()获取foreach中的值,并在方法Update()中实例化它们。来自ReceiveData()的foreach中的所有变量都是公共的,但它们在Update()中都为0。

2 个答案:

答案 0 :(得分:0)

foreach的工作方式如下:

在你的方法(ReceiveData)中,你将循环使用某种方法。然后从该循环中调用另一个方法(Update)。但是,您必须传递这些变量。你的方法目前是无效的,也没有任何论据。

我建议如下:

private void ReceiveData()
{
    var data = GetMyDataFromSomewhere(); // data should be an IEnumerable of some sort

    foreach (MyDataItemType dataItem in data)
    {
        Update(dataItem);
    }
}

private void UpdateData(MyDataItemType dataItem)
{
    // Do some update to dataItem here...
}

使用更多代码进行新更新后,可以使用更多信息。显然,您在课程中设置了一些字段/属性(xyz等)。并且更新方法将使用这些来更新某些内容。

那么为什么不在Update()循环中调用foreach,或者在循环之后调用foreach(SignalIndex si in xmlreader.cdpSignals) { x= ReadSingleBigEndian(data, si.index + 0); y= ReadSingleBigEndian(data, si.index + 4); z= ReadSingleBigEndian(data, si.index + 8); alpha= ReadSingleBigEndian(data, si.index + 12); theta= ReadSingleBigEndian(data, si.index + 16); phi= ReadSingleBigEndian(data, si.index + 20); UpdateData(); // Just call it! } 如果只需要在设置之后完成:

{{1}}

答案 1 :(得分:0)

编辑2:

ReaderWriterLockSlim的文档:http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx

struct CoordData
{
    public float X, Y, Z, Alpha, Theta, Phi;
    public int NIndex;
}

private List<CoordData> coordDataList = new List<CoordData>();
private ReaderWriterLockSlim lockObj = new ReaderWriterLockSlim();

private void ReceiveData() 
{

   IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("10.0.2.217"), port);
   client = new UdpClient(remoteIP);
    while (true) 
    {
       try 
        {
            IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
            data = client.Receive(ref anyIP);

            int nIndex = 0;
            foreach(SignalIndex si in xmlreader.cdpSignals)
            {
                 x= ReadSingleBigEndian(data, si.index + 0);
                 y= ReadSingleBigEndian(data, si.index + 4);
                 z= ReadSingleBigEndian(data, si.index + 8);
                 alpha= ReadSingleBigEndian(data, si.index + 12);
                 theta= ReadSingleBigEndian(data, si.index + 16);
                 phi= ReadSingleBigEndian(data, si.index + 20);

                 lockObj.EnterWriteLock();
                 try
                 {
                     coordDataList.Add(new CoordData() { X = x, Y = y, Z = z, Alpha = alpha, Theta = theta, Phi = phi, NIndex = nIndex });
                 }
                 finally
                 {
                     lockObj.ExitWriteLock();
                 }

                 nIndex++; //I'm assuming you want to do this here otherwise you'll be changing the same object in the array every time  
            }

        }
        catch (Exception err) 
        {
           print(err.ToString());
        }

    }

    client.Close();
}

void Update()
{
    lockObj.EnterReadLock();
    try
    {
        foreach (CoordData data in coordDataList)
        {
            xmlReader.unityGameObjects[data.NIndex].localPosition = new Vector3(data.X,data.Y,data.Z);
            xmlReader.unityGameObjects[data.NIndex].Rotate(data.Alpha,data.Theta,data.Phi);
        }
    }
    finally
    {
        lockObj.ExitReadLock();
    }
}