By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,062 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,062 IT Pros & Developers. It's quick & easy.

Use concantenated string as variable name

rrocket
100+
P: 116
I have a bunch of params:
InputParam1, InputParam2, InputParam3, etc...

I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work correctly.

Here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Dim iCount As Integer
  2. Dim objTemp As Object
  3. Dim strTemp As String
  4.  
  5. For iCount = 1 To 50
  6.     objTemp = "InputParam" & iCount.ToString
  7.     strTemp = CStr(objTemp)
  8. Next
  9.  
The major issue with what I have is that I cannot assign the string to the object and just creating the string gives me an error too.
May 29 '08 #1
Share this Question
Share on Google+
10 Replies


jeffstl
Expert 100+
P: 432
I have a bunch of params:
InputParam1, InputParam2, InputParam3, etc...

I would like to loop through them instead of writing out code for each one, but am having some issues getting it to work correctly.

Here is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. Dim iCount As Integer
  2. Dim objTemp As Object
  3. Dim strTemp As String
  4.  
  5. For iCount = 1 To 50
  6.     objTemp = "InputParam" & iCount.ToString
  7.     strTemp = CStr(objTemp)
  8. Next
  9.  
The major issue with what I have is that I cannot assign the string to the object and just creating the string gives me an error too.
Hm. I know what you are talking about and I have done this with ASP pages before but with using text boxes.

So I guess I'm confused, how are you collecting these input parameters?

If you are collecting them from textboxes why not make an array of textboxes? Then just loop through those instead of looping through named parameters?

Something like :

Expand|Select|Wrap|Line Numbers
  1. For icount = 1 to 50
  2.      strTemp = txtParameter(icount).Text
  3. Next
  4.  
  5.  
I guess another thing I was wondering was if this was VB.NET or VB6.0? I am not sure that .ToString is available in VB6.0 ?
May 29 '08 #2

rrocket
100+
P: 116
It is unfortunately VB6 and I am not the original programmer. The values are being passed to a function that sets up a stored procedure....

The initial function looks something like this:
Expand|Select|Wrap|Line Numbers
  1. Public Function RunSP(InputParam1, InputParam2, etc...)
  2. 'All the magic that is not currently working
  3. end function
  4.  
I need to go through the above InputParams and put them into something like this:
Expand|Select|Wrap|Line Numbers
  1. cmd.Parameters(iCount).value = InputParam & ICount
  2.  
Does that make any better sense? :)
May 29 '08 #3

P: 31
On the "Public Function RunSP..." line, do any of the InputParams have a type after them?

For example: InputParam1 As String
OR InputParam1 As Long

Usually there is some form of type that is assigned to each parameter in a function. If there isn't, it certainly makes it more difficult to determine which types of variables should be sent to the function as parameters.
We would need to get a good look at the rest of the function to get a better understanding of how it works.
May 30 '08 #4

rrocket
100+
P: 116
Param1 and Param2 are strings and the rest are optional.
May 30 '08 #5

P: 31
Then I presume that the function looks more like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. Public Function RunSP (InputParam1 As String, InputParam2 As String, Optional Var1...)
  2.  
If the rest of the parameters have the optional keyword next to them, then you obviously don't need to give them to the function.

Since the first two parameters are apparently of the "string" type, you need to send two string variables to the function, like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. RunSP MyStrVar1, MyStrVar2
  2.  
*Note: You can call the string variables whatever you want.

If the function has a "return value" (i.e. there is a line in the function itself somewhere that looks like this);

Example:
Expand|Select|Wrap|Line Numbers
  1. RunSP = Something
  2.  
then the line with which you call the function needs to begin with a variable that stores the return value, like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. I = RunSP(MyStrVar1, MyStrVar2)
  2.  
The variable that you use to store the return value must match (or be very much like) the return value's type.

I take it that you are using VB.NET?
Jun 1 '08 #6

rrocket
100+
P: 116
Then I presume that the function looks more like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. Public Function RunSP (InputParam1 As String, InputParam2 As String, Optional Var1...)
  2.  
If the rest of the parameters have the optional keyword next to them, then you obviously don't need to give them to the function.

Since the first two parameters are apparently of the "string" type, you need to send two string variables to the function, like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. RunSP MyStrVar1, MyStrVar2
  2.  
*Note: You can call the string variables whatever you want.

If the function has a "return value" (i.e. there is a line in the function itself somewhere that looks like this);

Example:
Expand|Select|Wrap|Line Numbers
  1. RunSP = Something
  2.  
then the line with which you call the function needs to begin with a variable that stores the return value, like this:

Example:
Expand|Select|Wrap|Line Numbers
  1. I = RunSP(MyStrVar1, MyStrVar2)
  2.  
The variable that you use to store the return value must match (or be very much like) the return value's type.

I take it that you are using VB.NET?
No, I am using VB6. Yes, I definitely need to give the optional strings to the function.
Jun 2 '08 #7

P: 31
Your first post mentions a "ToString" method.
I don't believe that this exists in VB6, which led me to believe that you were using VB.NET, or at least the code that you were trying to use came from a VB.NET progam.

We could probably solve this problem a lot quicker if you:

A: Provided us with the bulk of the code that you are trying to use (or all of it if you are not sure)

B: Told us exactly what it is that you are trying to progam.
Jun 3 '08 #8

rrocket
100+
P: 116
My bad on the toString part... I typed it out instead of copying it over and am just used to writing in .Net these days.

Here is the idea of how things are currently working:

1. The initial function that is being called:
Expand|Select|Wrap|Line Numbers
  1. RunSP "StoredProcName", StringVal1, StringVal2, optional1, optional2, etc
  2.  
There can be up to 50 optional values at this point.
2. Gets to the Main.bas file
Expand|Select|Wrap|Line Numbers
  1. Public Function RunSP(StoredProc as string, StringVal as String, StringVal as String, InputParam1 as optional, InputParam2 as optional, ..., InputParam50 as optional)
  2. 'This is what I would rather loop through instead of typing out the way I have it now.  My initial post has an example of how I was trying to do it...  Minus the .tostring part.
  3.  
  4. If IsMissing(InputParam1 = false then
  5. cmd.Parameters(1).Value = InputParam1
  6.  
  7. If IsMissing(InputParam2 = false then
  8. cmd.Parameters(2).Value = InputParam2
  9.  
  10. ' Through
  11.  
  12. If IsMissing(InputParam50) = false then
  13. cmd.Parameters(50).Value = InputParam50
  14.  
  15. end Function
  16.  
If InputParam1 = "Hello World" then "InputParam" & 1 should also equal the same value so that I can do something like this;

Expand|Select|Wrap|Line Numbers
  1. for Count = 1 to 50
  2. strTemp = "InputParam" & cstr(Count)
  3. If IsMissing(strTemp) = false then
  4. cmd.Parameters(Count).Value = strTemp
  5.  
Let me know if something does not make sense. Thanks.
Jun 3 '08 #9

rrocket
100+
P: 116
Anyone have any more ideas of how to do this? Or if it is even possible?
Jun 6 '08 #10

P: 31
Sorry I didn't get back to you quicker; I've been fairly busy over the last few days.

Have you tried giving an array to the function, and then looping through the array within the function? Using this approach would be much better than the existing one.

Reply again if you are not sure, and I'll try to provide you with some sample code.
Jun 8 '08 #11

Post your reply

Sign in to post your reply or Sign up for a free account.