Jump to content

C# properties practice

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

I am playing around with using properties and ran into a problem, its probably simple and i am over looking it, but what i want to do is when a person types in a textbox yes, the textbox is equal to pullfromclass.HavingFun which is a property that says if havingfun == yes (havingfun and Havingfun is different, but look at the code below to see if it makes sense or if that can be my problem) if yes then great, if not sorry to hear about that, but for some strange reason its not working again that could be the problem but please let me know if i am doing something wrong thanks:


the button object:

private void button1_Click(object sender, EventArgs e)

        {

            pullfromclass.Name = textBox3.Text;//from first class with property - pullfromclass.Name

            MessageBox.Show(pullfromclass.test(pullfromclass.Name) + " " + pullfromclass.lastname);

                                                                      //using inheritance to get a field from class2 from class1

            pullfromclass.funstuff(textBox4.Text);

            MessageBox.Show(pullfromclass.answer);

        }


the class1:


private string havingfun;

public string answer;


public string Havingfun

        {

            get { return havingfun; }

            set

            {

                if (havingfun.ToLower() == "yes")

                {

                    answer = "Great that you are";

                }

                else

                {

                    answer = "sorry to hear about that";

                }

            }

        }


thanks in advance

#2
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
You are not setting the string HavingFun... you are not setting in the set {} havingfun = yes and in the else havingfun = no , so that way when you make if (havingfun.ToLower() == "yes") it is not returning yes.. it's returning empty.

private string havingfun;

public string answer;


public string Havingfun

        {

            get { return havingfun; }

            set

            {

                havingfun = value;

                if (havingfun.ToLower() == "yes")

                {

                    answer = "Great that you are";

                }

                else

                {

                    answer = "sorry to hear about that";

                }

            }

        }

Probably that is what is not working, although I did not fully understand your problem.

#3
scottk

scottk

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
You may also consider using string.Compare() as the code you pasted will raise an exception if you set the property to 'null'.

#4
sindy55

sindy55

    Newbie

  • Members
  • PipPip
  • 11 posts
interesting :)

#5
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
I'm really struggling to understand just what it is you're trying to achieve here. Am I right in thinking that you want the user to enter either yes or no into the text box and then, depending on the answer, a message box shows a response?

If this is the case I don't understand how the code for the button's click event relates to this, but this could just be because you haven't shown us the full class definition.

Does your program even run properly? When I tried to compile a program using your property I get an error when I click on the button. The error I get is saying that the havingfun member is null. Although I understand why this error is coming up I'm having trouble coming up with a way to express it. Hopefully someone else will be able to fill the gap.

Personally I think you are trying to achieve too much with one property. Really you either need a property for both "havingfun" and "answer" (which should really be private as there's no need to have a property relating to a public member), or you could just have a property related to "answer" and do away with the "havingfun" member. I'd opt for the later just because I don't like having unnecessary members floating about.

Your property for "answer" could then look like this

	public string Answer
	{
		get {return answer;}
		set
		{
			if (value.ToLower() == "yes")
				answer = "Great that you are"
			else
				answer = "sorry to hear about that"
		}
	}

Your code within the button's click event would be

	pullfromclass.Answer = textBox.Text;
	MessageBox.Show(pullfromclass.Answer);

Hopefully I've understood what you are trying to do. In future it might be helpful to give us a little more of your code and an idea of what's going wrong.