Select to view content in your preferred language

Migrating VBA Function to vb.net

2941
9
11-24-2013 10:09 AM
HushamHassan
Regular Contributor
Hello,
I am facing problem migrating this function from VBA  to vb.net.  I do appreciate  your help. I am using 10.1 and vs2010

Public Function GenerateClassBreaks(pFeatLayer As IFeatureLayer, sValFieldName As String, lNClass As Long, sClassification As String) As Variant

    ' classifies data using IClassify2 and Quantile classification,
    ' then creates ClassBreaksRenderer using these breaks,
    ' and assigns to first layer in the map
    
    On Error GoTo ErrorHandler
    
    Dim pFClass As IFeatureClass
    Dim pFeature As IFeature
    Dim pFCursor As IFeatureCursor
    Dim pTable As ITable
    Dim pClassifyGEN As IClassifyGEN
    Dim pTableHistogram As ITableHistogram
    Dim pHistogram As IHistogram
    Dim frqs As Variant, xVals As Variant
    Dim i As Integer
    
    Set pFClass = pFeatLayer.FeatureClass
    Set pFCursor = pFClass.Search(Nothing, False)
    Set pFeature = pFCursor.NextFeature
    Set pTable = pFClass
    'Set pClassifyGEN = New Quantile
    Select Case sClassification
        Case "Equal Interval"
            Set pClassifyGEN = New EqualInterval
        Case "Natual Breaks"
            Set pClassifyGEN = New NaturalBreaks
        Case "Quantile"
            Set pClassifyGEN = New Quantile
        Case "Standard Deviation"
            Set pClassifyGEN = New StandardDeviation
    End Select
    'Set pClassifyGEN = New EqualInterval
    Set pTableHistogram = New TableHistogram
    Set pHistogram = pTableHistogram
    pTableHistogram.Field = sValFieldName
    
    ' matches renderer field
    Set pTableHistogram.Table = pTable
    pHistogram.GetHistogram xVals, frqs
    pClassifyGEN.Classify xVals, frqs, lNClass
    
    ' use "iNClass" number of classes
    GenerateClassBreaks = pClassifyGEN.ClassBreaks
    
    GoTo Endproc
    
ErrorHandler:
    MsgBox Err.Description, vbInformation, "GenerateClassBreaks"
    
Endproc:

    Set pFClass = Nothing
    Set pFeature = Nothing
    Set pFCursor = Nothing
    Set pTable = Nothing
    Set pClassifyGEN = Nothing
    Set pTableHistogram = Nothing
    Set pHistogram = Nothing
    
End Function

Regards
0 Kudos
9 Replies
AlexanderGray
Honored Contributor
Perhaps you could describe your problem.
0 Kudos
BruceNielsen
Frequent Contributor
A couple of hints:

  1. You no longer need to use the 'Set' keyword

  2. You can combine the 'Dim' and 'Set' statements on one line

  3. Dim pFClass As IFeatureClass
    Set pFClass = pFeatLayer.FeatureClass

    can now be expressed as
    Dim pFClass As IFeatureClass = pFeatLayer.FeatureClass

  4. Use a Try statement to trap errors. You can see the errors with ex.ToString

0 Kudos
HushamHassan
Regular Contributor
agray1, bruce.nielsen
Thank you,  here is my VB.net  code..
Public Function GenerateClassBreaks(pFeatLayer As IFeatureLayer, sValFieldName As String, lNClass As Integer, sClassification As String) As Object
        ' classifies data using IClassify2 and Quantile classification,
        ' then creates ClassBreaksRenderer using these breaks,
        ' and assigns to first layer in the map

        Try
            Dim pFClass As IFeatureClass

            Dim pFeature As IFeature

            Dim pFCursor As IFeatureCursor

            Dim pTable As ITable

            Dim pClassifyGEN As IClassifyGEN

            Dim pTableHistogram As ITableHistogram

            Dim pHistogram As IHistogram
            Dim frqs, xVals As Object
            Dim i As Integer


            pFClass = pFeatLayer.FeatureClass

            pFCursor = pFClass.Search(Nothing, False)
            pFeature = pFCursor.NextFeature
            pTable = pFClass
            'Set pClassifyGEN = New Quantile
            Select Case sClassification
                Case "Equal Interval"
                    pClassifyGEN = New EqualInterval()
                Case "Natual Breaks"
                    pClassifyGEN = New NaturalBreaks()
                Case "Quantile"
                    pClassifyGEN = New Quantile()
                Case "Standard Deviation"
                    pClassifyGEN = New StandardDeviation()
            End Select
            'Set pClassifyGEN = New EqualInterval
            pTableHistogram = New TableHistogram()
            pHistogram = pTableHistogram
            pTableHistogram.Field = sValFieldName

            ' matches renderer field
            pTableHistogram.Table = pTable
            pHistogram.GetHistogram(xVals, frqs)
            pClassifyGEN.Classify(xVals, frqs, lNClass)

            ' use "iNClass" number of classes

            GenerateClassBreaks = pClassifyGEN.ClassBreaks
        Catch ex As Exception
            MessageBox.Show(Information.Err().Description, "GenerateClassBreaks", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try

    End Function

but still the function  doesn't return a value.

Regards
0 Kudos
AlexanderGray
Honored Contributor
GenerateClassBreaks = pClassifyGEN.ClassBreaks
not sure this is the problem but, "GenerateClassBreaks = pClassifyGEN.ClassBreaks" should be:
"return pClassifyGEN.ClassBreaks"
0 Kudos
HushamHassan
Regular Contributor
agray1
Thank you for your help...  that not work..  the error maybe on this line of code..
Public Function GenerateClassBreaks(pFeatLayer As IFeatureLayer, sValFieldName As String, lNClass As Integer, sClassification As String) As Object


I replaced the variant with object, since there is no variant on .net

Regards
0 Kudos
AlexanderGray
Honored Contributor
object is a lot like variant, that should be ok.  It's not really a best practice to return an weak type in a method but not wrong.  If you assign pClassifyGEN.ClassBreaks to a variable before returning it, can you check if it has a value?
0 Kudos
HushamHassan
Regular Contributor
Hi agray1
Thank you  and sorry for delay response,  I did assign pClassifyGEN.ClassBreaks to a variable before returning it, but it has no value..
and for the same function I am getting  casting error "unable to cast object of type System.double to type system.int32"

Many thanks and Regards
0 Kudos
AlexanderGray
Honored Contributor
The function returns an array of doubles.  If you do something like this do you get an array?

dim breaks() as double =  pClassifyGEN. pClassifyGEN.ClassBreaks
0 Kudos
HushamHassan
Regular Contributor
hi,
Really apprciate your help.. still  I am getting Null funciton,    I did some casting  as follow
Dim dataFrequency As Object = Nothing
            Dim dataValues As Object = Nothing
            Dim frqs As Integer() = TryCast(dataFrequency, Integer())
            Dim xVals As Double() = TryCast(dataValues, Double())

I am getting values for most of the variables,  but still the function return Null

Regards
0 Kudos