Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning one value to multiple variables 4

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
Hello,
I have about 50-60 variables of the String type in my app (variables I then pass to stored procedure) and I need to create a routine to assign an empty string to all of those (after each time I call the stored procedure to make sure that old values are not beign passed)
What is the quickest way to do this, is there a way I can quickly put them all into array and then go through array?
It takes a lot of coding to do them one by one like
strVariable=""
strVariable2=""
strVariable3=""

Thanks for any suggestions!
 
Dim strVariable(1 to 50) As String
Dim LCV As long

For LCV = LBound(strVariable) to UBound(strVariable) Step 1
strVariable(LCV) = ""
Next
 
If you create an array for the variables, you can then loop through each of them to store the values.

Code:
'Declare Your Array and other variables
Dim strVariable(50) As String
Dim intLoop As Integer

For intLoop = 1 To 50
    strVariable(intLoop) = "SOME_VALUE" 
    'Change "SOME_VALUE" to "" for Zero Length Strings
Next intLoop

You Can then pass them to your procedure using another loop.

Hope this helps.

jgjge3.gif
[tt]'Very funny, Scotty... Now Beam down my clothes.'[/tt]
 
Cross Post.... Both will work fine.

jgjge3.gif
[tt]'Very funny, Scotty... Now Beam down my clothes.'[/tt]
 
OK, I am a little confused. I have different names for each variable, like strSomeSttring, strOtherString, strYetOtherString, etc
I cannot make them all one name, because I pass them to sp and it will make it really confusing it all my variables will have the same name and the array index will be the only difference

Sorry, I did not mention this before

Any ideas?

thanks, everyone
 
One thing that you could do is create constants for your strings and then use the constants as the array index. That would be a way to self-document them.

Const SomeString As Long = 0
Const OtherString As Long = 1
Const YetOtherString As Long = 2

strVariable(SomeString) = "Whatever"
strVariable(OtherString) = "You want"
strVariable(YetOtherString) = "To Put in"

Otherwise, I'm afraid you're just going to have to intialize each one seperately.
 
To set all values in an array to default values (0s or "" or Nothing) you can also use;

Erase ArrayName

If its a Dynamic array it is completely destroyed (back to 0 elements), if its a Static array the array survives but all elements are blanked.

regards Hugh,
 
I need Developer155 to DISREGARD this answer as a solution.

For the more experienced ones, is ther no way at all to group the variables declared on the form/in the module and use something similar to a FOR EACH.... NEXT statement to loop through them?

Just clutching at straws, but it would probably be nice...




jgjge3.gif
[tt]'Very funny, Scotty... Now Beam down my clothes.'[/tt]
 
Could you loop through the controls collection and find the text fields with the names you want?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
trollacious,
Off topic... just fyi...
You can reference other threads like this:
thread222-726846

Just copy the the threadxxx-xxxxxx string from under the thread title...

for forumns, it's just:
forum222

as you can see, both automatically turn into hyperlinks...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
>group the variables declared on the form ... FOR EACH.... NEXT

I can do it for a form's Properties, Methods and Events, but can't seem to do it for variables, no matter what their scope in the form
 
(correct me if I'm wrong)

I think For Each Next is used excusively for Objects...

In VB.NET, variables are objects, so it *might* work there (note the MIGHT)

But in VB6, they are simply memory references...

Objects are typically linked together, in some way, in a tree type structure... which allows the FOR EACH method to loop through them...

Variables, on the other hand, are just referenced areas of memory, with no "Intelegent" structure to them, which is why this is not possible in VB6

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
strongm-

Would the code you have work for custom properties created using Get/Set/Let? If so, they could prefix the properties with something and set the ones that are prefixed to zero. Just a thought. I think that there may be over more viable solutions already posted here though. I am a bit curious on what method you came up with for enumerating the props, methods and events though if you have time to post it...
 
Yep, it works for custom properties...

(and, actually, I can get it working for a form's - or other class's - Public variables)

My full code is at home, so you'll have to wait a few hours before I can post a more complete version, but here's something to be going on with; You will need to add a reference to the Typelib Information library, and a form with a command button and a listbox:
Code:
[blue]Option Explicit

Public JustTesting as Long

Private Sub Command1_Click()
    getProperties Form1
End Sub

Public Sub getProperties(objTarget As Object)
    Dim myTLI As TLIApplication
    Dim myII As InterfaceInfo
    Dim myMI As MemberInfo
    Dim PropertyString As String

    Set myTLI = New TLIApplication
    
    Set myII = TLI.InterfaceInfoFromObject(objTarget)

    For Each myMI In myII.Members
        PropertyString = ""
        ' For this example only bother with properties that could return values
        If myMI.InvokeKind = INVOKE_PROPERTYGET Then
            PropertyString = myMI.Name
        List1.AddItem PropertyString
        End If
    Next
End Sub[/blue]

 
Hi
Everyone given u some way to solve this. I thought I will give the way I do, I always use this.
My answer is again use Array.
But you said u will have a problem of keeping track of index of the array. For that i will give u a solution.

VB prvoided enum to solve this. You can make an enum which is accessisble at both the location ie from where u r making the array and where u r using the array.
If both the places are in one project file, the u can place the enum in a module file and define it as public

Public Enum ListIndexForArray
idxID = 0
idxName
idxAddress
...
idxLastField
End Enum

The code u write in form is:

Dim vArray(idxLastField) As String

vArray(idxID) = "1"
vArray(idxName) = "Some Name"
vArray(idxAddress) = "Some address"
...
vArray(idxLastField) = "something"

The code to clear the array will be
Dim intIndex As Integer

For intIndex = idxID To idxLastFiled
vArray = ""
Next

And the code near stored procedure will be
parameter1 = vArray(idxID)
parameter2 = vArray(idxName)
parameter3 = vArray(idxAddress)
...
and so on till last field

Hope the solution is clear to u, if you find any difficulty, please let me know ur mail id, i will mail u a proper working code.

Regards




Som :)
 
