Possible to pass a string to a Function as name of an array?
-
Hi all, I encountered a little problem, after I decided to tramsform a Sub in a Function (and made the name of the array involved in Function, as argument to pass to the Function as string). Code (Note: Asterisks and Number Sign # are note part of code)---------
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
'Dim NameArray As String # Dim LenString As UInteger = Len(NameArray(ComboBox1.SelectedIndex + 1)) StringWpt = "" : Index = 0 : CommaCounter = 0 Do Until CommaCounter = Field - 1 \* If NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," Then CommaCounter = CommaCounter + 1 Index = Index + 1 Loop \* If Index = Len(NameArray(ComboBox1.SelectedIndex + 1)) Then GoTo 10 Do Until NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," \* StringWpt = StringWpt + NameArray(ComboBox1.SelectedIndex + 1)(Index) Index = Index + 1 : If Index = LenString Then Exit Do Loop
10: Return LTrim(StringWpt)
End Function
Logically, the code (when working as Sub) is correct and works fine . The problem is that exactely like is written above, the Function generate 3 errors "Expression is not an array or a method, and cannot have an argument list" in the lines I signed with *, but strangely is not generated in the line I signed with #. If I enable as code the comment-line immediately below the definition of Function, and re-define again NameArray, the 3 errors are substituted with a single Error: "'NameArray' is already declared as a parameter of this method". Note: When working as Sub, I not used the Array-name as variable, but I used the effective name of Array. Do I forgotten something or is not possible to pass an Array name as argument? Thanks for help Ignazio
modified on Wednesday, September 9, 2009 12:16 PM
-
Hi all, I encountered a little problem, after I decided to tramsform a Sub in a Function (and made the name of the array involved in Function, as argument to pass to the Function as string). Code (Note: Asterisks and Number Sign # are note part of code)---------
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
'Dim NameArray As String # Dim LenString As UInteger = Len(NameArray(ComboBox1.SelectedIndex + 1)) StringWpt = "" : Index = 0 : CommaCounter = 0 Do Until CommaCounter = Field - 1 \* If NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," Then CommaCounter = CommaCounter + 1 Index = Index + 1 Loop \* If Index = Len(NameArray(ComboBox1.SelectedIndex + 1)) Then GoTo 10 Do Until NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," \* StringWpt = StringWpt + NameArray(ComboBox1.SelectedIndex + 1)(Index) Index = Index + 1 : If Index = LenString Then Exit Do Loop
10: Return LTrim(StringWpt)
End Function
Logically, the code (when working as Sub) is correct and works fine . The problem is that exactely like is written above, the Function generate 3 errors "Expression is not an array or a method, and cannot have an argument list" in the lines I signed with *, but strangely is not generated in the line I signed with #. If I enable as code the comment-line immediately below the definition of Function, and re-define again NameArray, the 3 errors are substituted with a single Error: "'NameArray' is already declared as a parameter of this method". Note: When working as Sub, I not used the Array-name as variable, but I used the effective name of Array. Do I forgotten something or is not possible to pass an Array name as argument? Thanks for help Ignazio
modified on Wednesday, September 9, 2009 12:16 PM
You cannot use the contents of a String variable as a variable name. I'd suggest rewriting the entire concept using collections of arrays, but, given the code sample you posted, I'm not sure if my explanation is going to make any sense to you at all. You're using VERY old BASIC concepts in VB.NET. I would HIGHLY suggest picking up a beginners book on VB.NET before continuing this project.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Hi all, I encountered a little problem, after I decided to tramsform a Sub in a Function (and made the name of the array involved in Function, as argument to pass to the Function as string). Code (Note: Asterisks and Number Sign # are note part of code)---------
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
'Dim NameArray As String # Dim LenString As UInteger = Len(NameArray(ComboBox1.SelectedIndex + 1)) StringWpt = "" : Index = 0 : CommaCounter = 0 Do Until CommaCounter = Field - 1 \* If NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," Then CommaCounter = CommaCounter + 1 Index = Index + 1 Loop \* If Index = Len(NameArray(ComboBox1.SelectedIndex + 1)) Then GoTo 10 Do Until NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," \* StringWpt = StringWpt + NameArray(ComboBox1.SelectedIndex + 1)(Index) Index = Index + 1 : If Index = LenString Then Exit Do Loop
10: Return LTrim(StringWpt)
End Function
Logically, the code (when working as Sub) is correct and works fine . The problem is that exactely like is written above, the Function generate 3 errors "Expression is not an array or a method, and cannot have an argument list" in the lines I signed with *, but strangely is not generated in the line I signed with #. If I enable as code the comment-line immediately below the definition of Function, and re-define again NameArray, the 3 errors are substituted with a single Error: "'NameArray' is already declared as a parameter of this method". Note: When working as Sub, I not used the Array-name as variable, but I used the effective name of Array. Do I forgotten something or is not possible to pass an Array name as argument? Thanks for help Ignazio
modified on Wednesday, September 9, 2009 12:16 PM
You are passing a string as a parameter. It will not behave as an array. You will need to pass the array as a method parameter. I would second Dave's recommendation to getting a book.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
-
You cannot use the contents of a String variable as a variable name. I'd suggest rewriting the entire concept using collections of arrays, but, given the code sample you posted, I'm not sure if my explanation is going to make any sense to you at all. You're using VERY old BASIC concepts in VB.NET. I would HIGHLY suggest picking up a beginners book on VB.NET before continuing this project.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thanks for reply Dave, But I can accept only first part of your help. For second part, I let you know that I decided to not use a collection of Arrays because rarely I need to use more than 1 Array and I found this solution the more convenient(and efficent)for me. I understand what you want say, but I not agree with you when you write that latest concepts of VB.NET are better than older concepts of Basic. Often old concepts of Basic are better and more efficent than latest. It's not a case that Microsoft mantained the compatibility of "Old Basic" in VB.NET And it's for these reasons that when possible (and convenient)I prefer to use the "OLD concepts". I not uderstand where in the code you see something that remember to you "Very old concept of Basic". If you refer to the GoTo instruction, do you really think that changing it the code is improved (or made more efficent)? Ignazio
-
You are passing a string as a parameter. It will not behave as an array. You will need to pass the array as a method parameter. I would second Dave's recommendation to getting a book.
It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD
Thanks for help, I think I'will follow your guideline. But I don't think will be necessary to buy and read a book. Ignazio
modified on Wednesday, September 9, 2009 3:40 PM
-
Thanks for help, I think I'will follow your guideline. But I don't think will be necessary to buy and read a book. Ignazio
modified on Wednesday, September 9, 2009 3:40 PM
d@nish is saying you need to purchase a book and read it. That is what Dave said as well, in fact all replies are suggesting you need to learn vb.net.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
-
Thanks for reply Dave, But I can accept only first part of your help. For second part, I let you know that I decided to not use a collection of Arrays because rarely I need to use more than 1 Array and I found this solution the more convenient(and efficent)for me. I understand what you want say, but I not agree with you when you write that latest concepts of VB.NET are better than older concepts of Basic. Often old concepts of Basic are better and more efficent than latest. It's not a case that Microsoft mantained the compatibility of "Old Basic" in VB.NET And it's for these reasons that when possible (and convenient)I prefer to use the "OLD concepts". I not uderstand where in the code you see something that remember to you "Very old concept of Basic". If you refer to the GoTo instruction, do you really think that changing it the code is improved (or made more efficent)? Ignazio
Hurricane3000 wrote:
but I not agree with you when you write that latest concepts of VB.NET are better than older concepts of Basic. Often old concepts of Basic are better and more efficent than latest.
Well, you should ponder this: 1 - A Microsoft team worked to clean up and improve VB, do you think you know better than them ? 2 - In fact MS wanted to stop VB users from being able to use old VB6 concepts completely, because they are that bad, however, a bunch of luddite MVPs howled about it and so they changed it at the last minute.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi all, I encountered a little problem, after I decided to tramsform a Sub in a Function (and made the name of the array involved in Function, as argument to pass to the Function as string). Code (Note: Asterisks and Number Sign # are note part of code)---------
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
'Dim NameArray As String # Dim LenString As UInteger = Len(NameArray(ComboBox1.SelectedIndex + 1)) StringWpt = "" : Index = 0 : CommaCounter = 0 Do Until CommaCounter = Field - 1 \* If NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," Then CommaCounter = CommaCounter + 1 Index = Index + 1 Loop \* If Index = Len(NameArray(ComboBox1.SelectedIndex + 1)) Then GoTo 10 Do Until NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," \* StringWpt = StringWpt + NameArray(ComboBox1.SelectedIndex + 1)(Index) Index = Index + 1 : If Index = LenString Then Exit Do Loop
10: Return LTrim(StringWpt)
End Function
Logically, the code (when working as Sub) is correct and works fine . The problem is that exactely like is written above, the Function generate 3 errors "Expression is not an array or a method, and cannot have an argument list" in the lines I signed with *, but strangely is not generated in the line I signed with #. If I enable as code the comment-line immediately below the definition of Function, and re-define again NameArray, the 3 errors are substituted with a single Error: "'NameArray' is already declared as a parameter of this method". Note: When working as Sub, I not used the Array-name as variable, but I used the effective name of Array. Do I forgotten something or is not possible to pass an Array name as argument? Thanks for help Ignazio
modified on Wednesday, September 9, 2009 12:16 PM
I disagree with the others. Your code is perfect. It's a masterpiece of obsfucation. I'm sure when you're trying to maintain it, that the extra time you take will go right onto your hourly rate, and I'm sure that writing code this hard to read means that the client will never find anyone else willing to work on it. Seriously, when professionals tell you that you need to update your skillset, your options are to accept their advice, or assume that everyone is an idiot except for you. People I've known who do the latter, tend to end up unemployed.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
I disagree with the others. Your code is perfect. It's a masterpiece of obsfucation. I'm sure when you're trying to maintain it, that the extra time you take will go right onto your hourly rate, and I'm sure that writing code this hard to read means that the client will never find anyone else willing to work on it. Seriously, when professionals tell you that you need to update your skillset, your options are to accept their advice, or assume that everyone is an idiot except for you. People I've known who do the latter, tend to end up unemployed.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Hi, I agree with you when say that my code is a masterpiece of obsfucation, but this is my "unwanted" style. The advantage is that it is a "natural obsfucation". I ensure that it works, and it is to selectively read fields contained in text-records (lines) stored in an Array. Fields (of each record) are separated by commas. The record is selected in a comboBox. Absolutely it's not true that when professionals tell me that I need to update my skillset, my options are to accept their advice, or assume that everyone is an idiot except for me! For me doesn't exist the second of yours options. For me the possible options are 3: Accept, Consider, Not accept. But in no case Not accept can means to consider others idiot, of course! I find strange if you consider only the 2 options, you written. Ignazio
-
Thanks for reply Dave, But I can accept only first part of your help. For second part, I let you know that I decided to not use a collection of Arrays because rarely I need to use more than 1 Array and I found this solution the more convenient(and efficent)for me. I understand what you want say, but I not agree with you when you write that latest concepts of VB.NET are better than older concepts of Basic. Often old concepts of Basic are better and more efficent than latest. It's not a case that Microsoft mantained the compatibility of "Old Basic" in VB.NET And it's for these reasons that when possible (and convenient)I prefer to use the "OLD concepts". I not uderstand where in the code you see something that remember to you "Very old concept of Basic". If you refer to the GoTo instruction, do you really think that changing it the code is improved (or made more efficent)? Ignazio
Hurricane3000 wrote:
but I not agree with you when you write that latest concepts of VB.NET are better than older concepts of Basic.
Considering your having problems with code written in the Dark Ages and could be far better resolved with modern OOP code, I think you're wrong. You've got legacy concepts all over that code snippet you posted. From the colons to the Goto to the non-terminated If statements and then some, you've got legacy concepts written all over this. I haven't used a Goto statement in, let's see..., oh! 9 years now. The more you use the old concepts, the deeper the hole you're digging yourself. If you want to maintain an ancient relic of a skill set, go ahead. You're only limiting your own abilities to keep a job.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Hi, I agree with you when say that my code is a masterpiece of obsfucation, but this is my "unwanted" style. The advantage is that it is a "natural obsfucation". I ensure that it works, and it is to selectively read fields contained in text-records (lines) stored in an Array. Fields (of each record) are separated by commas. The record is selected in a comboBox. Absolutely it's not true that when professionals tell me that I need to update my skillset, my options are to accept their advice, or assume that everyone is an idiot except for me! For me doesn't exist the second of yours options. For me the possible options are 3: Accept, Consider, Not accept. But in no case Not accept can means to consider others idiot, of course! I find strange if you consider only the 2 options, you written. Ignazio
Hurricane3000 wrote:
For me the possible options are 3: Accept, Consider, Not accept.
Well, you need a reason to not accept. Either we're all stupid, or we're right.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hurricane3000 wrote:
For me the possible options are 3: Accept, Consider, Not accept.
Well, you need a reason to not accept. Either we're all stupid, or we're right.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
If your last want be a question, I think that it will remains an "unaswered question" because yours is a "wrong question". Boolean matematich says that for each element(bit) added to a binary word, possible combinations are doubled. For example: If a binary word have only 1 bit, possible combinatins are 2. If a binary word have 4 bit, possible combinatins are 16. If a binary word have 8 bit, possible combinatins are 256. In your question you not specified the word "All" how many bits have. If "All" have more than 1 element(bit), the minimum possibilities to consider are 4. Example: 00,01,10,11 (not only 00 or 11). More, if words are minimum 2, to these can be applied following operators: And, Nand, Or, Nor, XOr, Not-XOr, with a lot of further possibilities.
-
Hi all, I encountered a little problem, after I decided to tramsform a Sub in a Function (and made the name of the array involved in Function, as argument to pass to the Function as string). Code (Note: Asterisks and Number Sign # are note part of code)---------
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
'Dim NameArray As String # Dim LenString As UInteger = Len(NameArray(ComboBox1.SelectedIndex + 1)) StringWpt = "" : Index = 0 : CommaCounter = 0 Do Until CommaCounter = Field - 1 \* If NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," Then CommaCounter = CommaCounter + 1 Index = Index + 1 Loop \* If Index = Len(NameArray(ComboBox1.SelectedIndex + 1)) Then GoTo 10 Do Until NameArray(ComboBox1.SelectedIndex + 1)(Index) = "," \* StringWpt = StringWpt + NameArray(ComboBox1.SelectedIndex + 1)(Index) Index = Index + 1 : If Index = LenString Then Exit Do Loop
10: Return LTrim(StringWpt)
End Function
Logically, the code (when working as Sub) is correct and works fine . The problem is that exactely like is written above, the Function generate 3 errors "Expression is not an array or a method, and cannot have an argument list" in the lines I signed with *, but strangely is not generated in the line I signed with #. If I enable as code the comment-line immediately below the definition of Function, and re-define again NameArray, the 3 errors are substituted with a single Error: "'NameArray' is already declared as a parameter of this method". Note: When working as Sub, I not used the Array-name as variable, but I used the effective name of Array. Do I forgotten something or is not possible to pass an Array name as argument? Thanks for help Ignazio
modified on Wednesday, September 9, 2009 12:16 PM
Hi all, I solved the problem simply changing the first line of code (definition of function)
Public Function ReadField(ByVal NameArray As String, ByVal Field As Integer) As String
With this one:
Public Function ReadField(ByRef NameArray() As String, ByVal Field As Integer) As String
Thanks fo help Ignazio