Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Data structure in VB

Hello All,
I am very new to VB.
I have a situation where I need something like a Hashtable.
Is there an equivalent to Hashtable in VB?

Loop through the resultset and load all the information in a data structure.

while not resultSet.EOF
   account = rsExist("account")
    pcfile = rsExist("pcfile")
    dateValue = rsExist("date")
    timeValue = rsExist("time")
    'Store this information in a collection
    filesInDB As New Collection

I thought I could create a new collection for each iteration and add it to the collection.
But, it doesnt let me create a new collection with-in an if or while block.
What is the solution?

Thanks in advance
smovva

0
smovva
Asked:
smovva
1 Solution
 
Arthur_WoodCommented:
1) you can create a User Defined Type (a Structure) in VB to hold the Data.  The create a Varibale of THAT user-defined typoe, to be added to the collection:

in the General Section of your code(at the VERY top of the code module)

Private Type MyData
   Account as String
   pcFile as String
   DateValue as Date
   TimeValue as Date
End Type
Private FilesInDB As Collection

then, where you are reading the data from the database:

Dim MyValues as MyType

Set FilesInDB = New Collection

Do While Not rsExist.EOF
  Set MYvalues = New MyData
  With MyValues
     .account = rsExist("account")
     .pcfile = rsExist("pcfile")
     .dateValue = rsExist("date")
     .timeValue = rsExist("time")
   'Store this information in the collection
     FilesInDb.Add MyValues
  End With
  rsExist.MoveNext
Loop

you will of course need to provide the code to load the data into the rsExist recordset.

Arthur Wood
0
 
smovvaAuthor Commented:
Thanks for the info.
It gives a compile Error:"Invalid use of New Keyword" at
set mYValues = New MyData
I am assuming u made a typo in
Dim MyValues as MyType

I declared it as
Dim MyValues as MyData

Thanks
smovva
0
 
smovvaAuthor Commented:
If I comment that line and move on (Just to compile)
it gave me another compile error
"Only User-defined types defined in public object modules can be coerced to or from a variant or passed to late bound functions" at FilesInDB.Add MyValues

Thanks
smovva
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Arthur_WoodCommented:
ok, so declare :

Public Type MyData
  Account as String
  pcFile as String
  DateValue as Date
  TimeValue as Date
End Type


and yes, that is correct (my error--Set MyValues = New MyData was WRONG, and that line Should NOT be in the code, and YES, it should have read :

Dim MyValues as MyData  )


AW
0
 
smovvaAuthor Commented:
still gives the same error:
"Only User-defined types defined in public object modules can be coerced to or from a variant or passed to late bound functions" at FilesInDB.Add MyValues

I am not doing anything different. I copied your code into a test project and trying to compile.

Thanks
smovva
0
 
DeepBlueInVBCommented:
you can also use a disconnected recordset.

create a Recordset at the Client side and populate it with the data of rsExist.


0
 
Arthur_WoodCommented:
ok, then we'll take another appraoch>>
Add a Class Module to your project,with thisd code:

Option Explicit
Dim mAccount As String
Dim mFile As String
Dim mDateValue As Date
Dim mTimeValue As Date

Public Property Get Account() As String
    Account = mAccount
End Property

Public Property Let Account(strValue As String)
    mAccount = strValue
End Property

Public Property Get pcFile() As String
    pcFile = mFile
End Property

Public Property Let pcFile(strValue As String)
    mFile = strValue
End Property

Public Property Get DateValue() As Date
    DateValue = mDateValue
End Property

Public Property Let DateValue(dtValue As Date)
    mDateValue = dtValue
End Property

Public Property Get TimeValue() As Date
    TimeValue = mTimeValue
End Property

Public Property Let TimeValue(dtValue As Date)
    mTimeValue = dtValue
End Property

Change the Properties of the Class module, to give it the name MyCLass (defaults to Class1)


Then change the calling code like this:


Dim MyValues As MyClass
Dim rsExist As Recordset
Set FilesInDB = New Collection

Do While Not rsExist.EOF
 Set MyValues = New MyClass
 With MyValues

    .Account = rsExist("account")
    .pcFile = rsExist("pcfile")
    .DateValue = rsExist("date")
    .TimeValue = rsExist("time")
  'Store this information in the collection
    FilesInDB.Add MyValues
 End With
 rsExist.MoveNext
Loop





0
 
Arthur_WoodCommented:
and welcome to the world of Object Oriented programming.  

;-)

AW
0
 
smovvaAuthor Commented:
It compiles now.
I will let you know when I am done.
Thanks alot for your help!!
smovva
0
 
Arthur_WoodCommented:
what you have created here is a VERY simple example of a Class.  At its simplest, a Class is a somewhat mode powerful version of a User Defined Type.

A class is in fact a NEW type (like an Integer, or a Form), which can have data, and can, in a more extended version, have 'methods' - operations that an object created from the class, can do.  This example simply allows you to store and retrieve some values (Account, pcFile etc).  Once you create an Object based on the class (called an INSTANCE of the class) then you can save THAT object, as we are doing here, in a Collection.  When you need the object, you can then get it from the Collection (the first object added to the collection can be retrieved using

    Dim MyObject as MyClass
    Set MyObject = FilesinDB.Item(1)  'the first item in the collection - the FIRST one that was Added originally


0
 
CleanupPingCommented:
Hi smovva,
This old question (QID 20562286) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 
smovvaAuthor Commented:
Good info for a beginer like me.
0
 
Arthur_WoodCommented:
thank you
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now