[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?
Re: [02/03] Option Strict again
try this
vb Code:
Dim pin() As Byte = CByte(RegistryKey.GetValue("PIN"))
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'
Re: [02/03] Option Strict again
try it this way
vb Code:
Dim pin As Byte = CByte(RegistryKey.GetValue("PIN"))
Re: [02/03] Option Strict again
But pin is a one dimensional array of bytes
Re: [02/03] Option Strict again
Quote:
Originally Posted by .paul.
try it this way
vb Code:
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:
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.
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
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.