How do I set the Type in a variable?

  • Thread starter Thread starter Shelby
  • Start date Start date
S

Shelby

Hi,

how do I set the Type of a variable?

For example:

Dim myhash as hastable
' I want to set the Type in myhash as String or Integer or even my own
custom Structure.

Public Structure ContactUser
Public firstname as string
Public middlename as string
......
End Structure

Dim tmpuser as new ContactUser
tmpuser.firstname = "John"
tmpuser.middlename = "D"
myhash.add (1,tmpuser)
 
I may be wrong but I don't really think you can do that.

But you could create your own custom hashtable class

Class MyCustomHashTable
Inherits HashTable

and overload the properties to only accept the type of class you are
wanting and return that class.

something like this--although syntax is probably a little off

public overloads function Items(index as integer) clsMyObjectType
return ctype(list.items(index),clsMyObjectType)
end function

This should do what you are wanting and require minimal effort..

HTH,

Shane
 
Hi Shelby,

Why did you not answer on my previous answer where I said that you could do
this as an object, what is a hashtable else by holding the objects as an
arraylist which I gave you in the sample to make it as simple as posible?

Cor
 
Hi Cor,
huh? I'm sorry I don't get you.

Shelby


Cor Ligthert said:
Hi Shelby,

Why did you not answer on my previous answer where I said that you could do
this as an object, what is a hashtable else by holding the objects as an
arraylist which I gave you in the sample to make it as simple as posible?

Cor
 
I did give you a sample with an arraylist however there was no follow up and
now you ask a question in the same way as before where you said the
arraylist did not fit and now you ask it for a hashtable. What is the basic
difference between a hashtable and an arraylist others than that the first
has a key and the second an index?

And now you come back with the same question, however did not tried my
answer.

Cor
 
* "Shelby said:
how do I set the Type of a variable?

For example:

Dim myhash as hastable
' I want to set the Type in myhash as String or Integer or even my own
custom Structure.

Public Structure ContactUser
Public firstname as string
Public middlename as string
......
End Structure

Dim tmpuser as new ContactUser
tmpuser.firstname = "John"
tmpuser.middlename = "D"
myhash.add (1,tmpuser)

What doesn't work? You will have to cast the object to your custom type
when getting it back from the hashtable ('DirectCast').
 
What doesn't work? You will have to cast the object to your custom type
when getting it back from the hashtable ('DirectCast').

What I have showed with a sample in the original thread.

Cor
 
* "Cor Ligthert said:
What I have showed with a sample in the original thread.

Mhm... I think I am not the only one who missed the original thread.
 
Oh i see.
I saw your arraylist sample.
I didn't use arraylist because I need to set the Key. So I use Hashtable.

Anyway, it's different questions.
Be it I use hashtable or ArrayList, the main question here is how do I set
the Type IN the variable.
Unlike normal system Array, I can code :
Dim myArr1() as myContactUser

Thanks
Shelby
 
You do not have to set it in, you can test it when you need it, as I showed
in the sample.

If TypeOff myobject Is myContactUser then

(Again, did you try the sample or only look to it and said no I can not use
it?)

Cor
 
Hi Cor,
I do not have to set it?
If I didn't set it, it will give me an error when I run the program.

This is my code:

Dim myhash as New HashTable
Dim objuser as ContactUser

For each olitem as outlook.ContactItem in myfold.Items
objuser = New ContactUser
objuser.fullname = olitem.Fullname
objuser.company = olitem.CompanyName


' Error Occurs here.
' It says something like object is not set as an instance or whatever.
' Sorry I can't remember the exact message coz I'm at home now.
myhash.Add(olitem.EntryID, objuser)

objuser = nothing
Next


Shelby
 
Hi Shelby,

this is in my code in the sample for you, you can also have a look at a
sample I sand to Alex about an hour ago it is in top of this newsgroup it is
even more in detail.

For Each f As Object In d
If TypeOf f Is Date Then
MessageBox.Show(DirectCast(f, _
Date).DayOfWeek.ToString)
Else
'I only show one cast
MessageBox.Show(f.ToString)
End If
Next

The TypeOff and the DirectCast are the things to look for.

TypeOff looks what is the type of your object, the DirectCast tells to use
that.

Although now your question ome in the Outlook area, while I thought it where
your own build class objects.

Cor
 
* "SStory said:
What is the benefit of directcast of ctype? I can't remember.

Place the caret on 'DirectCast' in the IDE and press F1. Help gives a
good explanation when to use 'CType' and when to use 'DirectCast', and
when using one of them is preferred.
 
Back
Top