How to control data member inside another form

P

Peter Duniho

[...]
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
 
W

weird0

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
 
W

weird0

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
 
P

Peter Duniho

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?
 
W

weird0

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
 
P

Peter Duniho

[...]
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.????

Well first, while Intellisense is indeed very useful in navigating and
even learning the class hierarchies and functionalities, there's no
substitute for really learning the classes and how the language works.
So you may want to do a little more directed learning with respect to
C# and .NET than just diving in and seeing what works.

As for why Intellisense isn't providing information, you haven't posted
code that would illustrate the problem but I am guessing that the
variable type is "object" rather than the actual type of the instance
you're trying to get at. So the only members of the type that are
known to Intellisense would be those defined in the "object" class, not
the actual instance's type.

So the solution to that is to store the instance reference in a
variable that has the same type as the instance itself. Then
Intellisense will know what sorts of things to show you.

As for the question of whether you can access the same data, you can as
long as the reference is the same reference used elsewhere. If you
only create one instance of the object, and then you pass that
reference to difference pieces of code, those pieces of code will all
refer to the same instance and yes, allow you to access the same data.

Pete
 
W

weird0

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

Peter Duniho

[...]
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
 

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