user594659
user594659

Reputation: 27

VB.NET Equivalent?

what is the equivalent of this C# code in VB.NET:

UctlTest uctlTest;
uctlTest= (UctlTest)Page.LoadControl("UserControls/UctlTest.ascx");
if (plcTest.Controls.Count == 0)
    plcTest.Controls.Add(uctlTest);

UctlTest: user control

plcTest: PlaceHolder

I tried this:

Dim uctlTestVB As UctlTestVB
uctlTestVB = CType(Page.LoadControl("~/UserControls/UctlTestVB.ascx"), UctlTestVB)
If (Equals(uctlTestVB.Controls.Count, 0)) Then         
    plcTest.Controls.Add(uctlTestVB)
End If

But count in VB.NET is equal to 1 whereas in C# it is equal to 0.

Upvotes: 1

Views: 316

Answers (3)

Matthew Lock
Matthew Lock

Reputation: 13476

Notice you have an extra "~" symbol in your VB version, but not in your c#:

uctlTest= (UctlTest)Page.LoadControl("UserControls/UctlTest.ascx");

vs

uctlTestVB = CType(Page.LoadControl("~/UserControls/UctlTestVB.ascx"), UctlTestVB)

Upvotes: 3

Chris Taylor
Chris Taylor

Reputation: 53699

At a quick glance your code is checking a different control's child count.

C#: plcTest.Controls.Count

vs.

VB: uctlTestVB.Controls.Count

Yet the VB code still adds the loaded control to plcTest, but checks the count of the uctlTestVB instance.

Upvotes: 0

Martin Booth
Martin Booth

Reputation: 8595

The code is equivalent, your control probably isn't.

Why not load the same control in both snippets and verify they work the same.

Alternatively use the debugger/watch window and take a look at the Controls collection in both and find out how they differ

Upvotes: -1

Related Questions