Welcome to Dream.In.Code
Become a C# Expert!

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!




Question about ref in C#

 
Reply to this topicStart new topic

Question about ref in C#

PsychoCoder
5 Jan, 2008 - 08:35 AM
Post #1

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,010



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

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


CODE

#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



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
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Question About Ref In C#
5 Jan, 2008 - 08:56 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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.

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About Ref In C#
5 Jan, 2008 - 08:58 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,010



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Question About Ref In C#
5 Jan, 2008 - 09:03 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif

This post has been edited by Martyr2: 5 Jan, 2008 - 09:04 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Question About Ref In C#
5 Jan, 2008 - 09:12 AM
Post #5

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,010



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:28AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month