|
For future extensibility they are useful I suppose - but I've found I nearly always need more control so I do it the 'old fashioned' way! Also, not being able to have true read only properties this way (you have to have a set; - although it can have a private accessor) is a pain.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I use properties a LOT, I have a class for each table in my BLL and a property for each field in the table (all auto generated) so nearly all of these can be serviced by the auto property. Expanding an auto to a full property is really simple and can be done at any time.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
you know you can do this:
public int MyInt{get; private set; }
|
|
|
|
|
Yeah - in my post...
DaveyM69 wrote: although it can have a private accessor
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Auto-Implemented Properties
|
|
|
|
|
There are many reasons - a couple to get you started...
1. Using a property enables you to do validation etc within the object that holds the variable in the getter and setter methods.
Imagine if the number variable should be restricted to a certain range under certain situations. If you expose the variable directly you have no control over what goes into it - other than the data type. With a property you can test the value parameter, and raise an exception or set the variable to a different value etc.
2. Properties do not have to refer to an actual variable. e.g.
private int number1;
private int number2;
public int Sum
{
get { return number1 + number2; }
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
A property can be an interface member, whereas a variable can't be. This means changing the definition of a class to add/remove a property is a breaking change. Generally speaking if "Number" is part of the publically available API, then you want to expose it as a property.
A variable can be passed by reference (ref/out), a property can't be. A variable has only one access modifier, a property can have a different modifier on get/set.
In terms of overhead, the effort of implementation is quite small if you use automatic properties. int Foo { get;set; }. In terms of performance the simple accessor will usually be inlined by JIT, making the performance identical (except on forms - they inherit MarshalByRef).
Also as a property is effectively a stub of metadata pointing to getter and setter methods, they can benefit from things like declarative security, etc.
|
|
|
|
|
Hi,
i have seen that a similar question to this was asked before, but the answer was just use a textbox.
i am trying to create a xchat / linkinus style conversation window. i have tried using a rich text box, but it just doesnt give me the level of control over rendering i need. Is there a way i could have a custom control, that still has ability to select text.
i have also looked at the WebBrowser control but it seems to rely on IE, which has two problems, 1. overhead, 2. cross platform.
( i have found a custom html render, but it doesnt have selectable text. http://www.bruceshankle.com/_mgxroot/page_10764.html[^]
is it possible to give this control this ablilty? )
Thanks in advance.
Lloyd Sparkes
|
|
|
|
|
I know we have alot of great talent here on code project and I need some help in a project that is due by nov 20th if you can help me on my problem please notify me at your earliest convenient thank you. Robert Anders
|
|
|
|
|
I'm not sure how we can help when you have not told us what your problem is.
Also, you'll find that people tend to take a dim view of doing other people's homework projects. They will help you on areas in which you are stuck, but if they think they will end up doing most of the work they'll happily let you fail the assignment.
|
|
|
|
|
Sure, everyone here is here to help. Post the part of your code that's giving you problems and we'll give you some pointers so you can get past that and concentrate on finishing your project.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Hi, I have a question about XMLSerializer example.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(VS.80).aspx
The code has an method GenerateKey(), How is that method implemented?
Can't find the code on MSDN.
Bye and Thanks, Gywox
Hashtable serializers = new Hashtable();
XmlSerializer s = new XmlSerializer(typeof(MyClass), myRoot);
object key = GenerateKey(typeof(MyClass), myRoot);
XmlSerializer ser = (XmlSerializer)serializers[key];
if (ser == null)
{
ser = new XmlSerializer(typeof(MyClass), myRoot);
serializers[key] = ser;
}
else
{
}
|
|
|
|
|
Hi folks !!
I'm new here and am trying to teach myself (and loving it) c# !!
Can anyone please give me an insght as to why the two loops i've listed only ever seem to execute once ?
I've put message boxes in to see how many times it's going round and it always seems to show 0 ?
I'm confused !!
Many thanks (i think you'll all be seeing a LOT more of me !
Neil
int tempnum = 0;
while (tempnum < 10)
{
newLoc.X = oldLocation.X + 5;
newLoc.Y = oldLocation.Y;
if (newLoc.X >= (250))
{
newLoc = oldLocation;
}
cat.Location = newLoc;
tempnum = tempnum + 1;
MessageBox.Show(tempnum.ToString());
}
for (int i = 0; i < 20; i++)
{
newLoc.Y = oldLocation.Y - 5;
newLoc.X = oldLocation.X;
if (newLoc.Y <= (2))
{
newLoc = oldLocation;
}
cat.Location = newLoc;
MessageBox.Show("" + i);
}
|
|
|
|
|
Welcome!
Quickly looking, nothing in the code you provided seems to create such effect. Perhaps there's something you didn't include. Anyway, use debugger to see what actually happens and watches to observe the value of tempnum.
Mika
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|
|
Another Welcome
I agree with Mr. Mika ... really there is nothing in your code should produce such behaviour ... showing 0 (for first loop for example) means that the tempnum = tempnum + 1; statement is not executed and in such a case you will loop for infinity ! and each time the box will show you 0 !!! and will not show once as you mentioned ..!
Sincerely Samer Abu Rabie
Note: Please remember to rate this post to help others whom reading it.
|
|
|
|
|
This is horrible code. The while loop is pointless, just use a for loop.
Also, comments like 'close inner if statement' are ridiculous.
nlowdon wrote: newLoc.Y <= (2)
(2) is superfluous, just 2 is fine.
nlowdon wrote: "" + i)
Use i.ToString()
There is no way I can see for that second loop to not run 20 times. The first loop also looks fine, but the code is obviously harder to read.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks for the 1st two replys guys - as for the 3rd ??
Like i said - i am new to this and statements like "this is horrble code" doesn't really help !
Anyway, it maybe something to do with where or how the method is being called ?
I'll keep trying !!
Thanks again
|
|
|
|
|
nlowdon wrote: statements like "this is horrble code" doesn't really help
While the statement may not have been very diplomatic, it is helpful once you push a bruised ego out of the way.
Christian did point out area where the code could be improved.
Yes, the while loop in this instance would be better served as a for(;;) loop.
The comment on "close inner if statement" isn't as useful as it might appear. A more normal practice is to repeat the condition if you are going to have a comment there so you know what that code block was doing. However, if you need to do that then you might want to think about refactoring your code. If your code blocks are so long that you can no longer see the orginal opening condition then it is an indication that the code is poorly written. Also, tools such as DevExpress's CodeRush and Refactor will decorate your IDE so it makes it very clear what bracket belongs to what if/while/using/method/class/namespace/etc.
The issue regarding the brackets around the 2 is also valid. If it was a more complex expression and you wanted to be explicit about the order of precidence then fair enough, however for a simple literal value it is overkill. If you've moved from a language that needed that then it will take a little time to get used to not needing it. It took me some time to move from some habits I picked up over the years when writing C++ code after I moved to C#.
Finally the issue over "" + i is also valid. It reminds me of when I first started programming and I wanted a negative number I would write 0 - 10 until I realised I could just write -10 .
Don't take the comments too personally, Christian is trying to help in his own way.
|
|
|
|
|
Hi There,
Thanks for the reply, a little 'diplomacy' goes a long way.
In my original posting there was actually 2 instances of a similar loop - the 2nd being a 'for' loop.
Unfortunately that doesn't work either
The extensive commenting of code mearly helps me at the moment as i am very new to this (as the simple coding may suggest - lol)
Anyway - tried lots now and still no joy - the execution of the method is called by a timer which i've slowed right down also - no joy.
Thanks again for the post - nice to see a fellow 'Jock' ??
|
|
|
|
|
MessageBox.Show(tempnum.ToString());
}
//second loop
..
..
Remove the '}'
|
|
|
|
|
}
cat.Location = newLoc;
MessageBox.Show("" + i);
Should be ("" + i.ToString());
|
|
|
|
|
Maybe there are some exception? just check it!
|
|
|
|
|
Thanks for all your help. have another question.
I have a database name star. My star database have 4 tables contact, units invoices and payments
in my contact table I have columns contact id, Fname, LName, Address, City, State, zip, email.
in the invoice table I have columns invoice id, contact id, startdate, enddate, subtotal, total,
I wrote a little app from the help from here to migrate my data because SSIS 2005 was having problems with null value data in the contact table import perfectly and invoice imports as well.
But my contact id in the Contact table is not matching the contact id in the invoice table it blank.
I need help to make them match and also make sure that when I open the app that using the star database show the contact information for that contactid and when I open the form for the invoice
the contact information matches to. The app with an empty data base because I create new accounts trying to migrate old data so the software use it. This way I don't have to manual imput every record.
if I can get the a sample of some code in c#
Learning to Code
|
|
|
|
|
If the referential integrity is lost in the migration process, I don't think there's any way to restore the correct foreign keys afterwards (unless you have other logic in the data that can combine correct invoice and contact).
Based on your description, it seems that the referential integrity is not enforced in your database and this lead to these problems. What I suggest is that you repeat the migration, but this time before migrating the data, put constraints to enforce the integrity.
I take it, that you use identity columns in the new database. If this is the case, your migration logic should be something like:
while (ReadOriginalContact) {
InsertContactToNewDatabase
GetTheNewIdentityForContact
while (ReadOriginalInvoiceForThisContact)
InsertInvoiceToNewDatabaseWithNewContactId)
}
}
The need to optimize rises from a bad design.
My articles[ ^]
|
|
|
|