0

Just got to know about C# snippet. But i am unable to use them in my code. Help me out please, I am messed up with get set and how they work.

Here is my test class named "myclass"

namespace WindowsFormsApplication1

{
    class myclass
    {
        public string getmessage(string givenName)
        {
            return "HB "+givenName;
        }

        private string bmessage;

        public string MyProperty
        {
            get { return bmessage; }
            set { bmessage = value; }
        }

    }
}

In button code from my form. I am unable to use these get set. It ll be great if someone clears that how can i use these get set. Moreover what is "MyProperty" here? I know it is not a method. What is its purpose? Thanks

4
  • 4
    This looks like a c# 101 question, not anything to do with Snippets. You should probably go read up on C# Properties. Commented Jun 27, 2014 at 13:06
  • Have you read the documentation? What does your question have to do with snippets? Commented Jun 27, 2014 at 13:06
  • 1
    homeandlearn.co.uk/csharp/csharp_s10p5.html check this link. It is adding prob through snippet. You may leave. if it is making any sense. Commented Jun 27, 2014 at 13:08
  • 1
    @BradChristie Your comment was very helpful. I would have accepted it, if u had putted it as an answer :) Commented Jun 27, 2014 at 13:14

2 Answers 2

3

Snippets itself are not executable statements. But are short-cuts that help you in writing executable statements.

For e.g.

If we write prop and press enter, it will give you a auto-generated property. You just have to change Datatype and Property Name.

Similarly propfull will give a property with get and set parts.

In your case MyProperty is the Property Name and string is the DataType. bmessage is the backing field for your property.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for propfull, so many years I have been using VS, I never noticed it.
The issue is, How will i use those get set function in my program? I'm unable to call them and use them with myclass object.
When you want to use MyProperty its value will be retrieved from bmessage. Similary while assigning, value will be residing in bmessage You don't have to do anything.
@NikhilAgrawal The first comment on my question was from where i got it cleared. That was just a formal thank for your answer :)
0

The properties of a class are set and retrieved by using set/get methods. Basically these are also methods.

namespace BusinessObjects
{
public class class_BusinessObjects
{
    int StusentId;      
        string StudentName;        

    public class_BusinessObjects ()
    {       
        StusentId = 0;
        StudentName = string.Empty;
    }

    public int StusentId
        {
                get
        {  
            return Id;
                }
                set
                {
                    Id = value;
                }
        }       

        public string StudentName
        {
                get
                {
                    return Name;
                }
                set
                {
                    Name = value;
                }
        }        
}
}


using BusinessObjects;
namespace MyModel
{
public class A
{
    public class_BusinessObjects Dispaly(int id, string name)
        {
        class_BusinessObjects obj = new class_BusinessObjects();
        obj.StusentId = id;
        obj.StudentName = name;
        return obj;
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.