|
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.
|
|
|
|
|
I will be looking forward to you article with the full 10... Also perhaps a bible of some sort so we can have a religion that follows logic (maybe a little fuzzy logic too )
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Luc Pattyn wrote:
another one is: use proper names for everything, it really matters.
I like this one. Now we just need to get some way to bring eternal damnation into play if people don't follow your commandments... Hmm...
|
|
|
|
|
Paddy Boyd wrote: Now we just need to get some way to bring eternal damnation into play...
Not really. IMO not obeying such rules makes programming and debugging harder, self-inflicted punishment is provided automatically.
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.
|
|
|
|
|
hi, i am doing a program to plot time vs speed in excel file. but right now i have some problems into doing so. i am unable to convert the x-axis into time format.. below is the code i have written.... please help by giving examples... thanks lots!!
while (((Microsoft.Office.Interop.Excel.Range)oldsheet.Cells[row, column]).Value2 != null)
{
string splitcell = ((Microsoft.Office.Interop.Excel.Range)oldsheet.Cells[row, column]).Value2.ToString();
split = splitcell.Split(',');
int.TryParse(split[0], out hour);
int.TryParse(split[1], out min);
int.TryParse(split[2], out sec);
int.TryParse(split[21], out rpm);
time = new DateTime(1, 1, 1, hour, min, sec);
newsheet1.Cells[row, column] = time.ToString("HH:mm:ss");
newsheet1.Cells[row, column + 1] = rpm;
row++;
}
row--;
range = newsheet1.get_Range("A2", "B" + row.ToString());
xlChart.Legend.Clear();
Microsoft.Office.Interop.Excel.Axis x = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlCategory, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
x.CategoryType = Microsoft.Office.Interop.Excel.XlCategoryType.xlTimeScale;
x.BaseUnit = Microsoft.Office.Interop.Excel.XlTimeUnit.xlDays;
//x.MajorUnitScale = Microsoft.Office.Interop.Excel.XlTimeUnit.xlDays;
//x.MajorUnit = 0.0208333333;
x.HasTitle = true;
x.AxisTitle.Text = "Time";
Microsoft.Office.Interop.Excel.Axis y = (Microsoft.Office.Interop.Excel.Axis)xlChart.Axes(Microsoft.Office.Interop.Excel.XlAxisType.xlValue, Microsoft.Office.Interop.Excel.XlAxisGroup.xlPrimary);
y.HasTitle = true;
y.AxisTitle.Text = "Speed";
xlChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterSmoothNoMarkers;
xlChart.SetSourceData(range, Type.Missing);
series = (Microsoft.Office.Interop.Excel.Series)xlChart.SeriesCollection(1);
series.Format.Line.Weight = (float)0.25;
|
|
|
|
|
Im doing some working with creating and deleting access databases. Problem is I dont know how to write the syntax to save to the default program files folder.
IE
if(file.exists(@"C:\Program Files\blah blah))
file.delete(@"C:\Program Files\blah blah);
I would think Id be able to insert something in place of @"C:\Program Files that tells the system to do to the default program files folder whether it be program files or program file(x86).
Any suggestions.
I searched online but havent found anything, probably due to not really knowing what this would be called to search for it.
Thanks
|
|
|
|
|
string programFilesPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hi,
what musefan told you is the correct answer to your specific question, however you should not do that at all for several reasons:
1. the "Program Files" folder is not supposed to contain files directly; it holds folders, one per application.
2. the "Program Files" folder is a shared folder, all users can read it, chances are noone can write there (Vista would deny write access, the system admin might decide to make the folder read-only on all Windows versions.
Hence you should store your data files elsewhere; now the SpecialFolder has several useful values, such as AppData (user-specific location) and CommonAppDate (shared by all users).
[ADDED]There too you should obey a folder hierarchy, so create a subfolder either for your company or your product.[/ADDED]
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.
modified on Thursday, May 21, 2009 11:04 AM
|
|
|
|