In VB .net, When i use ByRef value, always occur runtime error help me

320 Views Asked by At

I want to use .GetText(Col As Integer, Row As Integer, ByRef var As Object) As Boolean method.

But when i use this method, runtime error always occured

I want to show you my code

With fGridview 
  For iCol = 1 To .MaxCols
      For iRow = 1 To .MaxRows
        .Col = iCol
        .GetText(iCol, iRow, tmpVar)
        If tmpVar = "0" Then
         .SetText(iCol, iRow, "1")
        End If
      Next
    Next
End with

As You can see, When iCol=1, iRow=1, It is ok. But When iCol=1, iRow=2, runtime error occured. i think why this happened is because of ByRef parameter.

If i used tmpVar=nothing like this

 .GetText(iCol, iRow, tmpVar)
 If tmpVar = "0" Then
   .SetText(iCol, iRow, "1")
 End If
 tmpVar=nothing

, it works well. I don't know why this happen. Why do i have to use nothing in tmpVar, when i use ByRef parameter. i need your help plz.

and Type Missmatch error

this is vb 6.0 code

With fGridview
For iCol = 1 To .MaxCols
    For iRow = 1 To .MaxRows
        .Col = iCol
        .GetText iCol, iRow, tmpVar
        If tmpVar = "0" Then
        .SetText iCol, iRow, "1"
        End If
    Next
Next
End With

p.s My english is not good. so i don't know if you can understand my word. anyway Thank you very much

3

There are 3 best solutions below

0
On

According to this help on their site GetText requires as first parameter the ROW index and as second parameter the COLUMN index (The same is true also for SetText).
No third parameter is present. Are you mistakenly using the syntax required in VB6 instead of the one required for the NET component?

I could be wrong because I don't use this component, but perhaps you need to change your code in this way

With fGridview 
  For iCol = 1 To .MaxCols
      For iRow = 1 To .MaxRows
        .Col = iCol
        tmpVar = .GetText(iRow, iCol)
        If tmpVar = "0" Then
         .SetText(iRow, iCol, "1")
        End If
      Next
    Next
End with

Let me know if this fix your problem

2
On

At the top of your source file, add Option Strict On if you have not already done so. VB.NET is actually the name of two rather different languages which are selected based upon the "Option Strict" setting. The Option Strict Off dialect, which is unfortunately the default, was designed to facilitate porting of VB6 code, and has horrible goofy semantics which are even worse than those of VB6 (I'm not sure even its designers know all of the odd corner cases of the way different types interact); in that dialect, it is very common for code which should never work to sometimes kinda-sorta work, which sounds like what you're seeing. Use Option Strict On and many things which should never work will generate errors rather than yielding goofy behavior. One may have to add some annoying typecasts to make certain things which should work keep working (e.g. passing Double coordinates to drawing routines which take parameters of type Single) but that's a lot better than having to figure out what code is trying to do in Option Strict Off mode.

0
On

Steve awnser should be correct, but if GetText really returns a boolean, I assume you would want to do something like this

With fGridview 
  For iCol = 1 To .MaxCols
      For iRow = 1 To .MaxRows
        .Col = iCol
        If .GetText(iCol, iRow, tmpVar) Then
          If tmpVar = "0" Then
           .SetText(iCol, iRow, "1")
          End If
        End If
      Next
    Next
End with