2017-04-18 56 views
0

好的,請在vb.net中使用C#中的一些示例代碼。通常不是什麼大問題,但我的大腦已經完全鎖定在這個While循環中。整體代碼使用XML字符串和文件進行PUT Web請求。 While循環正在讀取文件的字節並添加到Web請求中(我認爲 - 再次鎖定大腦)。像往常一樣感謝任何幫助提前。將C#轉換爲VB.net - 字節數組循環

這裏是C#while循環,我需要在vb.net

while ((bytesRead = postData.Read(buffer, 0, buffer.Length)) != 0) 
{ 
requestStream.Write(buffer, 0, bytesRead); 
} 

這裏是我目前在vb.net(這是錯誤的)

While (bytesRead, postData.Read(buffer, 0, buffer.Length)) <> 0 
      requestStream.Write(buffer, 0, bytesRead) 
     End While 
+0

作爲第一步,您可以隨時嘗試Telerik的代碼轉換器:http://converter.telerik.com/ – Sorceri

回答

0

內部的分配表達式在vb.net中不起作用。這實際上需要更多的代碼:

bytesRead = postData.Read(buffer, 0, buffer.Length) 
While bytesRead <> 0  
    requestStream.Write(buffer, 0, bytesRead) 
    bytesRead = postData.Read(buffer, 0, buffer.Length) 
End While 
0

我不相信While條件語句中預期可以執行任務的Visual Basic使用的=運營商都平等的比較和分配。這是C#的好處之一,它可以輕鬆區分兩者(因爲它們是不同的運營商)。

相反,嘗試開始閱讀它的循環之外第一個呼叫,然後在循環中處理它隨後所有的電話:

' Initial read ' 
bytesRead = postData.Read(buffer, 0, buffer.Length) 
' Read if there are bytes to be read ' 
While bytesRead <> 0  
    ' Write the current bytes out of the buffer ' 
    requestStream.Write(buffer, 0, bytesRead) 
    ' Read some more ' 
    bytesRead = postData.Read(buffer, 0, buffer.Length) 
End While 
+0

感謝您的幫助,可能會工作。 – Brettvd

0

C#像其他C衍生語言中允許使用分配的作爲表達。 VB不允許這樣做。您可以明確地完成分配(如其他答案中所做的那樣),也可以將其轉換爲函數。

由於後者的一個例子:

Function ReadBytes(<insert correct declaration for buffer here>, ByRef bytesRead as Integer) As Boolean 
    bytesRead = postData.Read(buffer, 0, buffer.Length) 
    Return bytesRead <> 0 
End Function 

While(ReadBytes(buffer, bytesRead)) 
    requestStream.Write(buffer, 0, bytesRead) 
End While 

取決於其他細節,你沒有顯示,則可能還需要postData是一個參數ReadBytes