How to create a variable in Visual Studio

Home > Search > How-to
  by

When first learning variables, probably the easiest varabile to learn is one that hold's a string of text. In this example, let's say you have a page in Visual Studio named example.aspx, and you want to create a variable that contains the text "Hello World".


Create variable

In Design View, double left-click to open code behind. Adding Label1.Text = "Hello World" in code behind creates a variable named Label1 that displays the text Hello World.

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles Me.Load  Label1.Text = "Hello World"
End Sub

 

C#

protected void Page_Load(object sender, EventArgs e){
    Label1.Text = "Hello World";
}

 


Use variable

To use the Label1 variable, the following markup will need to be added to the example.aspx page:

<asp:Label ID="Label1" runat="server"></asp:Label>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments