Join 137,420 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,981 people online right now. Registration is fast and FREE... Join Now!
//Create the cache dependencies and insert the object into the cache if ((tmpObj != null)) { if (myCache[constantKey] == null) { myCache.Insert(constantKey, DateTime.Now); } myCache.Insert(constantIdentifier + key, tmpObj, new System.Web.Caching.CacheDependency(applicationConstantsFileName)); } } return tmpObj; } #endregion
But when I try private static int thisVersion = getConstant("THIS_VERSION"); I get 2 errors:
The best overloaded method match for 'CM.ConstantsManager.getConstant(ref string)' has some invalid arguments
Argument '1' must be passed with the 'ref' keyword
Can I get away with the ref keyword all together, here is the VB.Net version of the same function
CODE
Public Shared Function getConstant(ByRef key As String) As Object Dim tmpObj As Object If Not (myCache(constantIdentifier & key) Is Nothing) Then tmpObj = CType(myCache(constantIdentifier & key), Object) Else tmpObj = pullConstantFromFile(key)
'Create the cache dependencies and insert the object into the cache If Not IsNothing(tmpObj) Then If myCache(constantKey) Is Nothing Then myCache.Insert(constantKey, now) End If myCache.Insert(constantIdentifier & key, tmpObj, New CacheDependency(applicationConstantsFileName)) End If End If Return tmpObj End Function
So in my mind the ref keyword in the C# version isn't even necessary, am I correct?
To make things easier this class is being used to read constants from am XML file that can be shared across multiple applications, even Web, all from one XML file so the actual value of "THIS_VERSION" is an integer value
This post has been edited by PsychoCoder: 5 Jan, 2008 - 08:38 AM
I'd ditch the ref if you can. In the context of string, in makes no sense. It's an evil C thing and should be avoided, shame on them. ref int makes sense, ref anyNonPrimativeType doesn't, AFAIK.
Also, maybe private static int thisVersion = getConstant("THIS_VERSION"); should read private static object thisVersion = getConstant("THIS_VERSION");?
Problem solved, see what happens when you write code with no coffee. to pass a ref variable it has to be assignable, so I created private static versionConstant = "THIS_VERSION"; then my call
CODE
private static int thisVersion = (int)getConstant(ref versionConstant);
And the build was successful, and from what I cal tell it works fine now. Thanks for all who read this
Your choices are that you can include the ref keyword when you call the getConstant function (you need ref both when calling and in the function signature if you are going to use ref... like it is advising you in the error) or it looks like you can do away with ref being that you are just passing a simple string and you don't appear to be modifying it (I could be wrong since I don't know what is going on in pullConstantFromFile).
Just to make sure that you stick closest to the VB.NET version that you know is working you could include ref, get it working, and then see if you can simple take out all the ref keywords.
Edit: Glad you got it solved.
This post has been edited by Martyr2: 5 Jan, 2008 - 09:04 AM
You understood most of the question. I am porting a class I wrote a while back which reads constants from an XML file, with this class these constants can be shared across multiple applications. I originally wrote it in VB.Net (God only know why) and am now porting it to my language, C#, and have ran into a couple problems with the porting. I need ref because the value can change before it it passed to the calling method so ref covers that possibility.
But to pass a value using ref it has to be assignable, my way (from the VB file) wasn't assignable, so I solved the problem like so:
CODE
private static string versionConstant = "THIS_VERSION"; private static int version = (int)getConstant(ref versionConstant);
That way I can keep my ref there (Damn C lol)
PS: "THIS_VERSION" is a constant in the XML file, I added version control to my XML document so it can have many items that are the same (db connection, email server, etc) all separated by their version number, so that is a value I need right away.
One the application loads the XML file is loaded and all the items I need for this version are loaded into the XML Cache so I no longer need to reference that file again, until the app closes, than any changes made to the values in the file I update the file, thus the ref reference.
This post has been edited by PsychoCoder: 5 Jan, 2008 - 09:12 AM