We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

VB string function equivalent in c#...

ube100
ube100 asked
on
Medium Priority
1,111 Views
Last Modified: 2012-06-27
How do i convert this vb code into c#

strTemp = String(255, Chr$(0))
Comment
Watch Question

Top Expert 2009

Commented:
Like this:

string strTemp = new string('\0', 255);
Top Expert 2009

Commented:
The '\0' part is the equivalent of the the Chr(0) function (ascii character NUL).

Btw, I assume you meant this as VB code, because the mentioned VB code will not compile (first parameter of constructor cannot be an Integer):

Dim strTemp As New String(Chr(0), 255)

Author

Commented:
next code is:

lRet = GetTempPath(255, strTemp)

API function for the GetTempPath is declared like this:

Private Declare Function GetTempPath Lib "Kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

How will I convert this into C#.
Top Expert 2009
Commented:
You don't need API functions old style anymore in .NET. You can use the .NET library functions, which are type safe. In this case:

System.IO.Path.GetTempPath()

If you do not want the "System.IO" in front of it, you can put that in the using headers:

using System.IO;

...
string strTemp = Path.GetTempPath();

As you can see, you do not need to initialize your variables with nullpadded strings anymore. Quite handy this way, isn't it? And whether you use VB.NET or C#.NET or any other .NET language, you can always use these base libraries.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thank very much sir for help!!!
Top Expert 2009

Commented:
You're welcome, glad it helped :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.