strongm-

nice code, have a star on me! what else did you find at home?

I suppose that the reason you can do the public variables is related to the conversation that was had previously about the compiler automatically creating the Get and Set statements for them...
 
Here's the longer (but messier) example. Again, you will need to add a reference to the Typelib Information library, and a form with a command button

Code:
[blue]Option Explicit

Public wombat As Double

Private Sub Command1_Click()
    ' We'll walk a form for this example
    ' But we could declare one of ouir own or a third party class
    
    wombat = 5.25
    Dim myobj As Object
    Set myobj = Form1
    PropertyWalk myobj
    Debug.Print wombat
End Sub

' Function that walks through the properties of an object
' In this example we happen to only be interested in Function properties
Public Sub PropertyWalk(ByRef QueriedObject As Object)

   Dim TLIApp As TLIApplication
   Dim TLInfo  As TLI.InterfaceInfo

   ' Dim wombat As TypeLibInfo
    
    

   Dim MI As MemberInfo
   Dim vParam As Variant
   'Dim Param As Object
   Dim ParamCount As Long
   
   Dim Tail As String

   Set TLIApp = New TLIApplication
'  grab info from object
   Set TLInfo = TLI.InterfaceInfoFromObject(QueriedObject)
'   Set wombat = TLI.TypeLibInfoFromFile("c:\program files\common files\system\ado\msado15.dll") '(TLInfo.Guid, TLInfo.MajorVersion, TLInfo.MinorVersion, 1024)
 
'  Loop through attribute members of the object
   For Each MI In TLInfo.Members

    Select Case MI.InvokeKind
        Case INVOKE_CONST 'Constant
        Case INVOKE_EVENTFUNC ' Event
        Case INVOKE_FUNC ' sub/function
            ' AT this point MI contains all the info you need about a class
            If MI.AttributeMask = 0 Then
                Debug.Print "Private ";
            Else
                Debug.Print "Public ";
            End If
            If MI.ReturnType.VarType = VT_VOID Then
                Debug.Print "Procedure ";
                Tail = ""
            Else
                Debug.Print "Function ";
                Tail = DecodeVT(MI.ReturnType.VarType)
            End If
                
            Debug.Print MI.Name & "(";
                For ParamCount = 1 To MI.Parameters.Count
                    If ParamCount <> 1 Then Debug.Print ", ";
                    If MI.Parameters(ParamCount).Optional Then Debug.Print "Optional ";
                    Debug.Print MI.Parameters(ParamCount);
                    Debug.Print DecodeVT(MI.Parameters(ParamCount).VarTypeInfo.VarType);
                Next
            Debug.Print ")";
            Debug.Print Tail
            Beep
        Case INVOKE_PROPERTYGET ' Get
            Debug.Print "Public " & MI.Name & DecodeVT(MI.ReturnType.VarType)
            If MI.Name = "wombat" Then
                Debug.Print "Current value is: " & TLIApp.InvokeHook(QueriedObject, MI.Name, INVOKE_PROPERTYGET)
            End If
        Case INVOKE_PROPERTYPUT ' Let
            If MI.Name = "wombat" Then
                vParam = 17.258
                TLIApp.InvokeHook QueriedObject, MI.Name, INVOKE_PROPERTYPUT, vParam
            End If
        Case INVOKE_PROPERTYPUTREF ' Set
        Case INVOKE_UNKNOWN ' Dunno
        Case Else 'Oops
    End Select
   Next
End Sub

Private Function DecodeVT(VT As TliVarType) As String
    Select Case VT
        Case VT_R4
            DecodeVT = "Single"
        Case VT_R8
            DecodeVT = "Double"
        Case VT_I2
            DecodeVT = "Integer"
        Case VT_I4
            DecodeVT = "Long"
        Case VT_DISPATCH
            DecodeVT = "Object"
        Case VT_BSTR
            DecodeVT = "String"
        Case VT_BOOL
            DecodeVT = "Boolean"
        Case VT_UI1
            DecodeVT = "Byte"
        Case VT_CY
            DecodeVT = "Currency"
        Case VT_DATE
            DecodeVT = "Date"
        Case Else
    End Select
    
    If Len(DecodeVT) Then DecodeVT = " As " + DecodeVT
    
End Function
[/blue]
 
Now, this is way off topic, but is there anything like this for non-COM based .dll's?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top