|
ok, got it solve
but it is very straing
this not work:
-------
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();
try {if (q != null) q.school_ID = null;} cathc{ }
-------
following works:
int k = 0;
var q = (from d in sdc.student
where d.id == 2
select d).FirstorDefault();
try {if (q != null) k = 1/k;} catch { q.school_ID = null;}
---------------------------
Really don't know why when write this inside catch{} it works if write outside not work
Pleae check
Thanks
|
|
|
|
|
I don't see why this should not work.
What you mean by not work? Gives you an error, does nothing or what?
|
|
|
|
|
I've been using properties for quite some time, and it was until today that a question came to me: what's the difference between using a property and a variable? I know properties are better practice, but still... just wondering .
public int Number
{
get
{
return this.number;
}
set
{
this.number = value;
}
}
vs
public int number = 0;
|
|
|
|
|
You generally have more control over a property, but if you don't need that control then it's just overhead.
And have you looked at automatic properties (or whatever they're called)?
|
|
|
|
|
|
If you don't 'need' the private variable yourself but are just using it to hold a property's value with no validation then you can do this and the compiler takes care of it all for you. Personally I never use them as I like to be in control of what's happening.
public int Number { get; set; }
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)
|
|
|
|
|
What would be the initial value of the property?
|
|
|
|
|
Whatever the intial value of the data type - 0 in the case of an int. All value types have an initial value. Reference types will be null (I believe - I haven't checked) as no instance has yet been created.
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 like these, simple in the above style and can easily be expanded to a normal property if required.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
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.
|
|
|
|