473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Use concantenated string as variable name

rrocket
116 100+
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
10 5456
jeffstl
432 Expert 256MB
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
116 100+
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
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
116 100+
Param1 and Param2 are strings and the rest are optional.
May 30 '08 #5
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
116 100+
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
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
116 100+
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
116 100+
Anyone have any more ideas of how to do this? Or if it is even possible?
Jun 6 '08 #10
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

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

Similar topics

5
by: Nasos Makriyiannis | last post by:
Hi, I'm new to XSL and I was wondering if there is a string-compare function available. I am using the following IF statement but it does not seem to be working: <xsl:if...
2
by: Sebek | last post by:
Hello, I'm transforming a XML document in XHTML but I have problems using sub-strings, it will be clearer with an exemple: What I have: <form...
1
by: shaun roe | last post by:
I have an xml format for creating a database; lets say a fragment looks like this: <insertValue type="int">7</insert> <insertValue type="string">Hello everyone</insert> now I have a...
5
by: glenn | last post by:
Hi folks, The problem I have is that a query string works if hard-coded but if I pass a variable to it, it does not work as shown here. This works: querystring="SELECT * FROM USERS WHERE...
5
by: TheLostLeaf | last post by:
Hello, I am trying to get this code: p1_1.Image = System.Drawing.Bitmap.FromFile(imgPath + picture.ToString() + ".GIF"); p2_1.Image = System.Drawing.Bitmap.FromFile(imgPath +...
10
by: Bilal | last post by:
Hello, I'm trying to perform some string manipulations in my stylesheet and have gotten stuck on the issue below so hopefully can elicit some useful hints. Namely, the problem is that I need to...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
13
by: Jennifer.Berube | last post by:
well I'm not sure how to go about making my SQL connection string... The code below is what I need to replace with my SQL connection...I just don't know if that code is for DSN or access... I...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.