how to call 1 class method or constructor in other class ?
-
i have a class name DataConnection. and i want to call this in my form. so how to do with C# ? please give me some guide line ....
Well it all depends how you have them set up. Are your classes both in the same namespace? If so, then you can access the Class directly. Do you want to create an instance of the class? or have you already done that and what to access one of its functions? If you want to create an instance you can use
DataConnection dc= new DataConnection();
then call a function...
dc.FunctionCall();
from inside your form. Unless it is a static class in which case you cannot create an instance of it, but you can call any of its public methods like so...
DataConnection.FunctionCall();
Any help?
Life goes very fast. Tomorrow, today is already yesterday.
-
Well it all depends how you have them set up. Are your classes both in the same namespace? If so, then you can access the Class directly. Do you want to create an instance of the class? or have you already done that and what to access one of its functions? If you want to create an instance you can use
DataConnection dc= new DataConnection();
then call a function...
dc.FunctionCall();
from inside your form. Unless it is a static class in which case you cannot create an instance of it, but you can call any of its public methods like so...
DataConnection.FunctionCall();
Any help?
Life goes very fast. Tomorrow, today is already yesterday.
yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?
-
yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?
You need to create an instance first because, until you do, nothing will exist. You need to make sure that S is declared public as well. Alternatively, you can declare S as
public static
, in which case you can use it as you currently do. In any event get yourself a beginners C# book and work through it. You will save yourself a lot of pain.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
You need to create an instance first because, until you do, nothing will exist. You need to make sure that S is declared public as well. Alternatively, you can declare S as
public static
, in which case you can use it as you currently do. In any event get yourself a beginners C# book and work through it. You will save yourself a lot of pain.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
yes. please tell me more detail ... i have a project name Stock. and it has a form name "form", and a class name DataConnection. in DataConnection has only property "S" as string. and why i cannot access to "S" directly by "string str = DataConnection.S" ? why need to create an instand of it first ?
As Henry has said, you can access it the way you are trying only if the S string is static and publicly available. Which I am guessing is not the case. ensure you DataConnection class is similar to the following...
public class DataConnection
{
private string s = "default";//this is where the string value will be stored, assignment is not required
public string S{//this is a property that you can access with external class to get/set the value of the private string s
{
get{return s;}
set{s = value;}
}
}then in your main form you need to create an instance of your DataConnection class in which you can access the value of S, like so...
DataConnection dc = new DataConnection();
dc.S = "new value";
MessageBox.Show(dc.S);you need to ensure that your DataConnection instance is located in an appropriate place depending on where you want to access the data from i.e. mulitply functions would mean you should set it at a class level
Life goes very fast. Tomorrow, today is already yesterday.
-
xingselex wrote:
is C# has readonly keyworld ? and how it use in declare a property in Class
1. Yes. 2. public/protected/private (delete as applicable) readonly string S = string.Empty;
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
xingselex wrote:
is C# has readonly keyworld ? and how it use in declare a property in Class
1. Yes. 2. public/protected/private (delete as applicable) readonly string S = string.Empty;
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
my code is like this bellow . and why it message that "the modifier 'readonly' is not valid for this item " . what wrong with it ? sorry for my question. i'm just start to learn about coding... using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace Stock { class DataConnection { private string _ConnectStr = ""; public readonly string ConnectStr { get { return _ConnectStr; } set { _ConnectStr = value; } } public DataConnection() { string str = "Data Source=sokheng;Initial Catalog=stock;User Id=sa;Password="; try { System .Data .SqlClient .SqlConnection CN = new System.Data.SqlClient.SqlConnection() ; CN.ConnectionString = str; CN.Open (); _ConnectStr = str; CN.Close (); } catch(Exception ex) { } } } }
-
my code is like this bellow . and why it message that "the modifier 'readonly' is not valid for this item " . what wrong with it ? sorry for my question. i'm just start to learn about coding... using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace Stock { class DataConnection { private string _ConnectStr = ""; public readonly string ConnectStr { get { return _ConnectStr; } set { _ConnectStr = value; } } public DataConnection() { string str = "Data Source=sokheng;Initial Catalog=stock;User Id=sa;Password="; try { System .Data .SqlClient .SqlConnection CN = new System.Data.SqlClient.SqlConnection() ; CN.ConnectionString = str; CN.Open (); _ConnectStr = str; CN.Close (); } catch(Exception ex) { } } } }
-
You mixed "readonly" with a property, you could do this:
public string ConnectStr
{
get
{
return _ConnectStr;
}
private set
{
_ConnectStr = value;
}
} -
Your name is Rajdeep.NET and I claim my £5!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones