strange problem C#
hi all
i need som help
i am filling a string in a class but when i open that string in a difrent class it's empty.
can some on explain why
Code:
using System;
namespace NameTest
{
class teststring2
{
public string test;
}
class teststring
{
static void Main()
{
teststring3.test();
teststring2 TestStr = new teststring2();
Console.WriteLine("lets look at the string again " + "\" " + TestStr.test + " \""+" it should be here but its not why");
Console.ReadLine();
}
}
class teststring3
{
public static void test()
{
teststring2 TestStr = new teststring2();
TestStr.test = "\"Hi\"";
Console.WriteLine("lets look at the string " + TestStr.test);
Console.ReadLine();
}
}
}
The instance of "teststring2" you create in your Main function is different from the one you create in your teststring3 class. Therefore they have different "test" members which have different values.
Make a member of teststring3 that's publically accessible.
class teststring3{ public teststring2 TestStr; public void test() { TestStr = new teststring2(); TestStr.test = "\"Hi\""; Console.WriteLine("lets look at the string " + TestStr.test); Console.ReadLine(); }}class teststring{ static void Main() { teststring3 test = new teststring3(); test.test(); Console.WriteLine("lets look at the string again " + "\" " + test.TestStr + " \""+" now it should be there"); Console.ReadLine(); }}
i'm sarry but what did you have in mind withe
public teststring2 TestStr;
i gat an error at that part
Error 1 The type or namespace name 'teststring2' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Danie\Desktop\ConsoleApplication4\ConsoleApplication4\Program.cs 3 12 ConsoleApplication4
public teststring2 TestStr;
i gat an error at that part
Error 1 The type or namespace name 'teststring2' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Danie\Desktop\ConsoleApplication4\ConsoleApplication4\Program.cs 3 12 ConsoleApplication4
In MJP's post in
You could also define an instance of teststring2 in the global scope instead of in the teststring3 class and the main function so you have one instance which can be accessed by every function.
like this:
good luck,
ID
Console.WriteLine("lets look at the string again " + "\" " + TestStr.test + " \""+" it should be here and it is!");
TestStr.test does not return a string. You should do something like this: TestStr.test.test
so you access the string in the instance of teststring2 (public teststring2 TestStr;)You could also define an instance of teststring2 in the global scope instead of in the teststring3 class and the main function so you have one instance which can be accessed by every function.
like this:
using System;namespace NameTest{class teststring2{public string test;}teststring2 TestStr = new teststring2(); // define your instance here, outside of any function so you can use it in all of your functions.class teststring{static void Main(){teststring3.test();//teststring2 TestStr = new teststring2(); <-- not here.Console.WriteLine("lets look at the string again " + "\" " + TestStr.test + " \""+" it should be here and it is!");Console.ReadLine();}}class teststring3{public static void test(){//teststring2 TestStr = new teststring2(); <-- and not here.TestStr.test = "\"Hi\"";Console.WriteLine("lets look at the string " + TestStr.test);Console.ReadLine();}}}
good luck,
ID
Hi
There is still a small little problem.
When I try to compile this bit of code it says
Error 4 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\OPHEMD\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 9 9 ConsoleApplication1
At this part:
teststring2 TestStr = new teststring2();
I am using visual studio 2005
There is still a small little problem.
When I try to compile this bit of code it says
Error 4 A namespace does not directly contain members such as fields or methods C:\Documents and Settings\OPHEMD\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Program.cs 9 9 ConsoleApplication1
At this part:
teststring2 TestStr = new teststring2();
I am using visual studio 2005
With all due respect, I think you might want to look into object oriented programming and C# program structure a bit closer. Here's an excellent resource that explains what goes where and how everything fits together:
Joe Mayo tutorial series
As for your error message, it indicates that you're defining members in a namespace, rather than in a class. Instead of trying to explain what this means and why this doesn't work, I encourage you to work your way through Joe's series. That should clear up a lot I think.
Joe Mayo tutorial series
As for your error message, it indicates that you're defining members in a namespace, rather than in a class. Instead of trying to explain what this means and why this doesn't work, I encourage you to work your way through Joe's series. That should clear up a lot I think.
In particilar, you need to learn about variable scope. This is not just an OOP issue.
There should be plenty of tutorials on the web. Just google it.
There should be plenty of tutorials on the web. Just google it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement