Sign in to follow this  
precious roy

strange problem C#

Recommended Posts

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(); } } }

Share this post


Link to post
Share on other sites
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();
}
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
In MJP's post in
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
hi

i fixed my problem by doing it this way

using System;

namespace NameTest
{
public class options
{
private string xmlFile = @"Config.xml";
private string serverIp = "192.168.0.000";

public string XmlFile
{
get { return xmlFile; }
set { xmlFile = value;}
}
public string ServerIp
{
get { return serverIp; }
set { serverIp = value; }
}
}

class teststring
{
static void Main()
{
options LOptions = new options();
Console.WriteLine("the file name " + "\"" + LOptions.XmlFile + "\"" + " kijk");
Console.WriteLine("the file name " + "\"" + LOptions.ServerIp + "\"" + " kijk");
Console.ReadLine();

insert.optionsinsert(LOptions);

Console.WriteLine("the file name " + "\"" + LOptions.XmlFile + "\"" + " kijk");
Console.ReadLine();
}
}

class insert
{

public static void optionsinsert(options x)
{
x.XmlFile = @"d:\config.xml";
Console.WriteLine(x.ServerIp);
}
}
}



Share this post


Link to post
Share on other sites
Glad to hear you got it fixed. Two small things left for me to nag on [wink]

1. In C# it's common practice to start classes, method and property names with an uppercase letter (so Options instead of options). This doesn't really make a difference for your program, but it's easier to read for other C# coders trying to help you.

2. On these boards you can put your code between [ source lang=csharp ] [ /source ] (minus the spaces of course) to preserve the tabs and other whitespace, which makes it a bit more readable.

Good luck with your endavours! [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this