Results 1 to 7 of 7

Thread: [RESOLVED] Option Strict Problems

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Resolved [RESOLVED] Option Strict Problems

    Good day everyone.

    I have:

    Code:
    Debug.DebugTextBox.Text = ("Current Guest: " + e.Params("user"))
    And

    Code:
    Debug.DebugTextBox.Text = (("Guest Error: (" + e.Params("errorCode") & "): ") + e.Params("errorMessage"))
    Error BC30038 Option Strict On prohibits operands of type Object for operator '+'.

    Also, I'm trying to write the logged in user to another form (debug.debugtextbox.text), but I want to put it in new lines. As of now, for each output, it just erases what's currently in the textbox and then updates with the new log. Something like Environtment.NewLine(), but that doesn't seem to work for a separate form.

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Option Strict Problems

    Quote Originally Posted by Ritzky View Post
    Error BC30038 Option Strict On prohibits operands of type Object for operator '+'.
    In VB,always use the & Operator to concatenate strings, never "+".


    Quote Originally Posted by Ritzky View Post
    Also, I'm trying to write the logged in user to another form (debug.debugtextbox.text), but I want to put it in new lines. As of now, for each output, it just erases what's currently in the textbox and then updates with the new log. Something like Environtment.NewLine(), but that doesn't seem to work for a separate form.
    Does your TextBox have its MultiLine Property set? If so, the following is one way to do what you require:
    Code:
    Debug.DebugTextBox.AppendText("Your String" & Environment.NewLine)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: Option Strict Problems

    Replacing it with a "&" sign leaves it with the same error. (Still Option Strict Conversion Problem).

    The AppendText worked or my textbox, though.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Option Strict Problems

    e.Params returns an object of type Object... what you want is the string representation of that... interesting thing about the Object type, it comes with a .ToString method that will return the string representation of that object.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Option Strict Problems

    Quote Originally Posted by Ritzky View Post
    Good day everyone.

    I have:

    Code:
    Debug.DebugTextBox.Text = ("Current Guest: " + e.Params("user"))
    And

    Code:
    Debug.DebugTextBox.Text = (("Guest Error: (" + e.Params("errorCode") & "): ") + e.Params("errorMessage"))
    Error BC30038 Option Strict On prohibits operands of type Object for operator '+'.

    Also, I'm trying to write the logged in user to another form (debug.debugtextbox.text), but I want to put it in new lines. As of now, for each output, it just erases what's currently in the textbox and then updates with the new log. Something like Environtment.NewLine(), but that doesn't seem to work for a separate form.
    vb Code:
    1. Debug.DebugTextBox.Text = String.Format("Current Guest: {0}", e.Params("user"))
    vb Code:
    1. Debug.DebugTextBox.Text = String.Format("Guest Error: ({0}): {1}", e.Params("errorCode"), e.Params("errorMessage"))
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Option Strict Problems

    Quote Originally Posted by Ritzky View Post
    Replacing it with a "&" sign leaves it with the same error. (Still Option Strict Conversion Problem)

    My apologies. I had always assumed & worked with Objects that boxed String types, or types that could be converted to Strings. Apparently it disallows any operand that is an Object Type, full stop. I presumably haven't noticed this behaviour before as I normally always cast the Object back to its underlying Type before working with it.

    Live and learn

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Option Strict Problems

    Quote Originally Posted by Inferrd View Post
    My apologies. I had always assumed & worked with Objects that boxed String types, or types that could be converted to Strings. Apparently it disallows any operand that is an Object Type, full stop. I presumably haven't noticed this behaviour before as I normally always cast the Object back to its underlying Type before working with it.

    Live and learn
    Technically, the answer is "it only allows widening implicit conversions or explicit conversions". That's something we have to unravel, but it's useful to know because if you're just getting used to Option Strict it can seem like it chooses randomly.

    An "implicit conversion" is when you try to convert a type without using any casts like CType() or CDbl(), or a conversion method like ToString(). These are implicit conversions:
    Code:
    Dim value As Integer = 14.7 ' Converts from Double to Integer
    Dim otherWay As Double = 10 ' Integer to Double
    Dim another As Byte = 8 ' This is actually Integer to Byte and sometimes frustrating!
    Dim badIdea As Double = 12 - "14" ' Implicitly converts String to Integer, then performs subtraction
                                      ' returning an Integer, then implicitly converts to Double
    You probably don't consider the first few to be "type conversions" at all! That's why implicit conversions are supported, if you had to express all of those as casts you'd waste a lot of time on code clutter. But not all of those conversions are legal with Option Strict. Why?

    That's where "narrowing" conversions appear. It's useful to think of data types as having a "size", which is just "How many values can they represent?" Integers can represent 4 billion values, Bytes can hold 255. So Integer is "bigger" than Byte. More importantly, every Byte is also a value that Integer can represent, but Integer has many values such as -145 that Byte cannot.

    So in terms of conversions, we say Integer is "wider" than Byte. Byte to Integer is a "widening" conversion, because we are converting to a type that can not only represent all Byte values, but has more. But Integer to Byte is a "narrowing" conversion, because there are Integers that cannot be represented as Byte.

    So if Option Strict is on, we are forced to use explicit casts when we want to make narrowing conversions:
    Code:
    Dim value As Integer = CInt(14.7) ' Double is narrowed explicitly
    Dim otherWay As Double = 10 ' But Integer is widened implicitly
    Dim another As Byte = 8 ' This is a PITA in C# but VB seems to recognize it.
    Dim badIdea As Double = 12 - Integer.Parse("14") ' We had to parse String to Integer before the subtraction
    I'm sort of confused by that Byte conversion. Technically '8' is an Integer literal, and Intellisense confirms. Technically it's not safe to narrow Integer to Byte. It looks like the compiler's smart enough to do range checking and allow it. Feel blessed, C# makes this a pain.

    So, the reason Object needed a string conversion is Object is technically the widest type of all: it can be anything! That means converting from Object to anything else is always considered "narrowing", even when you can prove that the only thing the Object could be is one particular type. Option Strict makes you be explicit with your dangerous casts, in the hopes that you think harder about them.

    Now you know!
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

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