Ghoztrider
Ghoztrider

Reputation: 145

Using Directive for Text?

I'm taking a stab at learning C# now. I'm building a small web app in VS2013. I'm getting an error I can't seem to find a useful answer for. I've searched through stack and google for the error message below (as well as using directives). I feel like maybe it's telling me I'm missing a library, but not sure.... Below is the code and the error message.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyFirstWebApp
{
    public partial class Default : System.Web.UI.Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void okButton_Click(object sender, EventArgs e)
        {
            string firstName = firstName.Text;  <===Error
            string lastName = lastName.Text;  <===Error

            string result = "Hello " + firstName + " " + lastName;

            resultLabel.Text = result;
        }
    }
}

Error Message:

'string' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

I've tried a few different directives as well. System.Drawing; System.Text;

The two errors in question relate to two text fields on the web form. Thanks for any assistance!

Upvotes: 0

Views: 1271

Answers (1)

adricadar
adricadar

Reputation: 10219

This is happening because string don't have a property named Text, it's a primitive data type.

You named labels firstName and lastName, try to rename it to be more sugestive, like lblFirstName.

protected void okButton_Click(object sender, EventArgs e)
{
    string firstName = lblFirstName.Text;  
    string lastName = lblLastName.Text;  

    string result = "Hello " + firstName + " " + lastName;

    resultLabel.Text = result;
}

Upvotes: 1

Related Questions