|
Thank you once again.I just declared a variable and assign a value to it.
|
|
|
|
|
This may sound realy stupid, but are you actually debugging?
If you create a new (for example) console project, and enter a single line
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
int i = 1;
}
}
}
Then click to the left of the line to create a breakpoint, does a red dot appear?
When you run it, how are you doing that? Try running it from the menu "Debug...Start debugging"
What happens when you start the program using "Debug...Step into"?
I apologise if this seems a little too simple and basic, but sometimes it is worth starting at the begining.
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
|
|
|
|
|
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.
|
|
|
|
|
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.”
|
|
|
|
|
is C# has readonly keyworld ? and how it use in declare a property in Class ?
|
|
|
|
|
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.”
|
|
|
|
|
ok thank Henry Minute
|
|
|
|
|
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;
}
}
|
|
|
|
|
why i cannot use "MessageBox.show" in my class ? i want to show when catch an error ...
|
|
|
|
|
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
|
|
|
|
|
If it's a property you don't even need the readonly keyword, you could make the getter public and the setter private (or protected)
|
|
|
|
|
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";
public 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.
|
|
|
|
|
Hi guys. I got a problem I cant find solution to anywhere over the internet..I might have missed something in my logic.
<pre>
class DefComparing:DefCompareProperty
{
public void DefCompare()
{
List<string> def_compare = new List<string>();
********doing something to the list here******
DefCompareProperty df = new DefCompareProperty();
foreach (string s in def_compare)
{
df.SetDefComp(s);
}
}
}
</pre>
Then I want to use the def_compare list in another class.
<pre>
public class Aliniere
{
public List<string> def_compare = new List<string>();
public void Aliniate()
{
******doing some operations here****
DefComparing newdefcomp = new DefComparing();
newdefcomp.DefCompare();
def_compare = df.GetDefComp(); //Trying to get the list from that other class
</pre>
What I tried to do is make Set/Get properties for the list.
<pre>
class DefCompareProperty
{
private List<string> lst = new List<string>();
public List<string> GetDefComp()
{
return lst;
}
public void SetDefComp(string members)
{
lst.Add(members);
}
}
</pre>
So the problem is I cant get the list from class1 to class 2 using class3(set/get). I am doing some mistake. Can you help?
Thanks in advance
|
|
|
|
|
i would say in your SetDefComp you need to simply pass it the list and assign that to lst but that shouldnt really make to much difference with the code you have given.
Are you getting any error messages? or is the list just empty?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
When I try to make
foreach(string s in def_compare)
Console.Writeline(s);
It doesnt write anything.I guess the list is empty.
I tried how you told me to:
in class 1:
DefCompareProperty df = new DefCompareProperty();
df.SetDefComp(def_compare);
in class 3:
public void SetDefComp(List<string> members)
{
lst = members;
}
And I still have an empty list in class 2.
|
|
|
|
|
do some debugging in your code then...
Put a message box in to show the length of the list before you try to write it...
System.Windows.Forms.MessageBox.Show(def_compare.Length.ToString());
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
It doesn't work because your creating a new instance of DefCompareProperty . Each new instance has its own List and they are kept completely separate.
You can do this two ways, make lst in your DefCompareProperty class static so that there is only one of them which all instances share. Or pass an instance of the DefCompareProperty class to each of your other classes to use so that they both use the same one.
EDIT: If you are passing the same instance around already then damn, I don't know what's going on.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
I went for static in get/set class and it worked fine
Thanks very much for the easy solution SK Genius.
Thanks to all for trying to help too.
Cya
|
|
|
|
|
Hi,
you got it rather twisted. Basically what you have is:
1.
a base class DefCompareProperty which holds a list that you can add to, and get.
2.
a derived class DefComparing which inherits from DefCompareProperty, so it too holds the list DefCompareProperty is holding.
3. but then, inside the DefCompare method, you have the line DefCompareProperty df = new DefCompareProperty(); which adds a DefCompareProperty inside the DefComparing object (which is a DefCompareProperty itself), so now DefCompareProperty is actually holding two lists (until DefCompare method is done). Then you add items to the second list, and the method ends, so the second list no longer exists, and it all doesn't make much sense.
So, you are getting everyone confused by using very similar class names; also the SetDefComp() method is badly named, it should reallybe called add since it does not set anything, it adds something.
I would do things differently, something along these lines (not tested):
public class DefCompareProperty : List<string> {}
public class DefComparing:DefCompareProperty {
public void DefCompare() {
this.Clear();
}
}
The main differences are:
1. my classes don't hold lists, they are lists.
2. I operate on the existing list, not on a new one.
Of course you could as well forego class DefCompareProperty and simply write public class DefComparing:List<string> {...}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
No why couldn't the OP make it sound as simple as that... good work Luc
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Luc always uses the force.
|
|
|
|
|
The Force as I see it consists of a few very simple rules. Sort of ten commandments.
One of them is: if it does not work, and/or you don't understand it (isn't that almost the same?) try making it simpler. Simpler code means better understanding AND fewer bugs (and often better performance too).
another one is: use proper names for everything, it really matters.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|