|
-
Mar 29th, 2007, 01:51 PM
#1
Thread Starter
Lively Member
[2005] Option Stric ON, IF Statment Error and Ctype Question.
Greetings community.
I am Converting an application from Option Strict off to ON and came across two problems i cannot seem to figure out. Actualy i solved one but i think its a less than optimal fix so im looking for advice on the Ctype question a bit further down.
I am receiving an error in an If statment.
Code:
If cboClass.SelectedValue = 1 Then
getBigBrother()
End If
Debug Error: Option strict On disallows operands of type object for operator "=". Use the IS operator to test for object identity.
I rewrote the code attemting to use the IS accordingly.
Code:
Dim compval As Boolean = False
Dim num As Integer = 1
compval = cboClass.SelectedValue Is num
If compval = True Then
getBigBrother()
End If
Debug error: IS requires operands that have refrence types, but this operand has value type Integer.
SO theres my primary problem, Any advice would be apreciated..
Now my second question
I had code in my app writen like this:
Code:
Dim userIdValG as String
userIdValG = cboUserName.SelectedValue
It works fine with Strict OFF, now with Strict ON i had to modify as follows.
Code:
Dim userIdValG as String
userIdValG = Cstr(cboUserName.SelectedValue)
Is that the most efficient way to cast that object to a String.???
-
Mar 29th, 2007, 02:24 PM
#2
Thread Starter
Lively Member
Re: [2005] Option Stric ON, IF Statment Error and Ctype Question.
OK this seems to work but it seems wordy to me.
LMK what you all think on how i can make this more consice.
Code:
Dim classValue As String = CStr(cboClass.SelectedValue)
Dim num As Integer = 1
If classValue = num.ToString Then
getBigBrother()
End If
-
Mar 29th, 2007, 02:28 PM
#3
Re: [2005] Option Stric ON, IF Statment Error and Ctype Question.
1st question: with option Strict on, the IDE does not do any conversion for you behind the scene thus you have to explicitly do any required conversions yourself. That's good because it'll help reduce unexpected errors and your program also runs faster. You can get more details here:
http://msdn2.microsoft.com/en-us/library/zcd4xwzs.aspx
You should make use of the Intellisense... According to the intellisense, a combobox.SelectedValue property returns an Object and 1 is an Integer, so it's like your are comparing apples to oranges in this line (note that unless your combobox is databound and you properly set its ValueMember property, the SelectedValue will return Nothing. You should use SelectedItem instead.)
Code:
If cboClass.SelectedValue = 1 Then
Thus, to make it work, you would have to convert one of the objects to the same type of the other object... Since the type Object is too general (an Object can be anything), so instead of converting the number 1 to an object (yes, you can do this too), it's a good practice to convert an object to the more defined type, an Integer in this case... So the code should become like this
Code:
'Note that I've changed Selectedvalue to SelectedItem too
If CInt(cboClass.SelectedItem) = 1 Then
getBigBrother()
End If
Now for 2nd question: If you know exactly what type an object is truly is, you can use DirectCast, otherwise CType will work just fine... So your code will be a bit faster to run if written like this:
Code:
userIdValG = DirectCast(cboUserName.SelectedValue, String)
-
Mar 29th, 2007, 02:35 PM
#4
Thread Starter
Lively Member
Re: [2005] Option Stric ON, IF Statment Error and Ctype Question.
Thank you...
Yes the comboBox is Databound to alookup table in our database. The Display member is Text but the Value member needs to be used when comparing the selcted item to other data.
Display member (table user name, varchar)
Value member (table user ID, int)
so, selected value is what i need exactly, but thank you for the information it was most help full!! and il definely change the ones i converted to direct casts, they are simple strings all of them.
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
|