|
Is it possible for a class to call one of its own get/set accessors?
For example:
public class MyClass
{
double m_dbl;
public MyClass(double dbl)
{
m_dbl = dbl;
}
public int DummyProperty
{
get
{
return (int)m_dbl;
}
set
{
m_dbl = (double)value;
}
}
}
|
|
|
|
|
It's the same whether you are outside the class or inside the class.
DummyProperty = yourValue;
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Yes. You could have easily determined this by a test-- calling the accessor from the constructor and checking the result, maybe with a WriteLine statement in the setter. I'm not griping, just trying to save you some trouble for next time!
Your question is a good one; writing your code this way is often a smart thing to do for maintainability-- this way any validation for each property can be located in one place. I believe it could in some cases slightly degrade performance over setting your properties in the constructor, since each property set amounts to a method call, but the .NET compiler should in any even inline those calls unless they're over a certain size limit.
Conceptually speaking, you probably could've guessed the answer without testing if you'd written it this way:
this.DummyProperty = dbl;
As for me, I never use 'this' except for in a few specific situations, like writing a copy method.
Regards,
Jeff Varszegi
|
|
|
|
|
I know you already got your answer: Yes. But, I wanted to expand upon this a little...
I would say that you always want to access your properties through their accessors. Obviously, you can't always do this (i.e. read-only properties); but, it's an ideal that I suggest you try to adhear to. My reasoning behind this is that you might have business logic tied into your accessor methods that should always be used. You've showed this in your example, but let me show you another:
public class User
{
private string m_strName;
private string m_strPassword;
public User(string name, string password)
{
Name = name;
Password = password;
}
public string Password
{
get { return m_strPassword; }
set
{
if ( ValidPassword(value) )
m_strPassword = value;
}
}
...
} With this example, I'd have to do the User.ValidPassword(string) check every time I wanted to set the value. Instead, if I keep this logic in the setter, and just plan to always use that, I don't have to worry about it.
Of course, you won't always have special logic to put into your getters and setters. But, it would be better to be in the habbit of using them, so when you need to do it that way, you're not trying to change your ways.
Michael Flanakin
Web Log
|
|
|
|
|
Hi,
I have a datagrid populated from an XML file.
<pairs>
<pair>
<x>1</x>
<y>1</y>
</pair>
<pair>
<x>2</x>
<y>8</y>
</pair>
...
</pairs>
When I click the headers to sort it, they seem to be sorted as if the values were strings (ie a list of 1 to 10 comes out 1, 10, 2, ...)
How do I persuade them to be treated as numbers (doubles)?
---
J
|
|
|
|
|
You should create Schema for your XML files, you can create a file with .xsd extenstion and you indicate your types in it. For the start point see XML Schema Reference (XSD) topic in MSDN. I think there are some examples in this site too.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Thanks - I was aware of XSDs, but didn't believe they'd affect datagrid behaviour - I shoud have just tried it shouldn't I?!
---
|
|
|
|
|
jazzle wrote:
but didn't believe they'd affect datagrid behaviour
DataSet.ReadXmlSchema()
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
thanks, will make sure it's read.
---
|
|
|
|
|
I created an XSD using the built-in in Vis Studio, edited it to use doubles instead of strings, but it still doesn't correct the problem, and I really can't see why.
any ideas anyone?
---
|
|
|
|
|
You can post a new Message to get more help and be more spcific about how to do it or what is your code.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
will do, thanks
---
|
|
|
|
|
How i can make a Partiture with musical syntax in word 2003 using C#
Andrei
|
|
|
|
|
I take it your talking about writing sheet music in Word 2003?
RageInTheMachine9532
|
|
|
|
|
There are a couple of musical fonts, but you cannot combine the symbols in word 2003, because word won't write multiple symbols across each other on specific positions.
I suppose you use a musical symbols font to write lines, notes etc. on a Bitmap, and don't use word at all.
|
|
|
|
|
Any ideas? I can sort of do it at the moment but they all render themselves on top of each other at (0, 0) instead of their correct locations.
"I think I speak on behalf of everyone here when I say huh?" - Buffy
|
|
|
|
|
I'm trying to figure out the size of base types in bytes so that i can use the FieldOffset attribute properly. However, i'm seeing contradictory information.
If a byte is 8 bits, then a 64 bit integer (ulong ) would take up 8 bytes in memory. So, i would assume that:
[FieldOffset(0)]<br />
ulong value1;<br />
[FieldOffset(8)]<br />
ulong value2;
would be the correct way of declaring this, however, in the FieldOffset documentation, they are declared as follows:
[StructLayout(LayoutKind.Explicit)]<br />
public class SYSTEM_INFO<br />
{<br />
[FieldOffset(0)] public ulong OemId;<br />
[FieldOffset(4)] public ulong PageSize;<br />
[FieldOffset(16)] public ulong ActiveProcessorMask;<br />
[FieldOffset(20)] public ulong NumberOfProcessors;<br />
[FieldOffset(24)] public ulong ProcessorType;<br />
}
these long values are using 4 bytes, which is a 32 bit memory block. Won't they over lap and cause the program to overlap, or behave weird?
Cheers
Cata
|
|
|
|
|
ulong / 2 = uint Hint look at the proper class names
top secret
|
|
|
|
|
I'm still confused. How does that tie in with the size? Why declare a ulong for a uint sized memory block?
I was looking at these pages:
http://www.developerfusion.com/show/4392/3/
http://authors.aspalliance.com/aspxtreme/aspnet/types/basedatatypestable.aspx
Just trying to put 2 and 2 together and get 4 bits.
Cata
|
|
|
|
|
|
Hmmm, i can see that declaring ulong in C# in a 4 field offset would not be a problem, providing the value never exceeds the max / min of integer. But why declare it as ulong in C# in the first place when it would be better handled as an int?
Otherwise, if C# forwards a value greater than max of int, it will crash the com component because of overlap?
Cata
|
|
|
|
|
Thats why u should be using uint instead, not ulong. The C function expects 32bits, thats all that matters. On the C# side u just need to know whether the value is signed or unsigned.
top secret
|
|
|
|
|
So how does C handle 64 bit integers? uVeryBloodyLong?
|
|
|
|
|
either with long long or unsigned long long
top secret
|
|
|
|
|
i want to create a graphical interface in wich i have a treeview
i want to drag a node from a treeview in a listview (like in windows explorer) but i want to put, at the mouse position in the list view only a picture (icon)
how cant i do this??
Ps: i'am new in windows form programing
|
|
|
|