4 Replies - 3946 Views - Last Post: 05 January 2008 - 10:12 AM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Question about ref in C#

Post icon  Posted 05 January 2008 - 09:35 AM

I am in the process of porting some code to C# from VB.Net, one of the functions:


#region getConstant
public static object getConstant(ref string key)
{
	object tmpObj;
	if ((myCache[constantIdentifier + key] != null))
	{
		tmpObj = (object)myCache[constantIdentifier + key];
	}
	else
	{
		tmpObj = pullConstantFromFile(ref key);

		//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



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: 05 January 2008 - 09:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about ref in C#

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5929
  • View blog
  • Posts: 12,851
  • Joined: 16-October 07

Re: Question about ref in C#

Posted 05 January 2008 - 09:56 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");?

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about ref in C#

Posted 05 January 2008 - 09:58 AM

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

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
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Question about ref in C#

Posted 05 January 2008 - 10:03 AM

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: 05 January 2008 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Question about ref in C#

Posted 05 January 2008 - 10:12 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:

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: 05 January 2008 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1