Get and Set Property in VS2005

R

Rick

I have a main for frmMain and another form formTest.
I want to set a value of a property in frmMain and then access that value in
frmTest.
I am able to set the property in frmMain as:
MyClass myClass = new MyClass();
myClass.MyProperty = "My New Value";

But, I can't retrieve this value in frmTest; this returns blank:
MyClass myClass = new MyClass();
string MyReturnedValue = myClass.MyProperty;

How do I do it?

******** sample class *********
using System;
using System.Collections.Generic;
using System.Text;
namespace MyApplication
{
class MyClass
{
private string myPropertry;
public string MyProperty
{
get { return myPropertry; }
internal set { myPropertry = value; }
}


}
}
 
L

Lee

If you are using MyClass to transfer values, create the properties as
static. You can then set and retrieve these values without the need
to create an instance of the object (using the 'new' command)

Using static saves memory since you do not have to create an instance
of every class to use its values, but you must remember that the
values you set are global to the object. If you change the values
anywhere, that change will be reflected everywhere.

declare:
static public string MyProperty

then you set it like:
MyClass.MyProperty = "My New Value";

and get it like:
string MyReturnedValue = MyClass.MyProperty;

and everything will work just fine.

L. Lee Saunders
http://oldschooldotnet.blogspot.com
 
X

xcal

" It's about knowing whether something makes sense as a per-instance member or not."

hi Peter,

I believe that the "this command" should be used allways,
i.e. to use
this.member1
this.property1
etc
because doing so would solve any
per-instance member issues, am I correct ?

regards, Carlos.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top