Code Accessor Methods

Class library usage, coding and language questions.
regex
Posts: 48
Joined: Tue Aug 14, 2012 5:47 pm

Code Accessor Methods

Post by regex » Tue Nov 06, 2012 8:16 pm

I am trying to encapsulate several fields to prevent the typical public proposition from multiple sources. Here is my code within a code module:

Code: Select all

public static string dateAttached(string dateAttached)
		{
		   get { return _dateAttached; }  
    		   set { _dateAttached = value; }  
	        
		}
/code]

I get formatting errors as enclosed within the attachment.  The second attachment shows how I've set the fields to a public variable in the past.  

QUESTION:

How do I format the accessor methods!
You do not have the required permissions to view the files attached to this post.

User avatar
artur_gadomski
Posts: 207
Joined: Mon Jul 19, 2010 6:55 am
Location: Copenhagen, Denmark
Contact:

Re: Code Accessor Methods

Post by artur_gadomski » Wed Nov 07, 2012 8:44 am

http://msdn.microsoft.com/en-us/library ... s.80).aspx

This should work:
public static string dateAttached
{
    get { return _dateAttached; } 
    set { _dateAttached = value; } 
}

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Code Accessor Methods

Post by Support Team » Wed Nov 07, 2012 4:08 pm

Hello,

It seems that you have tried to mix properties with methods.
You could write a property as shown below:
public static string dateAttached { get; set;}
Another solution with properties is shown below:
private static string _dateAttached;
public static string dateAttached
{
	get { return _dateAttached; }
	set { _dateAttached = value; }
}
Another way to do that is to use methods as shown below:
private static string _dateAttached;
public static void setDateAttached(string dateAttached)
{
	_dateAttached = dateAttached;
}
public static string getDateAttached()
{
	return _dateAttached;
}
Regards,
Markus (T)