469,576 Members | 1,716 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,576 developers. It's quick & easy.

How to control data member inside another form

The scenario is that i have two forms, Form1( main form) and Form2
and both of them opened. Form1 has called Form2 inside of its
constructor.

class Form1

{
const int NodeLimit=30;
Node[] nodeArray=new Node[NodeLimit];

public Node getNode()
{
...... // some code
return nodeArray[0]; // get some object that i want
}

}

class Form2

{

// want getNode() here
}

How can i get call getNode() and get a node object from Form1. Since
it is already open.

1. I cannot create a new object for Form1 since it is already open
2. One sol would be to have static class with nodeArray as a static
member. But won't i be violating the concepts of good object -
oriented programming?

Please suggest possible solutions. Damn!! I know OOP but not much of
C# and desktop development!!

Need Help
Regards

Nov 18 '07 #1
8 1606
On 2007-11-17 23:22:39 -0800, weird0 <am********@gmail.comsaid:
[...]
How can i get call getNode() and get a node object from Form1. Since
it is already open.
You just need the reference to the Form1 instance you've created.
1. I cannot create a new object for Form1 since it is already open
Well, you could create a new object for Form1 -- there's nothing to
stop you from having more than one instance of a given form class --
but of course the new instance wouldn't be the one with the data you
want.
2. One sol would be to have static class with nodeArray as a static
member. But won't i be violating the concepts of good object -
oriented programming?
Is the array something that would make sense as a static member? If
so, your current design is already flawed. :)

If it's not something that would make sense as a static member -- that
is, it really is specific to the form instance and if you did open
multiple instances of that form class, would you want the data separate
per-form? -- then making it static just to solve this issue definitely
wouldn't be a good idea.
Please suggest possible solutions. Damn!! I know OOP but not much of
C# and desktop development!!
Well, as I wrote to start with here: as things stand now, you simply
need to keep a reference to the form. There are a few different common
ways to deal with this:

* look for the form instance in the Application.OpenForms collection
* make the form a singleton, with a static accessor property
* pass the reference to the form to whatever object is going to
need it; for example, if your second form is what needs to access the
data, then a common technique is to pass the reference to the Form1
instance in the constructor when creating Form2.

Pete

Nov 18 '07 #2
hi Peter Duniho!! That is a very good reply.... "Need a reference for
Form1".

I have implemented the constructor thing that you told me, in my one
my semester course projects.

But how can i take reference of Form1(which contains nodeArray object,
that i want to access in Form2) inside of Form2. Can you explain this
please? or refer me to some good link?

Need help[ Chasing deadline]
Regards

Nov 18 '07 #3
I cant really figure out how to use Application.OpenForms .
Nov 18 '07 #4
Yess!!!!!!!!

I got it ..... what exactly you meant!!!!!!!

Thanks
Nov 18 '07 #5
On 2007-11-18 03:10:36 -0800, weird0 <am********@gmail.comsaid:
Yess!!!!!!!!

I got it ..... what exactly you meant!!!!!!!

Thanks
Glad it helped. I assume that means you don't need an answer for your
previous question?

Nov 19 '07 #6
Hey!! someway( what i call i a MindFreak Technique!!) I managed to
pass data from one form to another. But still i can't really
understand the reference thing to pass data between forms.

I have three forms:

class Form1
{
}

class TreeViewForm
{

event()
{
// create object and nodeproperties
}

}

class NodeProperties
{
Node currNode; // I have a node object inside of this class.... the
top two classes also contain the same object...

}
But hey!! How do i update the same objects inside Form1 and
TreeViewForm?????????? which means passing data backward to Forms!!
Even if i keep a reference of the first two classes inside third
class.. ref object and pressing dot does not allow me to access the
class data members. Even if it does, will it allow me to access the
same data.????

I am a newbie to C# desktop programming.

Need help
Regards
Nov 20 '07 #7
Let me clear this out!!!

class Form1
{
Node nodeobj;
}

class TreeViewForm
{
Node nodeobj;
event()
{
// create object and nodeproperties

}
}

class NodeProperties
{
Node currNode; // I have a node object inside of this class.... the
top two classes also contain the same object...

}

I have the nodeobj inside of every class, which seems to be the only
way to get the information of currentNode.
Now, after opening the 3rd form, that means all the constructors are
open.... I update the 3rd form(NodeProperties).
How can information be updated back to Form1 and Form2?

Information can be updated from Form1, to Form2 and Form2 to Form3
because they are binded by composition. I call the UpdateRef()
function and update the status ? But, how do I go backwards?

Can't do it with constructors because constructor just takes the
status, when the form is opened. Not, if it is already opened.

Regards
Thanks for all the help uptil now....
Nov 22 '07 #8
On 2007-11-22 02:35:38 -0800, weird0 <am********@gmail.comsaid:
[...]
I have the nodeobj inside of every class, which seems to be the only
way to get the information of currentNode.
Only way? I'm sure I don't know what that means. There's always an
alternative, even if it's not a desirable one.
Now, after opening the 3rd form, that means all the constructors are
open.... I update the 3rd form(NodeProperties).
How can information be updated back to Form1 and Form2?
What does "update" mean? What are you actually changing?

If you have an instance of the Node class that is referenced in all
three classes, and you modify that instance, then all three classes
will automatically have access to the modifications. They are all
referencing the same instance.
Information can be updated from Form1, to Form2 and Form2 to Form3
because they are binded by composition. I call the UpdateRef()
function and update the status ? But, how do I go backwards?
If you are asking how to change the "nodeobj" field inside the first
two classes when some instance in the third class has been replaced by
a completely different reference, then the first two classes need some
sort of member that is accessible by the third class that will change
the "nodeobj" field.

In the simplest case, this is just making the field "nodeobj" a public
field. I would never do this because it breaks encapsulation, but it's
certainly a possibility.

Alternatively, you could make a public property that sets the "nodeobj"
field. Finally, you could just write a regular method that sets the
"nodeobj" field.

In all of the above, depending on the relationship of the three
classes, it's possible that a "protected" or "internal" member would
suffice. It just depends on your design.

It's very difficult to answer your question because the words you write
are not very clear. You're not using the usual programming jargon, nor
have you yet been very specific about what exact action you want your
code to take.

Hopefully one of the above suggestions relates to what you're trying to
do. If not, you should be much more careful about how you write your
post, trying to be as specific as possible and making sure you don't
assume that we know what vague phrases like (for example) "information
be updated" mean.

Pete

Nov 22 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dennis | last post: by
2 posts views Thread by Jorge Ayala | last post: by
2 posts views Thread by John Lau | last post: by
1 post views Thread by Oleg Ogurok | last post: by
3 posts views Thread by Simon Harvey | last post: by
4 posts views Thread by gsb58 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.