|
-
Aug 28th, 2007, 11:42 AM
#1
Thread Starter
Lively Member
[RESOLVED] [2005] Implicit conversion - HELP!!!
I have an enumerated list of integers, which I'm trying to read from a SqlDataReader and pass into a method as an argument.
Because I have the "Option Strict On", the IDE is complaining that I must convert the value to the enumerated value (the list is already declared as an integer!?!??!
My enumerated list:
Code:
Public Enum StockItemType As Integer
INGREDIENT = 0
PACKAGING = 1
UTENSIL = 2
End Enum
Reading in the value:
Code:
While rdrSQL.Read()
Me.Items.Add(New StockItem(.GetInt32(0), .GetString(1), .GetInt32(2)))
End While
My method (instantiates a new class object):
Code:
Public Sub New(ByVal ID As Integer, Optional ByVal Name As String = "", Optional ByVal StockType As StockItemType = StockItemType.INGREDIENT)
Initialize(ID, Name)
End Sub
I've tried CInt, but that still prompts the error. The database value has been changed from a TinyInt value to an Int, just to see if it would resolve the problem but unfortunately, not so.
Any ideas? Any help would be greatly appreciated.
P.S. Advice on having the database value set to a TinyInt value, and still having the enumerated list construct and method/argument still working would also be of great help!
Thanks.
-
Aug 28th, 2007, 11:59 AM
#2
Re: [2005] Implicit conversion - HELP!!!
I don't see where you are using StockItemType.
-tg
-
Aug 28th, 2007, 12:01 PM
#3
Thread Starter
Lively Member
Re: [2005] Implicit conversion - HELP!!!
Please check again.
I initially had the wrong constructor (I have 2 constructior methods declared) method on the example, but I re-edited the message.
-
Aug 28th, 2007, 12:28 PM
#4
Re: [2005] Implicit conversion - HELP!!!
1) It's recommended that you don't use optional parameters in .NET, but rather overloaded functions instead (one with the parameter, one without). Not mandatory, not required, just highly suggested.
2) Because your parameter type is of type StockItemType the value passed in must be of the same type - Even though both are of the same BASE type (integer), the enumerated type becomes a super type.
Solution:
1) change the parameter type back to Integer. And leave the rest as it is. Simple, easy, but you lose the intellisense when typing in the calls to the function
2) CTYpe the value from the Reader to type StockItemType. This route is probably safer, and would be safer still if the value was extracted to a varaible of type StockItemType first, within a Try Catch, that way if in the off chance an invalid value is passed (say, like 4), you can catch that upfront.
-=tg
-
Aug 28th, 2007, 12:34 PM
#5
Thread Starter
Lively Member
Re: [2005] Implicit conversion - HELP!!!
Thanks techgnome.
The 2nd solution worked a treat and I think is a better alternative to your 1st as it preserves the data integrity to some extent.
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
|