Use concantenated string as variable name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rrocket
    New Member
    • Aug 2007
    • 116

    Use concantenated string as variable name

    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:
    [code=vb]
    Dim iCount As Integer
    Dim objTemp As Object
    Dim strTemp As String

    For iCount = 1 To 50
    objTemp = "InputParam " & iCount.ToString
    strTemp = CStr(objTemp)
    Next
    [/code]

    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.
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    Originally posted by rrocket
    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:
    [code=vb]
    Dim iCount As Integer
    Dim objTemp As Object
    Dim strTemp As String

    For iCount = 1 To 50
    objTemp = "InputParam " & iCount.ToString
    strTemp = CStr(objTemp)
    Next
    [/code]

    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 :

    [code=vb]
    For icount = 1 to 50
    strTemp = txtParameter(ic ount).Text
    Next

    [/code]

    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 ?

    Comment

    • rrocket
      New Member
      • Aug 2007
      • 116

      #3
      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:
      [code=vb]
      Public Function RunSP(InputPara m1, InputParam2, etc...)
      'All the magic that is not currently working
      end function
      [/code]

      I need to go through the above InputParams and put them into something like this:
      [code=vb]
      cmd.Parameters( iCount).value = InputParam & ICount
      [/code]

      Does that make any better sense? :)

      Comment

      • tristanlbailey
        New Member
        • Apr 2007
        • 30

        #4
        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.

        Comment

        • rrocket
          New Member
          • Aug 2007
          • 116

          #5
          Param1 and Param2 are strings and the rest are optional.

          Comment

          • tristanlbailey
            New Member
            • Apr 2007
            • 30

            #6
            Then I presume that the function looks more like this:

            Example:
            Code:
            Public Function RunSP (InputParam1 As String, InputParam2 As String, Optional Var1...)
            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:
            Code:
            RunSP MyStrVar1, MyStrVar2
            *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:
            Code:
            RunSP = Something
            then the line with which you call the function needs to begin with a variable that stores the return value, like this:

            Example:
            Code:
            I = RunSP(MyStrVar1, MyStrVar2)
            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?

            Comment

            • rrocket
              New Member
              • Aug 2007
              • 116

              #7
              Originally posted by tristanlbailey
              Then I presume that the function looks more like this:

              Example:
              Code:
              Public Function RunSP (InputParam1 As String, InputParam2 As String, Optional Var1...)
              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:
              Code:
              RunSP MyStrVar1, MyStrVar2
              *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:
              Code:
              RunSP = Something
              then the line with which you call the function needs to begin with a variable that stores the return value, like this:

              Example:
              Code:
              I = RunSP(MyStrVar1, MyStrVar2)
              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.

              Comment

              • tristanlbailey
                New Member
                • Apr 2007
                • 30

                #8
                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.

                Comment

                • rrocket
                  New Member
                  • Aug 2007
                  • 116

                  #9
                  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:
                  [code=vb]
                  RunSP "StoredProcName ", StringVal1, StringVal2, optional1, optional2, etc
                  [/code]
                  There can be up to 50 optional values at this point.
                  2. Gets to the Main.bas file
                  [code=vb]
                  Public Function RunSP(StoredPro c as string, StringVal as String, StringVal as String, InputParam1 as optional, InputParam2 as optional, ..., InputParam50 as optional)
                  '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.

                  If IsMissing(Input Param1 = false then
                  cmd.Parameters( 1).Value = InputParam1

                  If IsMissing(Input Param2 = false then
                  cmd.Parameters( 2).Value = InputParam2

                  ' Through

                  If IsMissing(Input Param50) = false then
                  cmd.Parameters( 50).Value = InputParam50

                  end Function
                  [/code]

                  If InputParam1 = "Hello World" then "InputParam " & 1 should also equal the same value so that I can do something like this;

                  [code=vb]
                  for Count = 1 to 50
                  strTemp = "InputParam " & cstr(Count)
                  If IsMissing(strTe mp) = false then
                  cmd.Parameters( Count).Value = strTemp
                  [/code]

                  Let me know if something does not make sense. Thanks.

                  Comment

                  • rrocket
                    New Member
                    • Aug 2007
                    • 116

                    #10
                    Anyone have any more ideas of how to do this? Or if it is even possible?

                    Comment

                    • tristanlbailey
                      New Member
                      • Apr 2007
                      • 30

                      #11
                      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.

                      Comment

                      Working...