|
Nothing, so go ahead and make the public field and enjoy the backward compatibility.
But! If you want the setter to have different (more-restricted) access than the getter, use an automatic property.
public int MyIntValue { get; protected set; }
Using the automatic property to begin with makes changing your mind later less problematic.
|
|
|
|
|
Now that makes a heck of a lot more sense. I didn't know split access is allowed like that. Thanks.
|
|
|
|
|
In addition to the reasons given in the response from PIEBALDconsult, if you are following the coding style guidelines (and I guess that you aren't ) you could also run into problems if you later need to change the access or the setting methods. Even if you do not follow the guidelines, the same circumstances could trip you up, requiring a great deal of retyping of references to the member, because there are often circumstances where accessing the member via the changed accessor could give side effects, where accessing the member directly wouldn't.
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.”
|
|
|
|
|
Inherently, the use of properties makes it a lot easier to serialize your class than if you use fields - not so ridiculous after all.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Ah, serialization. 99% of what I do is SQL Server-driven web applications. I can't remember the last time I needed serialization.
So apparently the "trivial property" has its uses, just not in my small little world 
|
|
|
|
|
I thought serialization was only useful for that Web crap, which is why I don't use serialization either.
|
|
|
|
|
Get/set make it easier to change how access to the property is handled, WITHOUT having to change your code every place the property is referenced.
For example, you could make the property (in effect) virtual, calculating it only if/when it's needed. Or you could easily add pre/post-processing to property accesses.
Another benefit is it gives you a location to set a breakpoint when debugging, so you can see WHERE accesses to this property are coming from.
|
|
|
|
|
Hi everyone.
I have a coded a Proxy Server like code that works really fine for me!
But there is an stupid problem!
When I begin surfing the internet through my program (using the proxy setting in firefox) at the beginning, the CPU usage is normal with some times raising up to 2 and then back to 0 again, but as soon as I open 2 or 3 pages in firefox, specially the page with flv videos, the CPU usage raises up to 99 percent!
Here is my TCP process code:
<br />
private void ClientConnectionHandler(object client)<br />
{ <br />
TcpClient tcpClient = null;<br />
<br />
NetworkStream networkStream = null;<br />
<br />
TcpClient remoteTcpClient = null;<br />
<br />
NetworkStream remoteNetworkStream = null;<br />
<br />
try<br />
{<br />
tcpClient = (TcpClient)client;<br />
<br />
const int bufferSize = 1024;<br />
<br />
byte[] buffer = new byte[bufferSize];<br />
<br />
tcpClient.ReceiveBufferSize = bufferSize;<br />
<br />
tcpClient.SendBufferSize = bufferSize;<br />
<br />
networkStream = tcpClient.GetStream();<br />
<br />
int bytesRead = networkStream.Read(buffer, 0, bufferSize);<br />
<br />
string request = Encoding.ASCII.GetString(buffer, 0, bytesRead);<br />
<br />
string host = "";<br />
<br />
string[] headers = request.Split(new string[] { "\r\n" }, StringSplitOptions.None);<br />
<br />
foreach (string header in headers)<br />
{<br />
if (header.ToLower().Replace(" ", "").Contains("host:"))<br />
{<br />
host = header.Replace(" ", "").Split(':')[1];<br />
}<br />
}<br />
<br />
IPAddress[] hostAddresses = Dns.GetHostAddresses(host);<br />
<br />
remoteTcpClient = new TcpClient<br />
{<br />
ReceiveBufferSize = bufferSize,<br />
SendBufferSize = bufferSize<br />
};<br />
<br />
remoteTcpClient.Connect(hostAddresses[0].ToString(), 80);<br />
<br />
remoteNetworkStream = remoteTcpClient.GetStream();<br />
<br />
remoteNetworkStream.Write(buffer, 0, bytesRead);<br />
<br />
bool eof =<br />
request.Contains("\r\n");<br />
<br />
while (!eof)<br />
{<br />
bytesRead = networkStream.Read(buffer, 0, bufferSize);<br />
<br />
remoteNetworkStream.Write(buffer, 0, bytesRead);<br />
<br />
eof =<br />
Encoding.ASCII.GetString(buffer, 0, bytesRead).Contains("\r\n");<br />
}<br />
<br />
bytesRead = remoteNetworkStream.Read(buffer, 0, bufferSize);<br />
<br />
int received = bytesRead;<br />
<br />
networkStream.Write(buffer, 0, bytesRead);<br />
<br />
string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);<br />
<br />
int contentLength = 0;<br />
<br />
headers = response.Split(new string[] { "\r\n" }, StringSplitOptions.None);<br />
<br />
foreach (string header in headers)<br />
{<br />
if (header.ToLower().Replace(" ", "").Contains("content-length:"))<br />
{<br />
contentLength = int.Parse(header.Replace(" ", "").Split(':')[1]);<br />
}<br />
}<br />
<br />
int headerLength = response.IndexOf("\r\n\r\n") + 4;<br />
<br />
int totalLength = headerLength + contentLength;<br />
<br />
eof = (received == totalLength);<br />
<br />
while (!eof)<br />
{<br />
bytesRead = remoteNetworkStream.Read(buffer, 0, bufferSize);<br />
<br />
BytesRecieve += bytesRead;<br />
<br />
received += bytesRead;<br />
<br />
networkStream.Write(buffer, 0, bytesRead);<br />
<br />
eof =<br />
(received == totalLength);<br />
}<br />
}<br />
catch (Exception e)<br />
{<br />
Console.WriteLine(e.Message + "\r\n\r\n");<br />
}<br />
finally<br />
{<br />
if (tcpClient != null) tcpClient.Close();<br />
<br />
if (networkStream != null)<br />
{<br />
networkStream.Close();<br />
<br />
networkStream.Dispose();<br />
}<br />
<br />
if (remoteTcpClient != null) remoteTcpClient.Close();<br />
<br />
if (remoteNetworkStream != null)<br />
{<br />
remoteNetworkStream.Close();<br />
<br />
remoteNetworkStream.Dispose();<br />
}<br />
}<br />
<br />
Thread.CurrentThread.Abort();<br />
}<br />
I start the TcpListener usign this method:
<br />
private void StartServer()<br />
{<br />
var tcpListener = new TcpListener("127.0.0.1",777);<br />
<br />
tcpListener.Start();<br />
<br />
ThreadPool.SetMaxThreads(5, 5);<br />
<br />
while (AcceptConnection)<br />
{<br />
TcpClient tcpClient = tcpListener.AcceptTcpClient();<br />
<br />
ThreadPool.QueueUserWorkItem(ClientConnectionHandler, tcpClient);<br />
}<br />
}<br />
And also, I used these in a console program.
Does anyone know the problem please?
Sojaner!
|
|
|
|
|
Try to use bigger bufferSize, so that it takes less itration.
or
Try to create a seperate thread to receive the data, Thread are lightweight then process. so it might restrict the cpu usage.
|
|
|
|
|
Hi,
This question is related to the Calenday Dayview control done by Ertan Tike (Calendar DayView Control[^]) My question is how do I modify it that it stores the title, startdate, enddate, colors and so forth in a SQL server DB rather than in the list?
Any help will be much appreciated.
Thanks
|
|
|
|
|
An Enigma wrote: Calenday Dayview control done by Ertan Tik
Let me call out to his desk, and ask him. ERTAN !!!!
Oh, wait. This site is full of articles from people all around the world. The odds that anyone who reads this forum, is him, are low. If you have a question about an article, you need to ask in the forum under that article.
Having said that, it sounds like a straightforward programming task to me, just write some SQL and connect it up.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
My intention wasnt to ask a direct question to Ertan, I referenced his work so that somebody can have a look at his code, since I am very new to C#. I dont know which part of the code to modify since he uses a list to store the info in.
Thanks
|
|
|
|
|
I teach C# at a local college. I'm starting a new C# II semester in 2 weeks and I'm always looking for new
and interesting projects for the students to do.
It's a WinForm class, and should incorporate some simple SQL. The focus of the class is more advanced topics
such as events, threads and the like.
I'm open to suggestions.
Everything makes sense in someone's mind
|
|
|
|
|
Some Microsoft Press books or O'reilly books would be a good start. Checkout Amazon.com
|
|
|
|
|
Chat/messaging program / homework assignment program / test-taking program.
You post messages to the database (could be a homework assignment or a test question) and they need a client program to access it and then they can reply. Their client should poll the database periodically.
However, they probably shouldn't have direct access to the tables. 
|
|
|
|
|
Hi guys,
I am in a bit of dilemma and need some help please!!!
Here is my scenario
I have a couple datagridviews which populate data from a dataset
I have cell_click event which is used to move data from one grid to another. If there is no empty cell in my grid2 then I add a column in the cell click event and populate data into the new column cell.
I also have a drag drop event which I use to move data across grid2, after I do the cell click for some reason it doesn't let me do drag drop, it says object reference not set to an instance of an object in the new cell where the data is being dropped. My assumption is that after I add the column, for some reason there is a null reference on all cells except the added data cell. How do I add the column, update the dataset and the datagridview without reloading the data in the grid.
Hope I make sense. Please helppppp
Sameer
|
|
|
|
|
Without seeing the relevant code, and only relevant code please, it is almost impossible to give a sensible answer. Except to say that adding a column to the DataGridView , does NOT add a column to the underlying DataTable /DataSet , which may be where the null reference exception is occurring.
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.”
|
|
|
|
|
dear all
when using unsafe code in c#. How these codes are compiled. What i mean
these codes are compiled under clr or not?
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
MSIL also contains IL codes for pointer manipulation such as ldloca (load address of local) and stind (store value indirect) ldflda (load address of field)
|
|
|
|
|
|
If Yes, Than why do we need to access pointer.
By Yes it Means these codes should be changed to MSIL and another thing CLR manages the memory for .Net Application. Then How these c++ variables are not managed without using the keyword fixed?
If I am wrong please correct me.
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
Abdul Rahman Hamidy wrote: these c++ variables
They're not "C++ variables"; they're still C#.
|
|
|
|
|
Well, I really didnt realized the fact.
when using Pointers It means we are accessing the Memory directory without any layer.
when c# statement It means we are not able to directly access the Memory there should a layer to provide access to memory.
when using the application is fast. specially in loops when I checked it.
but If it runs Under CLR then Y its faster than c# as It is managed by CLR.
Please correct me in my points.
Abdul Rahaman Hamidy
Database Developer
Kabul, Afghanistan
|
|
|
|
|
My understanding is...
If it's written in C#/CLR it's managed.
Unsafe doesn't mean unmanaged.
In an unsafe context you can write managed code that can access unmanaged things.
Abdul Rahman Hamidy wrote: when using the application is fast. specially in loops
Yes, I hear that you can use unmanaged code to do that, but I expect it's better to write a DLL in C/C++ and then use an unsafe context to access that DLL.
But I could be wrong.
|
|
|
|
|
Hi There,
I have been running around on the net with regards to this question and i cannot seem to find a straight answer. I have created the following class with a list property which is of type System.Collections.Generic.List named "FFamily", one will note that i have also implemented the destructor to release the said list:
public class Person
{
private string FName;
private int FAge;
private List<person> FFamily;
public string Name
{
get { return FName; }
set { FName = value; }
}
public int Age
{
get { return FAge; }
set { FAge = value; }
}
public List<person> Family
{
get { return FFamily; }
}
public Person()
{
FFamily = new List<person>();
}
~Person()
{
FFamily.Clear();
}
//public static Comparison<person> AgeComparison =
// delegate(Person P1, Person P2)
// {
// return P1.Age.CompareTo(P2.Age);
// };
//public static Comparison<person> NameComparison =
// delegate(Person P1, Person P2)
// {
// return P1.Name.CompareTo(P2.Name);
// };
}
I have the following code on the main form to test the functionality:
Person Pers1 = new Person();
Pers1.Age = 24;
Pers1.Name = "Zemus";
Person Pers2 = new Person();
Pers2.Age = 2;
Pers2.Name = "Adrian";
Pers1.Family.Add(Pers2);
List<person> Lst1 = new List<person>();
Lst1.Add(Pers1);
foreach(Person PersTest in Lst1) {
MessageBox.Show(PersTest.Name);
foreach (Person PerTest2 in PersTest.Family)
{
MessageBox.Show(" - "+PerTest2.Name);
}
}
Lst1.Clear();
Now according to various articles on the internet it is not necessary to release any of the above mentioned lists and / or items be managed by the GC. I tried to see for myself whether this happens or not.
First thing i did was run the application and keep pressing the button to execute the code block over and over and watched the task manager and it kept on going up.
The second thing i did was call GC.Collect() and MessageBox.Show(GC.GetTotalMemory(false).ToString()) and i saw that the system would now and again drop this value (or do a collection I assume). Just before the count went down it would fire the destructor for the Person class and clear the Family list.
Now i want to know, considering the main list i am instantiating and considering that I am creating items with nested lists would the GC clean up the main list and all the "Person" objects with their "Family" lists? Do i not need to concern myself with the collection of these items? List does not need to implement "IDisposable" to be managed by the GC?
You see, i have many years of Delphi behind me where the creation and release of memory with respect to object instantiation was all explicit.
Thanks,
Anton
|
|
|
|