|
-
Dec 14th, 2005, 07:37 AM
#1
Thread Starter
Hyperactive Member
Eeeek Option Strict On [Resolved]
Just reading a post about Option Strict On, not being on by default, so thought I know I'll go into my project and switch it ON.
Thinking to myself that It'll all be fine, low and behold I get about a trillion error messages. Anyway most are easy peasy. Am a little stuck on the below.
I do a lot of interaction in my project with outlook. How would I do the below with option strict on.
It says option strict disallows late binding, which is were I get stuck, as how can I pre assign the below before runtime, as I don't know how many e-mail will be in the inbox, if any.
Anyway very confused.
VB Code:
For x = myitems.Count To 1 Step -1
objSafeMail.item = myitems(x) 'errors here
Me.txttotalemail.Text = CStr(CInt(Me.txttotalemail.Text) + 1)
Me.txtcurrmess.Text = myitems(x).Subject 'errors here
mtype = emailtype()
If typeemail(mtype) = True Then
myitems(x).Delete() 'errors here
Else
myitems(x).Move(destfolder) 'errors here
End If
Next x
Last edited by Oliver1; Dec 15th, 2005 at 03:49 AM.
-
Dec 14th, 2005, 07:57 AM
#2
Re: Eeeek Option Strict On
The issue is almost certainly that myitems(x) is returning an Object reference and you are trying to use it as though it is some other more specific type. The object itself may be a particular type but the reference you are using is of type Object. Now, the Object class has no Subject, Delete or Move members, so you need to cast it as the specific type that it is, either with DirectCast or CType, in order to access those members. I don't know what the types are that you are dealing with, so let me give you an example using an ArrayList and a String:
VB Code:
Dim myArrayList As New ArrayList
myArrayList.Add("Hello World")
Dim stringLength As Integer = myArrayList(0).Length
The last line would give the same error you are getting because indexing an ArrayList returns an Object reference and the Object class has no Length property. The object must be cast as a String in order to access members of the String class:
VB Code:
Dim stringLength As Integer = CStr(myArrayList(0)).Length
Dim stringLength As Integer = CType(myArrayList(0), String).Length
Dim stringLength As Integer = DirectCast(myArrayList(0), String).Length
The result of all three of these lines is the same, although the mechanism is a little different. With the String type you can use the specific CStr function. This obviously won't be available to you, so you would need to use CType or DirectCast. If the object is a reference type (read: class) and you know for sure that it is a specific type then you should use DirectCast. Otherwise, use CType.
Edit: Note that you can set the default for Option Strict to On for all future projects in the Options for the IDE.
-
Dec 14th, 2005, 10:49 AM
#3
Thread Starter
Hyperactive Member
Re: Eeeek Option Strict On
Ah, I see.
VB Code:
For x = myitems.Count To 1 Step -1
CType(objSafeMail, Redemption.SafeMailItem).Item = myitems(x)
Me.txttotalemail.Text = CStr(CInt(Me.txttotalemail.Text) + 1)
Me.txtcurrmess.Text = CType(myitems(x), Outlook.MailItem).Subject
mtype = emailtype()
If typeemail(mtype) = True Then
CType(myitems(x), Outlook.MailItem).Delete()
Else
CType(myitems(x), Outlook.MailItem).Move(destfolder)
End If
Next x
Does the above look correct and I assume this means more work, as the object in question isn't all ways a mailitem, as it can be a report item or such like.
Oh well might have to leave option explict off until I have some more time.
Why can't someone invent a computer that reads your mind finds out what you want, and stuff you haven't thought of yet, and then does it for you. Would make my job easier.
-
Dec 14th, 2005, 10:59 AM
#4
Re: Eeeek Option Strict On
code is about 10% making the programing do what it is supposed to, and 90% preventing the user from doing what they shouldn't do, but will do.
-
Dec 14th, 2005, 11:48 PM
#5
Re: Eeeek Option Strict On
That code does look essentially correct, but I will say a couple of things. Instead of casting the same reference over and over again, you would just assign the cast reference to a new variable and use that variable everywhere it was needed:
VB Code:
Dim myItem As Outlook.MailItem = CType(myitems(x), Outlook.MailItem)
Me.txtcurrmess.Text = myItem.Subject
Also, you say that the item will not always be an Outlook.MailItem. Do the other types it may be also have Subject, Delete and Move members? If so, do they all inherit those members from a common base class? If they do, you could then cast the reference to that base class instead, which will then account for any of the possible types.
It does mean a bit more work in the development phase, although once you get used to it you do it all so automatically you don't even notice it. Intellisense does some of the work for you anyway, and even more so in 2005.
-
Dec 15th, 2005, 03:49 AM
#6
Thread Starter
Hyperactive Member
Re: Eeeek Option Strict On
Thanks, for your help, is all a bit clearer now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|