-1

I Have below code which gives me error "Option Strict On disallows narrowing from type "Option Strict On disallows narrowing from type 'Object' to type 'String' in copying the value of 'ByRef' parameter 'varExpression' back to the matching argument." Please note that I don't want to turn option strict OFF. below is the code.

 clsDataElemMaintRowXMLDataSet.SetFieldText(TAG_FLD_DEM_AMT, IIf(IsNumeric(strAmt), IIf(blnNegativeAmt, "-", "+"), EMPTY_STRING) &                                                           
Format_Renamed(strAmt, MASK_CURRENCY))

these are the declaration for the above code.

    Dim clsDataElemMaintRowXMLDataSet As "someclass object"
   Public Sub SetFieldText(strFieldName As String, strFieldText As String)
   Private Const TAG_FLD_DEM_AMT As String = "DEMAmt"
ByVal blnNegativeAmt As Boolean
 Public Const MASK_CURRENCY As String = "$#,##0.00"
 ByVal strAmt As String
  ByVal blnNegativeAmt As Boolean

   Public Function Format_Renamed(ByRef varExpression As Object, ByRef 
    Optional strFormat As String = "", ByRef Optional vbFirstDayOfWeek As 
   FirstDayOfWeek = FirstDayOfWeek.Sunday, ByRef Optional 
    vbFirstWeekOfYear As FirstWeekOfYear = FirstWeekOfYear.Jan1) As 
    String
vandy
  • 63
  • 8
  • I don't see varExpression in any of the code you posted. Post the line with the error and the declaration for the variable with the error. – UnhandledExcepSean Oct 24 '18 at 15:24
  • Can you post the actual code? You have pasted a mess of lines which can't exist on their own. – djv Oct 24 '18 at 15:28
  • edited original question. hope that helps. – vandy Oct 24 '18 at 15:29
  • 2
    Why would "ByRef varExpression As Object" not be declared as "ByRef varExpression As String"? Oh, and good for using option strict; I frankly don't think it should be an option. – UnhandledExcepSean Oct 24 '18 at 15:30
  • I changed ByRef varExpression As String". after that I got error "Option Strict On prohibits operands of type Object for operator '&'." – vandy Oct 24 '18 at 15:38
  • You really need to post the calling code as well. – UnhandledExcepSean Oct 24 '18 at 15:41
  • I posted all calling code. only left is "IsNumeric" which is from Public Module Information and name space is Microsoft.VisualBasic . – vandy Oct 24 '18 at 15:44
  • As you tagged this [vb6-migration], you need to realize that `ByRef` was the default argument passing mechanism and it likely not appropriate to use `ByRef` in the VB.Net version. Also, VB6 variants are converted to .Net `System.Object` type. This also causes issues and you may be better served by rewriting the method as a generic to handle the various types that a variant type could represent. The main issue is you need to understand what the VB6 code did and implement that dame functionality in proper VB.Net syntax; don't assume trans-literal code conversion will yield the same outcome. – TnTinMn Oct 24 '18 at 15:45
  • try EMPTY_STRING).ToString() – UnhandledExcepSean Oct 24 '18 at 16:21

1 Answers1

0

I got the answer. Basically, have to remove & operator and need to concatinate string using String.Concat. below is the code.

clsDataElemMaintRowXMLDataSet. 
   SetFieldText(TAG_FLD_DEM_AMT, 
                String.Concat(IIf(IsNumeric(strAmt), 
                              IIf(blnNegativeAmt, "-", "+"), 
                            EMPTY_STRING), Format_Renamed(CObj(strAmt), 
                MASK_CURRENCY))) 
jmoreno
  • 12,752
  • 4
  • 60
  • 91
vandy
  • 63
  • 8
  • Actually the answer most likely that you should never use IIF, instead you should be using IF, which would cause all of your strings to be strings, and you'd not be having the problem with using & and an object. – jmoreno Oct 30 '18 at 01:38