Joe
Joe

Reputation: 1057

Vb.net Nickname an object?

Is it possible to do something like this:

Dim ThisThing as ThisThingToolStripMenuItem.Text

So i can later in the code just do ThisThing = "whatever" ?

Upvotes: 1

Views: 134

Answers (2)

NoAlias
NoAlias

Reputation: 9193

A variable has to be a type so the best you can do is something like this...

Dim objYourShorterName As New ToolStripMenuItem

'Where 1 is the particular index of the ToolStripMenuItem you wish
'the variable to reference.
objYourShorterName = ToolStrip1.Items(1)

'You still have to use the Text property.
objYourShorterName.Text = "Blah"

You can write a Sub Procedure to do it for you too.

Private Sub ShorterName(ByVal strText As String)

   ToolStrip1.Items(1).Text = strText

End Sub

Then to change the text...

ShorterName("New Text")

Upvotes: 0

Scott Whitlock
Scott Whitlock

Reputation: 13839

No, that won't work, but you can assign ThisThingToolStripMenuItem to a variable with a smaller name, but you still have to reference it as ThisThing.Text.

You can also use a delegate defined with a lambda expression:

Action<string> setter = (string s) => ThisThingToolStripMenuItem.Text = s;

...

setter("whatever");

In VB.Net:

Dim Setter = Sub(s) ThisThingToolStripMenuItem.Text = s

...

Setter("whatever")

Upvotes: 1

Related Questions