• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1455
  • Last Modified:

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

0
josephdaviskcrm
Asked:
josephdaviskcrm
  • 5
  • 3
1 Solution
 
Jamie McAllister MVPSharePoint ConsultantCommented:
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.



0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
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];
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
Pretty sure you need square brackets in VB.NET too!

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
josephdaviskcrmAuthor Commented:
Sorry... using square brackets isn't doing the trick.
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
Surely changed the message you get?!

What's the new message and code?

0
 
josephdaviskcrmAuthor Commented:
Dim buf As Byte() = New Byte[8192]

Identifier Expected
The error underline is under the 8192 inside the square brackets.
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
Shouldn't it be this?;

Dim buf As Byte[] = New Byte[8192]
0
 
josephdaviskcrmAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now