locked
Type byte has no constructors RRS feed

  • Question

  • Why I am I getting the "type byte has no constructors" error here?

                Dim stream As System.IO.MemoryStream = New MemoryStream()
    
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
                stream.Position = 0
                Dim image As Byte() = New Byte(stream.Length)

     


    Brett The Jet
    Thursday, January 5, 2012 10:10 PM

Answers

  • The compiler is not reading 'New Byte(stream.Length)' as a declaration of an array with a size, but as a call to the Byte constructor with an argument, hence the message.  The correct form for what you are trying to do is

      Dim image(Stream.Length) As Byte

    The 'New Byte' form is used when the array elements are provided. For instance:

      Dim image As Byte() = New Byte() {1, 2, 3, 4, 5}

    • Proposed as answer by Blackwood Friday, January 6, 2012 12:31 AM
    • Marked as answer by Mark Liu-lxf Tuesday, January 17, 2012 6:45 AM
    Friday, January 6, 2012 12:07 AM
  • Brett,

    I updated a sample on our website and now I see today 3 questions where it can be used.

    First your actual question. It is as it is, the Byte has now constructor in its structure.

    A Constructor is simply in VB a Method which looks like Sub New
                                                                                           End Sub

    It runs when the object is constructed. 

    Be aware you can construct a new array of bytes, but that is not a new byte but an array which holds items from the type Byte.

    For that are many possibilities. However almost impossible to do by hand with an image, you need for that to stream the bytes of an image. 

    However for what you are doing I've updated the sample today in a more todays way.

    http://www.vb-tips.com/image2bytearray.aspx


    Success
    Cor




    • Marked as answer by brmcdani44 Thursday, January 5, 2012 10:34 PM
    • Unmarked as answer by brmcdani44 Thursday, January 5, 2012 10:57 PM
    • Edited by Cor Ligthert Friday, January 6, 2012 7:44 AM
    • Marked as answer by Mark Liu-lxf Tuesday, January 17, 2012 6:45 AM
    Thursday, January 5, 2012 10:23 PM

All replies

  • Brett,

    I updated a sample on our website and now I see today 3 questions where it can be used.

    First your actual question. It is as it is, the Byte has now constructor in its structure.

    A Constructor is simply in VB a Method which looks like Sub New
                                                                                           End Sub

    It runs when the object is constructed. 

    Be aware you can construct a new array of bytes, but that is not a new byte but an array which holds items from the type Byte.

    For that are many possibilities. However almost impossible to do by hand with an image, you need for that to stream the bytes of an image. 

    However for what you are doing I've updated the sample today in a more todays way.

    http://www.vb-tips.com/image2bytearray.aspx


    Success
    Cor




    • Marked as answer by brmcdani44 Thursday, January 5, 2012 10:34 PM
    • Unmarked as answer by brmcdani44 Thursday, January 5, 2012 10:57 PM
    • Edited by Cor Ligthert Friday, January 6, 2012 7:44 AM
    • Marked as answer by Mark Liu-lxf Tuesday, January 17, 2012 6:45 AM
    Thursday, January 5, 2012 10:23 PM
  • The compiler is not reading 'New Byte(stream.Length)' as a declaration of an array with a size, but as a call to the Byte constructor with an argument, hence the message.  The correct form for what you are trying to do is

      Dim image(Stream.Length) As Byte

    The 'New Byte' form is used when the array elements are provided. For instance:

      Dim image As Byte() = New Byte() {1, 2, 3, 4, 5}

    • Proposed as answer by Blackwood Friday, January 6, 2012 12:31 AM
    • Marked as answer by Mark Liu-lxf Tuesday, January 17, 2012 6:45 AM
    Friday, January 6, 2012 12:07 AM
  • Brett,

    Yesterday eve I realized me that I gave you a wrong reply. 

    I've changed it now.

     


    Success
    Cor
    Friday, January 6, 2012 6:10 AM