ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Dictionary - Add method parameters (https://www.excelbanter.com/excel-programming/326000-dictionary-add-method-parameters.html)

Jac Tremblay[_4_]

Dictionary - Add method parameters
 
I checked many posts about the Scripting.Dictionary and still cannot figure
out what the two parameters are, the key and a description or an item and a
key or what? Then what is an item (or the description) and what can it be
used for?
If I have a description for each key, can I store it alongside the key and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On each
line, there is a contract number and a 3 character field (a code) that I must
use to determine the template to use. Can I use the dictionary to solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number and its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay

Bob Phillips[_6_]

Dictionary - Add method parameters
 
The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot

figure
out what the two parameters are, the key and a description or an item and

a
key or what? Then what is an item (or the description) and what can it be
used for?
If I have a description for each key, can I store it alongside the key and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On each
line, there is a contract number and a 3 character field (a code) that I

must
use to determine the template to use. Can I use the dictionary to solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number and

its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay




Tom Ogilvy

Dictionary - Add method parameters
 
so
Dic.Add CStr(contract_number), template_code

if you will look up using the contract number to get the template code.

--
Regards,
Tom Ogilvy


"Bob Phillips" wrote in message
...
The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot

figure
out what the two parameters are, the key and a description or an item

and
a
key or what? Then what is an item (or the description) and what can it

be
used for?
If I have a description for each key, can I store it alongside the key

and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On each
line, there is a contract number and a 3 character field (a code) that I

must
use to determine the template to use. Can I use the dictionary to solve

my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number and

its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay






Jac Tremblay[_4_]

Dictionary - Add method parameters
 
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could not get
any help on the Scripting.Dictionary; only the Word Dictionary. Next time, I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely have to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot

figure
out what the two parameters are, the key and a description or an item and

a
key or what? Then what is an item (or the description) and what can it be
used for?
If I have a description for each key, can I store it alongside the key and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On each
line, there is a contract number and a 3 character field (a code) that I

must
use to determine the template to use. Can I use the dictionary to solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number and

its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay





Bob Phillips[_6_]

Dictionary - Add method parameters
 
Hi Jac,

Yes I guess so, but why would you need the index, the Dictionary object is
not index based. If you want the value, try

If d.Exists("c") Then
MsgBox d.Item("c")
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could not

get
any help on the Scripting.Dictionary; only the Word Dictionary. Next time,

I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely have

to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot

figure
out what the two parameters are, the key and a description or an item

and
a
key or what? Then what is an item (or the description) and what can it

be
used for?
If I have a description for each key, can I store it alongside the key

and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On

each
line, there is a contract number and a 3 character field (a code) that

I
must
use to determine the template to use. Can I use the dictionary to

solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number

and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay







Jac Tremblay[_4_]

Dictionary - Add method parameters
 
Hi again Bob,
The reason is that I need the corresponding template code to determine which
template to use for a particular contract number.

"Bob Phillips" wrote:

Hi Jac,

Yes I guess so, but why would you need the index, the Dictionary object is
not index based. If you want the value, try

If d.Exists("c") Then
MsgBox d.Item("c")
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could not

get
any help on the Scripting.Dictionary; only the Word Dictionary. Next time,

I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely have

to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot
figure
out what the two parameters are, the key and a description or an item

and
a
key or what? Then what is an item (or the description) and what can it

be
used for?
If I have a description for each key, can I store it alongside the key

and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On

each
line, there is a contract number and a 3 character field (a code) that

I
must
use to determine the template to use. Can I use the dictionary to

solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number

and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay







Jac Tremblay[_4_]

Dictionary - Add method parameters
 
Hi Tom,
Why do you convert to string the contract number and not the template code
(or both)?
Thanks for your reply.


"Tom Ogilvy" wrote:

so
Dic.Add CStr(contract_number), template_code

if you will look up using the contract number to get the template code.

--
Regards,
Tom Ogilvy


"Bob Phillips" wrote in message
...
The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot

figure
out what the two parameters are, the key and a description or an item

and
a
key or what? Then what is an item (or the description) and what can it

be
used for?
If I have a description for each key, can I store it alongside the key

and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On each
line, there is a contract number and a 3 character field (a code) that I

must
use to determine the template to use. Can I use the dictionary to solve

my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number and

its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay







Tom Ogilvy

Dictionary - Add method parameters
 
That is the whole point. You use the contract number to retrieve the
contract code. You don't need the index number.
In Bob's example, the sample code:

If d.Exists("c") Then
MsgBox d.Item("c")
End If



should return Cairo in the message box.

--
Regards,
Tom Ogilvy



"Jac Tremblay" wrote in message
...
Hi again Bob,
The reason is that I need the corresponding template code to determine

which
template to use for a particular contract number.

"Bob Phillips" wrote:

Hi Jac,

Yes I guess so, but why would you need the index, the Dictionary object

is
not index based. If you want the value, try

If d.Exists("c") Then
MsgBox d.Item("c")
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could

not
get
any help on the Scripting.Dictionary; only the Word Dictionary. Next

time,
I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely

have
to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still

cannot
figure
out what the two parameters are, the key and a description or an

item
and
a
key or what? Then what is an item (or the description) and what

can it
be
used for?
If I have a description for each key, can I store it alongside the

key
and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On

each
line, there is a contract number and a 3 character field (a code)

that
I
must
use to determine the template to use. Can I use the dictionary to

solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract

number
and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay









Tom Ogilvy

Dictionary - Add method parameters
 
out of habit from using collections. You can use a number

Sub AB()
Set d = CreateObject("Scripting.Dictionary")
d.Add 12, "Athens" ' Add some keys and items.
d.Add 18, "Belgrade"
d.Add 32, "Cairo"

Debug.Print d.Item(32)

End Sub

Will Return Cairo


--
Regards,
Tom Ogilvy



"Jac Tremblay" wrote in message
...
Hi Tom,
Why do you convert to string the contract number and not the template code
(or both)?
Thanks for your reply.


"Tom Ogilvy" wrote:

so
Dic.Add CStr(contract_number), template_code

if you will look up using the contract number to get the template code.

--
Regards,
Tom Ogilvy


"Bob Phillips" wrote in message
...
The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot
figure
out what the two parameters are, the key and a description or an

item
and
a
key or what? Then what is an item (or the description) and what can

it
be
used for?
If I have a description for each key, can I store it alongside the

key
and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On

each
line, there is a contract number and a 3 character field (a code)

that I
must
use to determine the template to use. Can I use the dictionary to

solve
my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number

and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay








Jac Tremblay[_4_]

Dictionary - Add method parameters
 
Hi Bob,
You are right again. I had not noticed that one could get the item value
directly like that. There is no need to make things more complicated than
they really are.
Thanks to you, and thanks to Tom who opened my eyes.


"Bob Phillips" wrote:

Hi Jac,

Yes I guess so, but why would you need the index, the Dictionary object is
not index based. If you want the value, try

If d.Exists("c") Then
MsgBox d.Item("c")
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could not

get
any help on the Scripting.Dictionary; only the Word Dictionary. Next time,

I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely have

to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still cannot
figure
out what the two parameters are, the key and a description or an item

and
a
key or what? Then what is an item (or the description) and what can it

be
used for?
If I have a description for each key, can I store it alongside the key

and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On

each
line, there is a contract number and a 3 character field (a code) that

I
must
use to determine the template to use. Can I use the dictionary to

solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract number

and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay







Jac Tremblay[_4_]

Dictionary - Add method parameters
 
Your are right again, Tom. Thank you for your comment. That makes my day.

"Tom Ogilvy" wrote:

That is the whole point. You use the contract number to retrieve the
contract code. You don't need the index number.
In Bob's example, the sample code:

If d.Exists("c") Then
MsgBox d.Item("c")
End If



should return Cairo in the message box.

--
Regards,
Tom Ogilvy



"Jac Tremblay" wrote in message
...
Hi again Bob,
The reason is that I need the corresponding template code to determine

which
template to use for a particular contract number.

"Bob Phillips" wrote:

Hi Jac,

Yes I guess so, but why would you need the index, the Dictionary object

is
not index based. If you want the value, try

If d.Exists("c") Then
MsgBox d.Item("c")
End If


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
Hi Bob,
Your reply is quite helpful. I had only checked the F1 key and could

not
get
any help on the Scripting.Dictionary; only the Word Dictionary. Next

time,
I
will look in MSDN first.
Now that I know I can use the dictionary for my case, I have one more
question. When I check if a specific key exists, then do I absolutely

have
to
iterate through the items to find out its index (0 to .count - 1)?
Thank you for your comment.

"Bob Phillips" wrote:

The example in MSDN seems to show it well

Dim d ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"

Check if it exists

If d.Exists("c") Then
msg = "Specified key exists."
Else
msg = "Specified key doesn't exist."
End If

Use items

a = d.Items ' Get the items.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Create return string.
Next

Use keys

a = d.Keys ' Get the keys.
For i = 0 To d.Count -1 ' Iterate the array.
s = s & a(i) & "<BR" ' Return results.
Next


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jac Tremblay" wrote in message
...
I checked many posts about the Scripting.Dictionary and still

cannot
figure
out what the two parameters are, the key and a description or an

item
and
a
key or what? Then what is an item (or the description) and what

can it
be
used for?
If I have a description for each key, can I store it alongside the

key
and
use it later after I found its corresponding key?
My problem is as follows: I have a text file with 114000 lines. On
each
line, there is a contract number and a 3 character field (a code)

that
I
must
use to determine the template to use. Can I use the dictionary to
solve my
problem or not?
The Add method is generally used in this manner:
Dic.Add CStr(Item), Item
or
Dic.Add sh.Name, CStr(sh.Name)
Could I use
Dic.Add strContract, strCode
If yes, then how do I search and display a specific contract

number
and
its
corresponding code?
If no, should I use a Dictionary and a dynamic array instead?
Thanks



--
Jac Tremblay











All times are GMT +1. The time now is 10:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com