Click here to Skip to main content
15,888,967 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i have three class. First Class where i declare property,in second class i set prperty value and third class for use its value.but in third class property is empty.
class1
C#
class first
{
public string first{get; set;}
}

class2
XML
class second
{
public second()
{
  first f1=new first();
  f1.first="hello";
}
}
</pre>

class third
XML
class third
{
public third()
{
  first f1=new first();
  string nn=f1.first;
}
}


string nn show null. what should i do?
Posted

If you want Forms to have access to the value (contents) of a Field/Property (variable) in another Form, then you have two essential choices:

1. make the Field/Property in the Form you wish to share 'static: then that Field becomes a Field of the Class, rather than a Field of each instance of the Class.
C#
public class FirstClass
{
    public static string FirstString {get; set;}
}

public class SecondClass
{
    string valueOfFirstStringInFirst = FirstClass.FirstString;
}

// test it
FirstClass fc = new FirstClass();
FirstClass.FirstString = "hello";
SecondClass sc = new SecondClass();
Console.WriteLine(sc.valueOfFirstStringInFirst);
Notice that you had to access the static Property 'FirstString using the Class name, not the name of the instance ('fc) of the Class.

2. Other ways for one Class to access the values (contents) of Fields/Whatever in another Class which do not involve using static include:

a. by using Interfaces which both Classes implement
b. by injecting references to instances into instances of other Classes
c. by exposing public methods that indirectly access the value (contents)
 
Share this answer
 
In second you create an instance of first. I third you create an other instance of first. These are two different instances. The property is not static, thus every instance has a different memory space allocated for it. Sou either share one single instance or use static property.
But I can't tell you which path to choose, as your post is rather theoretic.
 
Share this answer
 
v2
Comments
ZurdoDev 6-Feb-15 16:27pm    
+5. Good explanation.
ssnk 6-Feb-15 16:35pm    
how can i share single instance in diffrent class
Zoltán Zörgő 6-Feb-15 16:39pm    
It really depends on how you intend to use them. The code posted tells us nothing at all. Sorry, I can't guide you further without something much more concrete.
ssnk 6-Feb-15 16:49pm    
its a wpf application
code:-
variable class-
public class Variable
{
public string content { get; set; }
}

page1
private void btn1_Click(object sender, RoutedEventArgs e)
{
Variable v =new Variable();
v.content = textbox1.Text;
Page2 p2 = new Page2();
this.NavigationService.Navigate(p2);
}
page2
public Page2()
{
InitializeComponent();
Variable v = new Variable();
textbox2.Text = v.content;
}
Zoltán Zörgő 6-Feb-15 16:59pm    
A) Extend Page2 with a parametrized constructor and pass v to it.
B) Add a property of type Variable with public setter to Pare2 and set it to v after you create it in that click method. But be aware not to create a new instance afterwards. In this case you won't be able to set the textbox text in the constructor, you have to put that in the setter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900