how to use variable form other class? C#
-
if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...
-
if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...
class B
{
public void SomeMethod()
{
if (A.a = "aaa")
{
DoSomeStuff();
}
}
}The A class needs to be public for this to work if the classes have different namespaces. The A class can also be internal if the namespace of the two classes is the same.
Kristian Sixhoej sixhoej.net - forums.sixhoej.net
modified on Thursday, November 27, 2008 1:50 PM
-
if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...
This is so rudimentary that you really need to buy a book and work through it, before you try to write any code that's not a book example.
Christian Graus Driven to the arms of OSX by Vista.
-
if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...
Hi, You need to create an instance from class A so you can use its public members. In this case you should write:
public class A
{
public string a = "aaa";
}public class B
{
A Ainstance = new A();
if(Ainstance.a = "aaa") {}
if (Ainstance.a == "aaa") { ;}
}If variable
a
(or any member in your class) is static, you can use it from class B without creating an instance. Just like:public class A
{
public static string a = "aaa";
}class B
{
if(A.a = "aaa") {}
if(A.a == "aaa") { ;}
}Hope can help :)
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
modified on Thursday, November 27, 2008 4:22 PM
-
Hi, You need to create an instance from class A so you can use its public members. In this case you should write:
public class A
{
public string a = "aaa";
}public class B
{
A Ainstance = new A();
if(Ainstance.a = "aaa") {}
if (Ainstance.a == "aaa") { ;}
}If variable
a
(or any member in your class) is static, you can use it from class B without creating an instance. Just like:public class A
{
public static string a = "aaa";
}class B
{
if(A.a = "aaa") {}
if(A.a == "aaa") { ;}
}Hope can help :)
I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog
modified on Thursday, November 27, 2008 4:22 PM
your tip can help me thank so much...
-
your tip can help me thank so much...
-
if i need to use variable form other class & other file but same namespace & project how to use that variable? example: --file1-- class A { public string a = "aaa"; } --file2-- class B { if(a = "aaa") {}} please hint to me... thank so much...