Question What Does : Dim Array() as String means and what Dim Array as String() Means?

AukI

Member
Joined
Nov 20, 2010
Messages
17
Programming Experience
3-5
What Does : Dim Array() as String means and what Dim Array as String() Means?

If I pass a array to function like below:

VB.NET:
Public Sub Array_AddSomething(ByVal strEntry As Object, ByRef strArray() As Object)
        Try
            Dim Upper As Integer
            If strArray IsNot Nothing AndAlso strArray.Length > 0 Then

                Upper = UBound(strArray)
                ReDim Preserve strArray(0 To Upper + 1)
                strArray(Upper + 1) = strEntry
            Else
                ReDim Preserve strArray(0)
                strArray(0) = strEntry
            End If
        Catch ex As Exception
            MsgBox("Please Contact with your vendor or developer. Some internal error from array passing.", MsgBoxStyle.Critical, "Array Value Adding")

        End Try
  

    End Sub

As I pass a array to this function and some thing , it should add to it... but it doesn't add unless the object is
VB.NET:
 Dim str() as object

but it should works as

VB.NET:
 Dim str() as string
or anything
 
What Does : Dim Array() as String means and what Dim Array as String() Means?
They mean exactly the same thing.

You need to make your method generic:
VB.NET:
Private Sub AddElement(Of T)(ByRef array As T(), ByVal element As T)
    System.Array.Resize(array, array.Length + 1)
    array(array.GetUpperBound(0)) = element
End Sub
Of course, if you were to use a collection instead of an array then you wouldn't have to worry about resizing. That's exactly why collections were created in the first place. You should be using a List(Of String) rather than a String().
 
Thanks a lot but can you give me more information what is method generic?

And why use

what does it means....


I saw few built in methods have the ability to put many more argument than its shows... like string replaces
"{0} this is my name {1}"

Moreover, can you give me an example how to use collection in these case...

thank you again for your help
 
Thanks a lot but can you give me more information what is method generic?

And why use


what does it means....
It means that it's generic, i.e. can be used for multiple types with the specific type being fixed when it's used. There is lots of information out there on .NET generics so you should do some reading on the subject. The most common use of a generic type or method would be the List(Of T) class, so you could start with that.
I saw few built in methods have the ability to put many more argument than its shows... like string replaces
That would be a ParamArray, which is another term you can research.
Moreover, can you give me an example how to use collection in these case...

thank you again for your help
Again, there's lots of information out there on collections in general and the List(Of T) class specifically. Read about them to get a proper understanding of how they work, not just an example of how you can write code for one specific situation.

If you can't find information or don't understand what you find, by all means post back and ask for clarification, but always do your own research first when you know what you're looking for. That's why I tell people what to look for but don't necessarily go into a lot of detail. The detail is generally already out there for those prepared to look.
 
Back
Top