|
-
Nov 3rd, 2005, 11:32 AM
#1
Thread Starter
Addicted Member
[Resolved] - Constness in C# like C++
Hi
I've read that the modifier const in C# is not possible. Does someone have a workaround / different implementation to have this sort of features.
I'm writing a Dll in C# and I would like to be able to certify that my functions / methods are read-only has it would be in C++ with the const modifier.
Thks
Ps: what's a "Mort" i've read it a few time and I don't know what that mean? (It should be something around different programmers style I guess)
Last edited by Megistal; Nov 3rd, 2005 at 06:42 PM.
-
Nov 3rd, 2005, 12:05 PM
#2
Re: Constness in C# like C++
Unless you explicitly tag a parameter with the modifier "ref", parameters are passed by value - hence they cannot be modified.
-
Nov 3rd, 2005, 01:06 PM
#3
Thread Starter
Addicted Member
Re: Constness in C# like C++
Quite difficult when you have an array...
It will by pass by ref not by value.
Last edited by Megistal; Nov 3rd, 2005 at 01:13 PM.
-
Nov 3rd, 2005, 06:36 PM
#4
Re: Constness in C# like C++
Like any class, if you pass an array to a method then you cann modify its properties but you cannot modify the reference, i.e. you can set any of the elements but you cannot assign a new array to the variable. If you don't ant that to be the case then you'll need to pass each element in individually, which you could do using the params keyword if you have an unknown number of elements. That's a bit messy though.
-
Nov 3rd, 2005, 06:42 PM
#5
Thread Starter
Addicted Member
Re: Constness in C# like C++
Yeah
That's why I said it will be quite difficult for an array. And Yes there's a lot of array for that function and I can't let an unknown number of arguments.
Well well well I guess they only thing I can do is write on the tech paper that this function does not modify the arguments.
Thks
-
Nov 4th, 2005, 12:14 AM
#6
Hyperactive Member
Re: [Resolved] - Constness in C# like C++
You could expose the array as a read only property array right?
I'm still a C# noob, but I know you can do it in VB.
This is a read only property. But it's not an array. I can't figure out how to make it an array, but I KNOW you can do it with VB.NET, so you must be able to do it with C#
Code:
class Class1
{
public int MyPropArr
{
get
{
return 1;
}
}
}
-
Nov 4th, 2005, 12:30 AM
#7
Hyperactive Member
Re: [Resolved] - Constness in C# like C++
Ok, I can't find any examples how to treat a property like an array. I refuse to believe that C# lacks this ability. So I vow to figure it out. But I did find this.
This code will let your class itself act like a read only array
Code:
public string this[int i]
{
get
{
return this.WhateverStringValueYouWant(i);
}
/**
set
{
this.WhateverStringValueYouWant(i) = value;
}
**/
}
As long as you don't have the "set" block in your property definition, it's read only. You can include "set" and omit "get" to make it write only property too.
-
Nov 4th, 2005, 11:05 AM
#8
Thread Starter
Addicted Member
Re: [Resolved] - Constness in C# like C++
Thks for trying umilmi81 but what you provide is an "assesseur" (I don't know the english word for that)
The "get" function tell others that use your class that you can get the variable or result from this function and the "set" one is to properly set the variable. It is appropriate when you have a private variable in your class and you want to do some process before using it (verification, hashing etc)
The "get" and "set" let you control what the information you let go and what you want to receive for a private/protected variable (or array) in your class (on the other hand, you have no control over a public variable).
My problem is: I want, from the code, to provide a warranty that the parameter use in the function cannot be modified by the called function. Thus the use of const like in C++ was perfect for this but I read that it doesn't exist in C# so I was wondering is someone have found a workaround that problem.
The only workaround I found is to write it down on the tech paper :/ More or less a warranty.
-
Nov 4th, 2005, 11:09 AM
#9
Re: [Resolved] - Constness in C# like C++
const is good for C/C++ function prototypes. It doesn't apply quite so much in an OO model. Also, it is fairly redundant - the proper place for it is in the documentation as you are doing.
-
Nov 4th, 2005, 11:28 AM
#10
Thread Starter
Addicted Member
Re: [Resolved] - Constness in C# like C++
penagate, you're right that in OO, const is less needed but I guess const have it's place in this situation (and others too):
Having a static class that provide functions. Something like a math or a processing class that don't need to be instanciate. All functions provided a result and each functions are mostly independant. They are regroup only by their meaning (mathematical solutions for examples for the Math Class.).
The need here for a const function is, from my point of view, very important. Of course we can do without, but I prefer to use it when I can. It tell user that your messing around with their parameters but after the call they are sure that they are not modified, by the compiler, not a piece of paper.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|