Within a With <obj> / End With block, is there a keyword to access <obj> directly?

StackOverflow https://stackoverflow.com/questions/16161661

  •  11-04-2022
  •  | 
  •  

Domanda

In VB.NET, just wondering if there is a kind of "this" keyword, with which one can access the object being used with in With <obj> ... End With block. Eg:

With myObj
  .thisMethod()
  someFunction(<this>) ' Where "<this>" refers to myObj
  .thatMethod()
End With

It would just be handy if it were possible, in those times you want to pass myObj without leaving the With block.

È stato utile?

Soluzione

You can't do that directly no. The only way I can think of doing this is to extend your object to include a reference to itself as a readonly property:

Public Class TextBoxExtended
    Inherits TextBox
    Public ReadOnly Property ObjRef As TextBox
        Get
            Return Me
        End Get
    End Property
End Class

You can then do this in your with Block:

With myObj
  .thisMethod()
  someFunction(.ObjRef)
  .thatMethod()
End With

However I would have to question why you would want to do this.

Altri suggerimenti

I don't quite follow you. Why can you refer to the object?

For example if that was a textfield.

With textbox1
.visible = true
.text = textbox1.text
End with

That would still work.

in fact this would also work.

.text = .text

Maybe not the best as you will never see a change...

Lets try one more relevent...

' The function to send to.
Function myfunction(thestring As String) As String
    thestring += "moretext"
    Return thestring
End Function

' The with statement
With Textbox1
.Text = myfunction(.Text)
End With

The textbox text will change to the original text + "moretext".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top