Results 1 to 6 of 6

Thread: Eeeek Option Strict On [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Resolved 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:
    1. For x = myitems.Count To 1 Step -1
    2.                 objSafeMail.item = myitems(x) 'errors here
    3.                 Me.txttotalemail.Text = CStr(CInt(Me.txttotalemail.Text) + 1)
    4.                 Me.txtcurrmess.Text = myitems(x).Subject 'errors here
    5.                 mtype = emailtype()
    6.  
    7.                 If typeemail(mtype) = True Then
    8.                     myitems(x).Delete() 'errors here
    9.  
    10.                 Else
    11.                     myitems(x).Move(destfolder) 'errors here
    12.  
    13.                 End If
    14.             Next x
    Last edited by Oliver1; Dec 15th, 2005 at 03:49 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim myArrayList As New ArrayList
    2.  
    3. myArrayList.Add("Hello World")
    4.  
    5. 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:
    1. Dim stringLength As Integer = CStr(myArrayList(0)).Length
    2. Dim stringLength As Integer = CType(myArrayList(0), String).Length
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Eeeek Option Strict On

    Ah, I see.

    VB Code:
    1. For x = myitems.Count To 1 Step -1
    2.                 CType(objSafeMail, Redemption.SafeMailItem).Item = myitems(x)
    3.                 Me.txttotalemail.Text = CStr(CInt(Me.txttotalemail.Text) + 1)
    4.                 Me.txtcurrmess.Text = CType(myitems(x), Outlook.MailItem).Subject
    5.  
    6.                 mtype = emailtype()
    7.  
    8.                 If typeemail(mtype) = True Then
    9.                     CType(myitems(x), Outlook.MailItem).Delete()
    10.  
    11.                 Else
    12.                     CType(myitems(x), Outlook.MailItem).Move(destfolder)
    13.  
    14.                 End If
    15.             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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim myItem As Outlook.MailItem = CType(myitems(x), Outlook.MailItem)
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    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
  •  



Click Here to Expand Forum to Full Width