Results 1 to 8 of 8

Thread: [RESOLVED] [02/03] Option Strict again

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Resolved [RESOLVED] [02/03] Option Strict again

    I have code:-

    Code:
     
     Dim pin() As Byte = RegistryKey.GetValue("PIN")
    that works OK with Option Stict off.

    When I turm Option strict on I get

    Code:
    Option Strict On disallows implicit conversions from 'System.Object' to '1-dimensional array of Byte'.
    The Registry Key is Reg Type REG_BINARY

    How should I be coding this to cope with Option Strict on?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [02/03] Option Strict again

    try this

    vb Code:
    1. Dim pin() As Byte = CByte(RegistryKey.GetValue("PIN"))

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [02/03] Option Strict again

    That give an error:-
    Code:
    A value of type Byte cannot be converted to '1 dimensional array of byte'

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: [02/03] Option Strict again

    try it this way

    vb Code:
    1. Dim pin As Byte = CByte(RegistryKey.GetValue("PIN"))

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [02/03] Option Strict again

    But pin is a one dimensional array of bytes

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

    Re: [02/03] Option Strict again

    Quote Originally Posted by .paul.
    try it this way

    vb Code:
    1. Dim pin As Byte = CByte(RegistryKey.GetValue("PIN"))
    No don't try it that way. If the original code works with Option Strict Off then the value retrieved from the Registry is obviously a Byte array, so casting it as type Byte is not going to work. You have to declare the variable as type Byte array and cast the value as type Byte array:
    vb.net Code:
    1. Dim pin() As Byte = DirectCast(RegistryKey.GetValue("PIN"), Byte())
    It's very simple. With Option Strict On strict typing rules are enforced. You cannot assign anything to a variable that is not of the same type, or at least of a type that it cannot be converted to without a narrowing conversion. A narrowing conversion is any type conversion that could result in a loss of data.

    In this case your assigning an Object reference to a Byte() variable. You know it's an object reference because the MSDN documentation and Intellisense both tell you that Registry.GetValue returns type Object. The reason for that is that it has to be able to return any type of value that can be stored in the Registry. The only common base type for all possible values is Object.

    So, you know that GetValue is going to return a Byte array but the compiler doesn't know that. You have to tell the compiler that the value will be a Byte array by performing a cast. The compiler then knows that it's OK to assign the result to a Byte array variable.
    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: [02/03] Option Strict again

    Thanks Guys

    That works with option strict off and the compiler does not complain about the statement now with option strick on. Unfortunately I have to get rid of umpteen other option strick relate errors before I can run the code.

    I was getting close but trying to use ctype instead of directcast.

    jmcilhinney - once again- thanks for your repeated help

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

    Re: [RESOLVED] [02/03] Option Strict again

    Your code will actually run faster now with Option Strict On. Everywhere you had to fix an error is a place where the execution of your code was slowed down by extra type checking. Your code will also be less susceptible to run time errors because many of the issues will have been caught at design time instead.

    As to CType and DirectCast, anywhere you can use DirectCast you can use CType too. DirectCast is more efficient though. That said, CType will also perform certain conversions that DirectCast cannot. Basically, always use DirectCast unless you need to use CType specifically.
    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

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