Dyanamic array declarations
-
HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana
-
HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana
I believe your problem is you haven't created your Names array yet. So I think your code should be something like: Dim Names() As String Sub New(ByVal st As List(Of String)) if Names is nothing then Names = new String(st.Length) end if st.CopyTo(Names) <-- value cannot be null error End Sub Anyway, hopefully you get the idea. Ben
-
I believe your problem is you haven't created your Names array yet. So I think your code should be something like: Dim Names() As String Sub New(ByVal st As List(Of String)) if Names is nothing then Names = new String(st.Length) end if st.CopyTo(Names) <-- value cannot be null error End Sub Anyway, hopefully you get the idea. Ben
Thanks Ben, But it is giving RunTime Error.Becuse these method (Names = new String(st.Length)) is not supported in Vb.net Dana
-
Thanks Ben, But it is giving RunTime Error.Becuse these method (Names = new String(st.Length)) is not supported in Vb.net Dana
-
HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana
It doesn't work because the Names array doesn't have any elements in it. If you delcared the array as:
Dim Names(10) As String
it would contain 11 elements of type String, all with the value of String.Empty. The way you've done it, there are no elements in the array. You'd have to ReDim the array to the proper size, then call CopyTo
Sub New(ByVal st As List(Of String)) ReDim Names(st.Count - 1) st.CopyTo(Names) End Sub
But, I highly recommend changing the array to a List.
Private \_names As List(Of String) Public Sub New(ByVal st As List(Of String)) \_names.AddRange(st) End Sub
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
It doesn't work because the Names array doesn't have any elements in it. If you delcared the array as:
Dim Names(10) As String
it would contain 11 elements of type String, all with the value of String.Empty. The way you've done it, there are no elements in the array. You'd have to ReDim the array to the proper size, then call CopyTo
Sub New(ByVal st As List(Of String)) ReDim Names(st.Count - 1) st.CopyTo(Names) End Sub
But, I highly recommend changing the array to a List.
Private \_names As List(Of String) Public Sub New(ByVal st As List(Of String)) \_names.AddRange(st) End Sub
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks Dave..... It worked well Dana