|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
GenericsWhen I do the following piece of code, it seems that it's illegal ton
typecast a string into a generic result: Private Function GetTypeName(Of t)(ByVal FieldName As String) As t Dim MyResult As t If TypeOf MyResult Is String Then MyResult = CType("Test", t) Else ' Some other stuff End If Return MyResult End Function What can I do to solve the issue? Hi, interesting...
The compiler : - knows that generally speaking you can't guarantee it will *always* work - doesn't care that you tested this in your code before doing this so you are pretty sure it will works but the compiler doesn't know this... So it issues a warning to tell you it can fail... You can use TryCast instead... Also I made a quick test and you may have to use : If GetType(T) Is GetType(String) Then rather than testing MyResult... (likely because under the hood myresult is of type T, not of type String even when you make T being String) so you need to check if the type t is the type string but I begin to be beyond my own knowlegde here). Hopefully someone else will tell us more... As a side note telling what you are trying to do could be also a good idea (not obvious from you code plus sometimes one has a whole different approach to something). What triggers this is that you have to check for a type. It doesn't seem something usual when suing generics... -- Patrice "Michel Racicot" <mraci***@hotmail.com> a écrit dans le message de groupe de discussion : uK$0EaT5JHA.6***@TK2MSFTNGP03.phx.gbl...Show quoteHide quote > When I do the following piece of code, it seems that it's illegal ton > typecast a string into a generic result: > > Private Function GetTypeName(Of t)(ByVal FieldName As String) As t > Dim MyResult As t > > If TypeOf MyResult Is String Then > MyResult = CType("Test", t) > > Else > > ' Some other stuff > End If > Return MyResult > End Function > > > What can I do to solve the issue? > > What I'm trying to accomplish is a function that will handle the DBNulls
values and some other casts correctly according to our business logic. For sure, I can create various overloads of the function for different datatypes, but I think it will be better with generics. for instance: EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID")) Name = HandleNullValue(of String)(MyRow("LAST_NAME")) previously, we had: EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID")) Name=NullToString(MyRow("LAST_NAME")) Show quoteHide quote "Patrice" <http://www.chez.com/scribe/> wrote in message news:48C959C4-6F08-448C-96D3-5C12B512AEC3@microsoft.com... > Hi, interesting... > > The compiler : > - knows that generally speaking you can't guarantee it will *always* work > - doesn't care that you tested this in your code before doing this so you > are pretty sure it will works but the compiler doesn't know this... > > So it issues a warning to tell you it can fail... > > You can use TryCast instead... > > Also I made a quick test and you may have to use : > If GetType(T) Is GetType(String) Then > > rather than testing MyResult... > > (likely because under the hood myresult is of type T, not of type String > even when you make T being String) so you need to check if the type t is > the type string but I begin to be beyond my own knowlegde here). Hopefully > someone else will tell us more... > > As a side note telling what you are trying to do could be also a good idea > (not obvious from you code plus sometimes one has a whole different > approach to something). What triggers this is that you have to check for a > type. It doesn't seem something usual when suing generics... > > -- > Patrice > > > > "Michel Racicot" <mraci***@hotmail.com> a écrit dans le message de groupe > de discussion : uK$0EaT5JHA.6***@TK2MSFTNGP03.phx.gbl... >> When I do the following piece of code, it seems that it's illegal ton >> typecast a string into a generic result: >> >> Private Function GetTypeName(Of t)(ByVal FieldName As String) As t >> Dim MyResult As t >> >> If TypeOf MyResult Is String Then >> MyResult = CType("Test", t) >> >> Else >> >> ' Some other stuff >> End If >> Return MyResult >> End Function >> >> >> What can I do to solve the issue? >> >> > Have you seen Nullable types ?
See : http://msdn.microsoft.com/en-us/library/ms235245.aspx -- Patrice "Michel Racicot" <mraci***@hotmail.com> a écrit dans le message de groupe de discussion : OBW3C5T5JHA.1***@TK2MSFTNGP06.phx.gbl...Show quoteHide quote > What I'm trying to accomplish is a function that will handle the DBNulls > values and some other casts correctly according to our business logic. > For sure, I can create various overloads of the function for different > datatypes, but I think it will be better with generics. > > for instance: > > EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID")) > Name = HandleNullValue(of String)(MyRow("LAST_NAME")) > > previously, we had: > > EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID")) > Name=NullToString(MyRow("LAST_NAME")) > > "Patrice" <http://www.chez.com/scribe/> wrote in message > news:48C959C4-6F08-448C-96D3-5C12B512AEC3@microsoft.com... >> Hi, interesting... >> >> The compiler : >> - knows that generally speaking you can't guarantee it will *always* work >> - doesn't care that you tested this in your code before doing this so you >> are pretty sure it will works but the compiler doesn't know this... >> >> So it issues a warning to tell you it can fail... >> >> You can use TryCast instead... >> >> Also I made a quick test and you may have to use : >> If GetType(T) Is GetType(String) Then >> >> rather than testing MyResult... >> >> (likely because under the hood myresult is of type T, not of type String >> even when you make T being String) so you need to check if the type t is >> the type string but I begin to be beyond my own knowlegde here). >> Hopefully someone else will tell us more... >> >> As a side note telling what you are trying to do could be also a good >> idea (not obvious from you code plus sometimes one has a whole different >> approach to something). What triggers this is that you have to check for >> a type. It doesn't seem something usual when suing generics... >> >> -- >> Patrice >> >> >> >> "Michel Racicot" <mraci***@hotmail.com> a écrit dans le message de groupe >> de discussion : uK$0EaT5JHA.6***@TK2MSFTNGP03.phx.gbl... >>> When I do the following piece of code, it seems that it's illegal ton >>> typecast a string into a generic result: >>> >>> Private Function GetTypeName(Of t)(ByVal FieldName As String) As t >>> Dim MyResult As t >>> >>> If TypeOf MyResult Is String Then >>> MyResult = CType("Test", t) >>> >>> Else >>> >>> ' Some other stuff >>> End If >>> Return MyResult >>> End Function >>> >>> >>> What can I do to solve the issue? >>> >>> >> > > Michel Racicot wrote:
Show quoteHide quote > What I'm trying to accomplish is a function that will handle the What is currently done inside NullToInteger, NullToString etc?> DBNulls values and some other casts correctly according to our > business logic. For sure, I can create various overloads of the > function for different datatypes, but I think it will be better with > generics. > > for instance: > > EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID")) > Name = HandleNullValue(of String)(MyRow("LAST_NAME")) > > previously, we had: > > EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID")) > Name=NullToString(MyRow("LAST_NAME")) What do you expect from HandleNullValue? Maybe you nedd something like this: Shared Function HandleNullValue(Of T)(ByVal value As Object) As T If value Is DBNull.Value Then '... Else Return DirectCast(value, T) End If End Function Armin Casting objects to T is actually a pretty good idea!!!
It solved the problem Show quoteHide quote "Armin Zingler" <az.nospam@freenet.de> wrote in message news:ulvM11U5JHA.5728@TK2MSFTNGP03.phx.gbl... > Michel Racicot wrote: >> What I'm trying to accomplish is a function that will handle the >> DBNulls values and some other casts correctly according to our >> business logic. For sure, I can create various overloads of the >> function for different datatypes, but I think it will be better with >> generics. for instance: >> >> EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID")) >> Name = HandleNullValue(of String)(MyRow("LAST_NAME")) >> >> previously, we had: >> >> EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID")) >> Name=NullToString(MyRow("LAST_NAME")) > > > What is currently done inside NullToInteger, NullToString etc? > What do you expect from HandleNullValue? > > Maybe you nedd something like this: > > Shared Function HandleNullValue(Of T)(ByVal value As Object) As T > If value Is DBNull.Value Then > '... > Else > Return DirectCast(value, T) > End If > End Function > > > > > Armin Michel Racicot wrote:
Show quoteHide quote > When I do the following piece of code, it seems that it's illegal ton A String can not be converted to every type. What if T is a Form? > typecast a string into a generic result: > > Private Function GetTypeName(Of t)(ByVal FieldName As String) As t > Dim MyResult As t > > If TypeOf MyResult Is String Then > MyResult = CType("Test", t) > > Else > > ' Some other stuff > End If > Return MyResult > End Function > > > What can I do to solve the issue? dim MyResult As Form MyResult = ctype("test", Form) How should this work? Armin Hi Michael
Why don't you just analyse the type of T again? As Armin said, it won't support every type, so you shouldn't support every type, that is poor coding. ... If TypeOf MyResult is String Then '//Check that the string can be converted into type 't' here before doing so, if not, throw an exception '//You could do this by making a small private method that accepts a type and returns a Boolean '//to let you know if it can be typecast from a string Else ... Also, personally, I'd use the Parse methods... Boolean.Parse(stringvalue) Integer.Parse(stringvalue) Single.Parse(stringvalue) etc. etc. Nick Show quoteHide quote "Michel Racicot" <mraci***@hotmail.com> wrote in message news:uK$0EaT5JHA.6136@TK2MSFTNGP03.phx.gbl... > When I do the following piece of code, it seems that it's illegal ton > typecast a string into a generic result: > > Private Function GetTypeName(Of t)(ByVal FieldName As String) As t > Dim MyResult As t > > If TypeOf MyResult Is String Then > MyResult = CType("Test", t) > > Else > > ' Some other stuff > End If > Return MyResult > End Function > > > What can I do to solve the issue? > > > for example////
Public Function canBeTypeCastFromString() As Boolean Select Case GetType(t).ToString() Case GetType(Integer).ToString(), _ GetType(Boolean).ToString(), _ GetType(Single).ToString() Return(True) Case Else Return(false) End Select End Function If TypeOf MyResult Is String Then if(canBeTypeCastFromString()) MyResult = CType("Test", t) else '//fall over and choke end if Else Show quoteHide quote "nak" <a@a.com> wrote in message news:B721889F-E571-4716-988D-8F79A8116D63@microsoft.com... > Hi Michael > > Why don't you just analyse the type of T again? > > As Armin said, it won't support every type, so you shouldn't support > every type, that is poor coding. > > ... > If TypeOf MyResult is String Then > '//Check that the string can be converted into type 't' here before > doing so, if not, throw an exception > '//You could do this by making a small private method that accepts > a type and returns a Boolean > '//to let you know if it can be typecast from a string > Else > ... > > Also, personally, I'd use the Parse methods... > > Boolean.Parse(stringvalue) > Integer.Parse(stringvalue) > Single.Parse(stringvalue) > > etc. etc. > > Nick > > "Michel Racicot" <mraci***@hotmail.com> wrote in message > news:uK$0EaT5JHA.6136@TK2MSFTNGP03.phx.gbl... >> When I do the following piece of code, it seems that it's illegal ton >> typecast a string into a generic result: >> >> Private Function GetTypeName(Of t)(ByVal FieldName As String) As t >> Dim MyResult As t >> >> If TypeOf MyResult Is String Then >> MyResult = CType("Test", t) >> >> Else >> >> ' Some other stuff >> End If >> Return MyResult >> End Function >> >> >> What can I do to solve the issue? >> >> >> > |
|||||||||||||||||||||||