Click here to Skip to main content
16,011,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a solution which consist of a folder called 'one' which has a a .cs file called project.cs, I have a string value hear which is say string msg="Hello"

Same solution I have a form Form1.cs where I need to split the string and show in datagrid,but for that I need to get the string value msg in form1?How to do that?

even if i say one.msg i dont see msg intilesense.

Should i set and get as a property ?
Thanks in advance
Posted

Hi,
Solution for windows application.
in a.cs file declare your variable like this public string msg="hello".
and from form1 you can get the value like this.
<instance of a.cs class or file>.msg.toString().split('Char');
i.e.
C#
cls_a obja = new cls_a();
string[] strArr = obja.msg.toString().split('l');
 
Share this answer
 
If you created the project.cs file in the second folder, Visual Studio probably gave it a new NameSpace, probably called one. Therefore you would need to reference the string variable like this one.project.msg. This assumes that msg is declared as public. I would also say it would be better to make msg a public property of the class project, thus you would have

C#
namespace one
{
    class project
    {
        public string msg { get; set; }
    }
}


and would refer to it liek this
<br />
one.project.msg
.

Hope this helps
 
Share this answer
 
v2
The name of the file is pretty much irrelevant: it is the name of the class (including the namespace) which is relevant.
The other thing which matters is whether msg is a static string or not. If it isn't, then you can only access it via an instance of the class that contains it - it doesn't exist in isolation, only as part of an instance of a class. Think car: you can't put a map in the glovebox of "a car", you have to put it in the glovebox of "this car", "that car" or "my car". The car is the class, "my car" is an instance.

Then you come to access level. From outside the class, you can only access public items (this is a lie, but the full story is more complex than you need at the moment). So the best way to do it is:
1) Within the project class, in the project.cs file, declare:
C#
public string msg { get; set; }
This creates the msg string as a class level public property.
2) In Form1, declare an instance of the project class, and you can access it's msg property.
C#
project myProject = new project();
myProject.msg = "Hello";


There are still a few things here you should change as far as naming conventions go (Classes should start with a Capital Letter, so it should be Project not project, Properties the same, plus they should have full names, rather than abreviations: Message rather than msg, but this should get you started.
 
Share this answer
 
What's your class name? I assume it is Project (as it resides in project.cs). Next thing, you should create class instance in Form1 - say its name is project1. So...
First, you should make sure your variable msg is accessible from outside your class: for that you should declare it as public
C#
public string msg="Hello";

or, better, create a property
C#
private string _msg="Hello";
public string msg
{
  get 
    {return _msg;} 
  set 
    {_msg = value;} 
}


Next, you need to make your class visible in Form1. For that, open project.cs and find line namespace namespace1 in the beginning of the file (under using statements). This value, namespace1, is the namespace of your class.

Finally, to access your variable msg you need to use one of two methods:
1) Add namespace to using declaration: put
C#
using namespace1;

in the beginning of Form1.cs and simply use
C#
Project project1 = new Project();
... // some code here
string val = project1.msg;


2) Use full class name where you create class instance:
C#
namespace1.Project project1 = new namespace1.Project();
... // some code here
string val = project1.msg;


Method slightly difers if your Project class is static... let me know if it is, I'll explain this case is well
 
Share this answer
 

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