• 0

[VB.Net] And


Question

OK in Visual Basic 6 you can do this:

lblanswer.caption = txtname & " you are " & txtage & "!"

but I do this in .Net

lblanswer.text = txtname & " you are " & txtage & "!"

Then it says & is an error! What's the problem, and is it an real error, and if so what can I do to do that? Do I need to delcare the text boxes as strings?

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

In .NET, there are no default properties like there are in VB 6. You're trying to concatenate objects in your .NET code;

This is the correct way in .NET. If you're going to do a lot of concatenation, you should use a StringBuilder.

lblanswer.Text = txtname.Text & " you are " & txtage.Text & "!"

Link to comment
Share on other sites

  • 0
OK, thanks it worked! But how do I declare strings? I use

Option Explicit

Dim strname as String

And I get errors for both lines?

Where are you declaring it? BTW: You don't need to do Option Explicit. VB.net will automatically do that :D

Link to comment
Share on other sites

  • 0
OK, thanks it worked! But how do I declare strings? I use

Option Explicit

Dim strname as String

And I get errors for both lines?

If you're trying to declare a string outside a class or module, you'll get an error. Everything has to be in a class. a module, a structure, or within a procedure.

Link to comment
Share on other sites

  • 0

You need to associate your variable with a class, structure, or module. If you want what is essentially a global variable, use the Shared statement which makes the variable associated with the class and not a specific instance.

Class Testing
    Shared sTest as String ' usage Testing.sTest = "Hi"
    Public sTest1 as String ' specific to an instance of class Testing, visible to whomever instantiates it.
    Dim sTest2 as String ' specific to an instance of class Testing, but private scope.
End Class

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.