0

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!

adricadar
  • 9,971
  • 5
  • 33
  • 46
Ghoztrider
  • 145
  • 1
  • 11
  • I suspect you should have named your textboxes as `firstNameTextBox` instead of `firstName` to avoid collisions with reasonably named local variables... – Alexei Levenkov May 23 '15 at 17:50
  • Do you have some textbox or other controls that keeps your first and last name? Because you are not using them, you just using your local string variables which does not have any `Text` property. – Soner Gönül May 23 '15 at 17:51
  • Yes.....I do have two text boxes respectively. Thanks. I'll revisit the tutorial and see if I missed something.. – Ghoztrider May 23 '15 at 17:57
  • I can't believe I actually made this mistake....lol. I guess I was to close to the forest to see the trees. Thanks for pointing out the obvious :) – Ghoztrider May 24 '15 at 00:40

1 Answers1

1

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;
}
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • What's a "basic" data type? – Rawling May 23 '15 at 17:53
  • 1
    @Rawling All data types in C# are basically the same except *some* of them are "value" types (pretty much the ones with keywords). `String` is actually a bit of a special case http://stackoverflow.com/questions/1069155/is-string-a-value-type-or-a-reference-type but regrardless, the answer is basically correct. `String` does not have a `Text` property. – BradleyDotNET May 23 '15 at 19:39