vb.netwebforms

Trying to understand VB.NET idiosyncrasies - circular references?


I'm looking at an existing project, trying to make sense of something that VB.NET is doing or allowing:

    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Session("UserID") Is Nothing Then
            Response.Redirect("~/AccessDenied.aspx")
            Exit Sub
        End If

        ' Variables
        Dim oNotes As New VistaClaims.ClaimsInfo.NoteDB
        Dim DSNotes As New DataSet
        Dim dgNotes As DataGrid = dgNotes
        Dim oRecoveries As New VistaClaims.ClaimsInfo.RecoveryDB
        Dim oBills As New VistaClaims.ClaimsInfo.PaymentDB
        Dim oSubro As New VistaClaims.ClaimsInfo.SubroDB
        Dim DSRecoveries As New DataSet
        Dim DSBills As New DataSet
        Dim DSSubro As New DataSet
        'Dim dgBills As DataGrid = dgBills
        'Dim dgRecoveries As DataGrid = dgRecoveries

screenshot

The issue I have is with the line Dim dgNotes As DataGrid = dgNotes.

I tried commenting it out, thinking it was defined on the ASP.NET WebForm, but it was not and I got errors.

I've done more C# development, and often VB.NET does things that I think should be illegal - but they work.

The code works. There is no error that I know of. I just happened to open this particular form on an old project today, and I thought I'd resolve the possible (likely, it seems to me) null reference exception.

What is that line of code doing? I don't understand.


Solution

  • As noted looks like a oversight, since a the one declare of a variable, and then assigning it to "it's self" does not make sense.

    I would suggest

    Dim dgNotes As DataGrid = Nothing. 
    

    As noted, it only a warning, but the line of code as noted does not make a whole lot of sense - and as noted, it could be left "as is". So, the error "warning" is correct in the sense that the declared variable does not "yet" have a value, and then assigning to it's self thus does not really make sense to do.

    In fact, the variable could be declared without assigning it to Nothing, and the same warning would exist.

    So, nothing "special" is being attempted here, and it looks to be a simple coding oversight, and as such, nothing really to see here. In many cases, the syntax checking is "less" then C#, and much of this is due to the history of VB.NET code, and it had a "number" of features to be somewhat "more" compatible with older VB5, and VB6 code.

    However, setting to Nothing is a more correct line of code and syntax.