473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get and Set Property in VS2005

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; }
}
}
}
Oct 28 '08 #1
6 2236
On Tue, 28 Oct 2008 16:34:00 -0700, Rick <Ri**@discussions.microsoft.com>
wrote:
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?
The property is an instance property, and so if you expect to get the same
value you set, you need to get the value from the same instance on which
you set the property.

You're using two different instances, so of course you don't get the value
you set.

You didn't used to write .NET code with VB.NET, did you? That mistake is
remarkably common for people who are used to VB.NET and just learning C#.
VB.NET has some implicit instance behaviors that can lead to
misunderstandings when using C#, which requires you to be explicit about
what object you're using.

Pete
Oct 29 '08 #2
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
Oct 29 '08 #3
On Tue, 28 Oct 2008 20:52:31 -0700, Lee <sa******@hotmail.comwrote:
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.
Only if it's okay for every use of the property to access the same value.

In most cases, when someone declares a property as an instance member,
it's important for the property to actually be per-instance. In that
case, making the property static would be wrong.

More generally, the question of "static" versus "instance" is almost never
about saving memory and is never about resolving some unexpected behavior
due to failure to understand how instance members work. It's about
knowing whether something makes sense as a per-instance member or not.

Pete
Oct 29 '08 #4

" 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.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 28 Oct 2008 20:52:31 -0700, Lee <sa******@hotmail.comwrote:
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.

Only if it's okay for every use of the property to access the same value.

In most cases, when someone declares a property as an instance member,
it's important for the property to actually be per-instance. In that
case, making the property static would be wrong.

More generally, the question of "static" versus "instance" is almost never
about saving memory and is never about resolving some unexpected behavior
due to failure to understand how instance members work. It's about
knowing whether something makes sense as a per-instance member or not.

Pete

Oct 29 '08 #5
On Wed, 29 Oct 2008 03:26:15 -0700, xcal <a@a.comwrote:
" 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 ?
I suppose that depends on what you mean by "per-instance member issues".
But taken literally, I don't think what you wrote is relevant here.

In most cases, the "this" keyword is not necessary. When you need to
explicitly reference the instance itself from within an instance method,
such as passing the reference to something else, or when you need to
distinguish a field member from a method argument of the same name, then
you'd need the "this" keyword. But it's not _solving_ a per-instance
member issue per se. That is, you can't get yourself out of a
per-instance member design issue by throwing the "this" keywoard at it.
All that "this" does is help the _implementation_ work correctly. If the
design is flawed, it's still flawed even if you use "this".

Pete
Oct 29 '08 #6
Thanks to you all; I got it to work using your suggestions.
"Peter Duniho" wrote:
On Wed, 29 Oct 2008 03:26:15 -0700, xcal <a@a.comwrote:
" 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 ?

I suppose that depends on what you mean by "per-instance member issues".
But taken literally, I don't think what you wrote is relevant here.

In most cases, the "this" keyword is not necessary. When you need to
explicitly reference the instance itself from within an instance method,
such as passing the reference to something else, or when you need to
distinguish a field member from a method argument of the same name, then
you'd need the "this" keyword. But it's not _solving_ a per-instance
member issue per se. That is, you can't get yourself out of a
per-instance member design issue by throwing the "this" keywoard at it.
All that "this" does is help the _implementation_ work correctly. If the
design is flawed, it's still flawed even if you use "this".

Pete
Oct 29 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Thorsten Ottosen | last post by:
Dear all, I'm new to C#, so forgive my stupid question. In my question to avoid boilerplate code, I was wondering if I could use attributes to generate some code for me. For example ...
3
by: A Traveler | last post by:
Hello, I am wondering if this is possible: Code myself a nice class, say MyClass. Give it some property, say MyProp which has Public Readonly but Protected Write-ability. So it would be, in...
1
by: Steve Teeples | last post by:
Using VS2005, is the a simple method to hide or show a property within a propertygrid based upon the changing value of another property within the propertygrid. For example, if I change a...
2
by: slawrence | last post by:
Hi I am getting an error "Code generation for property 'SubCategoryId' failed. Error was 'Property accessor 'SubCategoryId' on object 'yyy.zzz' threw the following exception: 'Object reference...
0
by: slawrence | last post by:
Hi I am getting an error "Code generation for property 'SubCategoryId' failed. Error was 'Property accessor 'SubCategoryId' on object 'yyy.zzz' threw the following exception: 'Object reference...
4
by: steve | last post by:
Hi All When I set a buttons image to a image file in the properties window it seems to lock the file even if I clear the button image property in the properties window Sometimes I am editing...
14
by: Dom | last post by:
Hi all I'm developing a control, and I need to hide some properties to the user. For example, suppose I need Text property to be completely inacessible (from a Form/Code that is into another...
1
by: Chris Peeters | last post by:
hi, in VS 2003 one used the MdiList-property of a menuitem to display the Active Mdi-child windows in the menuitem. But I can't find it nowhere in VS2005. what is the equivalent for that in...
1
by: rflloyd | last post by:
I wish to create a property of a control which is an array of Images, such that I can add, edit and delete images in the VS2005 property window. I've created the property as: private Image...
1
by: Nathan Sokalski | last post by:
I have added Description attributes to several of my Control's Properties, but they are not appearing in the Property Browser. Here is the declaration of my Property: ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.