[英]Convert C# to VB.net - byte array loop
好的,在vb.net中有一些我需要的C#示例代碼。 通常這沒什么大不了的,但是我的大腦已經完全鎖定了While循環。 總體代碼使用XML字符串和文件執行PUT Web請求。 While循環正在讀取文件的字節並將其添加到Web請求中(我認為-再次將大腦鎖定了)。 和往常一樣,感謝您的任何幫助。
這是我在vb.net中需要的C#while循環
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
表達式中的賦值在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
我不相信您可以在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
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.