Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing string by address

Posted on 2006-04-19
5
Medium Priority
?
234 Views
Last Modified: 2010-04-16
Hi I am trying to pass the starting and ending addresses of a string to a function:

string read = "whatever";

string newstring = Change(ref read[0], ref read[7]);
Trying to pass address of w and r for the string above to function Change

i want to do something like this:

private static void Change (ref int x, ref int y)
{
   *x = "c";
     or x = some other address location like read[2];
   
    same for y,
    *y = "c"
}

Is this possible in C#.  Thank you very much ahead of time.

   
0
Comment
Question by:nstefanov
5 Comments
 
LVL 5

Expert Comment

by:GENTP
ID: 16491282
use the unsafe block. You might have to config it in the Project Settings. Try this


private unsafe static void Change (ref int x, ref int y)
{
   *x = "c";
     or x = some other address location like read[2];
   
    same for y,
    *y = "c"
}
0
 
LVL 5

Expert Comment

by:AdGroot
ID: 16491449
use StringBuilder

private static string Change (string oldString, ref int x, ref int y)
{
            StringBuilder stringBuilder = new StringBuilder(oldString);
            char ch = stringBuilder[x];
            stringBuilder[x] = stringBuilder[y];
            stringBuilder[y] = ch;
            return stringBuilder.ToString();
}

Ad
0
 
LVL 5

Accepted Solution

by:
AdGroot earned 750 total points
ID: 16491486

You don't need the ref's before the int

private static string Change (string oldString, int x, int y)
{
            StringBuilder stringBuilder = new StringBuilder(oldString);
            char ch = stringBuilder[x];
            stringBuilder[x] = stringBuilder[y];
            stringBuilder[y] = ch;
            return stringBuilder.ToString();
}

Ad
0
 

Author Comment

by:nstefanov
ID: 16491587
Thanks AdGroot not exactly what I wanted but this looks like a great idea.  Will test and reward you the points if I can modify it to work.  Thanks again.

Nick
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16492414
This got me thinking..

I still tend to think to C++ like but, depending on what you're attempting to accomplish..


                  string sTest = "Michael";
                  int iStartPos = 2;
                  int iEndPos = 7;

                  string sResult = this.Change( sTest, iStartPos, iEndPos );



private string Change( string sOriginalString, int iStartPos, int iEndPos )
{
    // skipping all error checking
    string sWorkingString = sOriginalString;
    string sResultString = "";

    int iCurrentIndex = 0;
    foreach( char c in sWorkingString )
    {
        if( iCurrentIndex >= iStartPos && iCurrentIndex < iEndPos )
        {
      if( iCurrentIndex == iStartPos )
      {
            sResultString += "S";
      }
      else if( iCurrentIndex == iEndPos )
      {
           sResultString += "E";
      }
      else
      {
           sResultString += c.ToString();
      }
        }
        ++iCurrentIndex;
    }
    return sResultString;
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question