Link to home
Start Free TrialLog in
Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on

VB.NET - Cannot initialize Byte() array

I'm trying to do an HTTP web request with the code shown below...

But I'm getting an error saying: Type 'Byte' has no constructors.

What do I need to do to make this work?

Private Sub BuildReportHTML()
        Dim sb As StringBuilder = New StringBuilder
        Dim buf As Byte() = New Byte(8192)
        Dim count As Integer = 0
        Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.google.com"), HttpWebRequest)
        Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
        Dim strm As Stream = response.GetResponseStream
 
        Do
            count = strm.Read(buf, 0, buf.Length)
            If (count <> 0) Then
                sb.Append(Encoding.ASCII.GetString(buf, 0, count))
            End If
        Loop While count > 0
 
        Dim strTemp As String = sb.ToString
 
    End Sub

Open in new window

Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

Byte is a base type. You can't create instances of it.

Dim buf As Byte = New Byte(8192) is wrong.

Dim buf As Byte = (Byte)8192

or

Dim buf As Byte() = Byte.Parse("8192")

May be closer to what you need.



Sorry! I'm a C# guy and see I misread your code.

If I was creating a byte array it'd be like this in C#;

Byte[] array = new Byte[8192];
Pretty sure you need square brackets in VB.NET too!

Avatar of josephdaviskcrm

ASKER

Sorry... using square brackets isn't doing the trick.
Surely changed the message you get?!

What's the new message and code?

Dim buf As Byte() = New Byte[8192]

Identifier Expected
The error underline is under the 8192 inside the square brackets.
ASKER CERTIFIED SOLUTION
Avatar of Jamie McAllister
Jamie McAllister
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is not correct syntax in VB.

I found a different way to produce the desired result on my web appliation that has nothing to do with the http web request.  So I'm dropping the questionl.