4 Replies - 1068 Views - Last Post: 14 June 2009 - 06:11 AM Rate Topic: -----

#1 gotagoJOE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 12-June 09

having value from property in class display in textbox

Posted 14 June 2009 - 05:48 AM

Hello I have created windows project- added a class-
I have a form with label, textbox, and button.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication
{
	//add class to windows project
	class ModelName
	{
	 //declare variables   
	  public string _name;
	  private string _modelName;
// add constructor and set  _name variable to "enter model brand"
	  public ModelName()
	  {
		  _name = "Enter Model Brand";
		
	  }
//property called modelName- string type
	  public string modelName
	  {
		  get
		  {
			  return _modelName;
		  }
		  set
		  {
			  _modeName = "Martin";
		  }
	  }


	  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
	public partial class Form1 : Form
	{
		ModelName myClass = new ModelName();
		public Form1()
		{
			InitializeComponent();
		}
	// label has text 'enter brand name' when form loads
		private void Form1_Load(object sender, EventArgs e)
		{
	label1.Text = myClass._name;
		}

		private void label1_Click(object sender, EventArgs e)
		{
			

		}

		private void textBox1_TextChanged(object sender, EventArgs e)
		{
		  
		}

		private void button1_Click(object sender, EventArgs e)
		{
// ****NOT WORKING***** want martin to display
	textBox1.Text = myClass.modelName
		}
	}
}

I want to click on button in form and have MARTIN display in textbox.
thank you

This post has been edited by gotagoJOE: 14 June 2009 - 05:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: having value from property in class display in textbox

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: having value from property in class display in textbox

Posted 14 June 2009 - 05:54 AM

you should be getting a build error since you misspelled "model"

          set
          {
              _modeName = "Martin";
          }


Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: having value from property in class display in textbox

Posted 14 June 2009 - 06:00 AM

also, because of the way you setup the property, you must first "set" the property to a value.

private void button1_Click(object sender, EventArgs e)
{
    myclass.modelName = "";
    textBox1.Text = myclass.modelName;
}


Was This Post Helpful? 1
  • +
  • -

#4 gotagoJOE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 12-June 09

Re: having value from property in class display in textbox

Posted 14 June 2009 - 06:05 AM

View Posteclipsed4utoo, on 14 Jun, 2009 - 05:00 AM, said:

also, because of the way you setup the property, you must first "set" the property to a value.

private void button1_Click(object sender, EventArgs e)
{
    myclass.modelName = "";
    textBox1.Text = myclass.modelName;
}


Thank you for the help- it works now
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: having value from property in class display in textbox

Posted 14 June 2009 - 06:11 AM

this is what your class should be if you want a default value.

class ModelName
{
    //declare variables   
    public string _name;
    private string _modelName = "Martin";
    // add constructor and set  _name variable to "enter model brand"
    public ModelName()
    {
        _name = "Enter Model Brand";

    }
    //property called modelName- string type
    public string modelName
    {
        get
        {
            return _modelName;
        }
        set
        {
            _modelName = value;
        }
    }
}



then your button click would only need to be this...

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Text = myclass.modelName;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1