Answered by:
VB2010 - how use an array on ParamArray?

-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
Question
Answers
-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
A ParamArray is always considered optional, so that can trip you up sometime:
https://msdn.microsoft.com/en-us/library/ct363x9h.aspx
I'd suggest that you specify the type - not "As Object', and to use it, test that the .Length > 0.
Beyond that, I'm not sure what you're asking.
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
-
imagine creating a sub with unlimited parameters of any type and accept arrays
You can - an ArrayList is an example - but that's really not the panacea you think it is.
You have to keep up with what's what. If you don't know, then don't expect that it will.
For what it's worth...
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
A ParamArray is always considered optional, so that can trip you up sometime:
https://msdn.microsoft.com/en-us/library/ct363x9h.aspx
I'd suggest that you specify the type - not "As Object', and to use it, test that the .Length > 0.
Beyond that, I'm not sure what you're asking.
"A problem well stated is a problem half solved.” - Charles F. Kettering
let change it a bit:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s())'see here the diference
error message:
"Number of indices is less than the number of dimensions of the indexed array. "
Try this instead:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder For Each st As String In s sb.AppendLine(st) Next Stop
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
All replies
-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
A ParamArray is always considered optional, so that can trip you up sometime:
https://msdn.microsoft.com/en-us/library/ct363x9h.aspx
I'd suggest that you specify the type - not "As Object', and to use it, test that the .Length > 0.
Beyond that, I'm not sure what you're asking.
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
A ParamArray is always considered optional, so that can trip you up sometime:
https://msdn.microsoft.com/en-us/library/ct363x9h.aspx
I'd suggest that you specify the type - not "As Object', and to use it, test that the .Length > 0.
Beyond that, I'm not sure what you're asking.
"A problem well stated is a problem half solved.” - Charles F. Kettering
let change it a bit:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s())'see here the diference
error message:
"Number of indices is less than the number of dimensions of the indexed array. "
-
-
imagine creating a sub with unlimited parameters of any type and accept arrays
You can - an ArrayList is an example - but that's really not the panacea you think it is.
You have to keep up with what's what. If you don't know, then don't expect that it will.
For what it's worth...
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
-
i did, for learn more a function for print on debug:
Private Sub DebugPrint(ByVal ParamArray test() As Object) If IsArray(test) = False Then For Each i As Object In test Debug.Print(i.ToString) Next Else For i As Integer = 0 To test.Length - 1 Debug.Print(test(i).ToString) Next End If End Sub
i use the ParamArray for the function accept several parameters.
heres how i use it:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s(0))
i used the '0', because the ParamArray don't accept 's()'(an array). but i can't overloading the DebugPrint() just by changing the ParamArray.
so can i change the parameter for the ParamArray accept an array?
A ParamArray is always considered optional, so that can trip you up sometime:
https://msdn.microsoft.com/en-us/library/ct363x9h.aspx
I'd suggest that you specify the type - not "As Object', and to use it, test that the .Length > 0.
Beyond that, I'm not sure what you're asking.
"A problem well stated is a problem half solved.” - Charles F. Kettering
let change it a bit:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s())'see here the diference
error message:
"Number of indices is less than the number of dimensions of the indexed array. "
Try this instead:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder For Each st As String In s sb.AppendLine(st) Next Stop
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Marked as answer by Cambalinho Saturday, March 25, 2017 10:21 PM
-
-
-
let me ask anotherthing: why i can't overloading DebugPrint() just by changing the 'ParamArray' and yes use the same type?
I'm not following what you mean. Explain more please?
"A problem well stated is a problem half solved.” - Charles F. Kettering
like we know, we can Overloading the function's\sub's. but we must change the parameters types. but the ParamArray it's a keyword and not a type. so i can't overload it just by delete it, right? -
like we know, we can Overloading the function's\sub's. but we must change the parameters types. but the ParamArray it's a keyword and not a type. so i can't overload it just by delete it, right?
Oh I see - I think.
You're talking about method overloading. You can overload it by changing the signature and that can be done several ways, so long as the types are different - OR - the types are in a different order - OR - the quantity of parameters is different. The names are irrelevant.
Also though, it doesn't consider an optional parameter at all - so as far as it's concerned, nothing changed.
Make sense?
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Edited by Frank L. Smith Saturday, March 25, 2017 10:45 PM ...forgot one
-
like we know, we can Overloading the function's\sub's. but we must change the parameters types. but the ParamArray it's a keyword and not a type. so i can't overload it just by delete it, right?
Oh I see - I think.
You're talking about method overloading. You can overload it by changing the signature and that can be done several ways, so long as the types are different - OR - the types are in a different order. The names are irrelevant.
Also though, it doesn't consider an optional parameter at all - so as far as it's concerned, nothing changed.
Make sense?
"A problem well stated is a problem half solved.” - Charles F. Kettering
yes make sence, but it's limited because don't accept arrays. unless we change the arrays to System.Text.StringBuilder type(maybe theres anothers for numbers too)
-
yes make sence, but it's limited because don't accept arrays. unless we change the arrays to System.Text.StringBuilder type(maybe theres anothers for numbers too)
Experiment with it - as much as anything, that's never a bad idea.
You'll remember it later when it comes in handy. :-)
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
i convert it from a number to System.Text.StringBuilder:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As String In a sb.AppendLine(st) Next Debug.Print(sb.ToString)
and i get the output
i'm sorry but i'm confused(from what i had learned) why the 'new' is used on declaration and not assingned('=')?
thanks for all
-
i convert it from a number to System.Text.StringBuilder:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As String In a sb.AppendLine(st) Next Debug.Print(sb.ToString)
and i get the output
i'm sorry but i'm confused(from what i had learned) why the 'new' is used on declaration and not assingned('=')?
thanks for all
I'm not following what you're trying to do, really, but think about this:
Dim a() As Integer
Ok, that says that you're declaring an integer array. Then next you use this:
For Each st As String In a
Do you see the problem? It should be giving you a compile error, no?
"a" is declared as an array of integer(s), not a string.
"A problem well stated is a problem half solved.” - Charles F. Kettering
- Edited by Frank L. Smith Saturday, March 25, 2017 10:57 PM ...misstated
-
i convert it from a number to System.Text.StringBuilder:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As String In a sb.AppendLine(st) Next Debug.Print(sb.ToString)
and i get the output
i'm sorry but i'm confused(from what i had learned) why the 'new' is used on declaration and not assingned('=')?
thanks for all
I'm not following what you're trying to do, really, but think about this:
Dim a() As Integer
Ok, that says that you're declaring an integer array. Then next you use this:
For Each st As String In a
Do you see the problem? It should be giving you a compile error, no?
"a" is an integer, not a string.
"A problem well stated is a problem half solved.” - Charles F. Kettering
my compiler don't say an error maybe because i have 1 option wrong on compiler :(
Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As Integer In a sb.AppendLine(st.ToString) Next Debug.Print(sb.ToString)
heres my compiler options:
explicit is on
strict is on
compare is binary
infer is on
maybe i miss understood the book language(i'm portuguse)
-
i convert it from a number to System.Text.StringBuilder:
Dim s() As String = {"hey", "hello", "hi"} Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As String In a sb.AppendLine(st) Next Debug.Print(sb.ToString)
and i get the output
i'm sorry but i'm confused(from what i had learned) why the 'new' is used on declaration and not assingned('=')?
thanks for all
I'm not following what you're trying to do, really, but think about this:
Dim a() As Integer
Ok, that says that you're declaring an integer array. Then next you use this:
For Each st As String In a
Do you see the problem? It should be giving you a compile error, no?
"a" is an integer, not a string.
"A problem well stated is a problem half solved.” - Charles F. Kettering
my compiler don't say an error maybe because i have 1 option wrong on compiler :(
Dim sb As New System.Text.StringBuilder Dim a() As Integer = {10, 29, 40} For Each st As Integer In a sb.AppendLine(st.ToString) Next Debug.Print(sb.ToString)
heres my compiler options:
explicit is on
strict is on
compare is binary
infer is on
maybe i miss understood the book language(i'm portuguse)
Sorry for the delay.
Explain what you're trying to do and let's go from there.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
let change it a bit:
Dim s() As String = {"hey", "hello", "hi"} DebugPrint(s())'see here the diference
error message:
"Number of indices is less than the number of dimensions of the indexed array. "
imagine creating a sub with unlimited parameters of any type and accept arrays
@Cambalinho:
To pass an array as an argument, you pass it without the parentheses:
DebugPrint(s)
However, you need to change your DebugPrint code a wee bit. eg:
Private Sub DebugPrint(ByVal ParamArray test() As Object) For Each testItem As Object In test If IsArray(testItem) = False Then Debug.Print(testItem.ToString) Else For Each arrayElement In CType(testItem, Array) Debug.Print(arrayElement.ToString) Next End If Next End Sub
Example of use:
Dim intArr() As Integer = {1, 2, 3, 4} Dim strArr() As String = {"hey", "hello", "hi"} ' even multi-dimensional arrays... Dim str2dArray(,) As String = {{"R0 C0", "R0 C1"}, {"R1 C0", "R1 C1"}, {"R2 C0", "R2 C1"}} DebugPrint(11, strArr, "22", intArr, 3.412, str2dArray, 5.6) ' Output:= ' 11 ' hey ' hello ' hi ' 22 ' 1 ' 2 ' 3 ' 4 ' 3.412 ' R0 C0 ' R0 C1 ' R1 C0 ' R1 C1 ' R2 C0 ' R2 C1 ' 5.6