Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.NET question

VB.NET question

Scheduled Pinned Locked Moved Visual Basic
questioncsharp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blue Bird 0
    wrote on last edited by
    #1

    In C# I can do the following: int nSize = 24; object[] pObjArray = new object[nSize]; How do I do the same in VB.NET? Thanks.

    W J J D 4 Replies Last reply
    0
    • B Blue Bird 0

      In C# I can do the following: int nSize = 24; object[] pObjArray = new object[nSize]; How do I do the same in VB.NET? Thanks.

      W Offline
      W Offline
      Wooster2006
      wrote on last edited by
      #2

      Integer nSize = 24 Object() pObjArray = new Object() {nSize}

      J 1 Reply Last reply
      0
      • B Blue Bird 0

        In C# I can do the following: int nSize = 24; object[] pObjArray = new object[nSize]; How do I do the same in VB.NET? Thanks.

        J Offline
        J Offline
        j on
        wrote on last edited by
        #3

        Dim nSize As Integer = 24 Dim pObjArray(nSize) As Object '// And to test it: pObjArray(24) = "Test" MsgBox(CType(pObjArray(24), String)) There you go! ;) -- modified at 11:38 Tuesday 23rd May, 2006

        J 1 Reply Last reply
        0
        • W Wooster2006

          Integer nSize = 24 Object() pObjArray = new Object() {nSize}

          J Offline
          J Offline
          Joshua Quick
          wrote on last edited by
          #4

          Wooster2006 wrote:

          Integer nSize = 24 Object() pObjArray = new Object() {nSize}

          This is not correct. This creates an array containing 1 element whose value is 24.

          1 Reply Last reply
          0
          • J j on

            Dim nSize As Integer = 24 Dim pObjArray(nSize) As Object '// And to test it: pObjArray(24) = "Test" MsgBox(CType(pObjArray(24), String)) There you go! ;) -- modified at 11:38 Tuesday 23rd May, 2006

            J Offline
            J Offline
            Joshua Quick
            wrote on last edited by
            #5

            j-on wrote:

            Dim nSize As Integer = 24 Dim pObjArray(nSize) As Object

            Not quite right. This array contains 25 elements. Not 24. In VB.NET, the value that you use to set the array size does not represent the "Capacity" of the array like how it's done in C#. It represents the max "Index" of the array. And remember that arrays in VB.NET are zero based. So, if you count the elements from 0 to 24, you have a total of 25 elements. What you want to do is this instead. Dim nSize As Integer = 24 Dim pObjArray(nSize - 1) As Object This is one of the major difference between C# and VB.NET's handling of arrays. -- modified at 18:51 Tuesday 23rd May, 2006

            1 Reply Last reply
            0
            • B Blue Bird 0

              In C# I can do the following: int nSize = 24; object[] pObjArray = new object[nSize]; How do I do the same in VB.NET? Thanks.

              J Offline
              J Offline
              Joshua Quick
              wrote on last edited by
              #6

              Hello Blue Bird. The first 2 responses that you received were not quite correct. Please look at my responses to them. The following C# code...

              int nSize = 24;
              object[] pObjArray = new object[nSize];

              Will look like this in VB.NET...

              Dim nSize As Integer = 24
              Dim pObjArray(nSize - 1) As Object

              The big difference here is that the array initializer in VB.NET represents the largest index (ie: upper bound) and not the capacity of the array. That's why you should tack on a "-1" as shown above. You can also generate an array like this via the "ReDim" statement. Dim pObjArray() As Object ' Array not created yet. ReDim pObjArray(nSize - 1) ' This create a new array. ReDim pObjArray(1) ' This creates a new array having 2 elements. You can also create an array and initialize it at the same time like this... Dim pObjArray() As Object = {1, 2, 3} Or like this... Dim pObjArray() As Object pObjArray = New Object() {1, 2, 3} I hope this helps! :)

              1 Reply Last reply
              0
              • B Blue Bird 0

                In C# I can do the following: int nSize = 24; object[] pObjArray = new object[nSize]; How do I do the same in VB.NET? Thanks.

                D Offline
                D Offline
                Dave Doknjas
                wrote on last edited by
                #7

                Our Instant VB C# to VB.NET converter produces: Dim nSize As Integer = 24 Dim pObjArray As Object() = New Object(nSize - 1){} David Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C# to C++ converter and VB to C++ converter Instant J#: VB to J# converter Clear VB: Cleans up VB.NET code Clear C#: Cleans up C# code

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups