KnowDotNet

Replacing VB.NET Functionality in C#

Space() Function

by Les Smith

How can I replace the Space() function, found in VB and VB.NET in C#?  The answer is hardly worth writing an article for, but here is the answer.

Let's see you have the following VB.NET code:

      Dim s2 As String
      
s2 = Space(5) & "A"

To create the same code in C#, use the following code:
  
  
string s2;
   s2 =
string.Empty.PadLeft(5) & "A";

Both lines of code produce the same value in s2.  It would be the same as writing:

   s2 = "     A"

So, if you are an old VB'r, write a C# function like this

   public string Space(int n)
   {
      
return String.Empty.PadLeft(n);
   }  

You could create a class of static utility functions and place your new Space() function in it.  It might look something like this.

  public class VB
  {
     public static string Space(int n)
     {
        return String.Empty.PadLeft(n);
     }  
   }

Now, you can reference it, without instantiating an instance of the VB class, as shown below.

   string s2 = VB.Space(5) + "A";


Another article that is not rocket science, but if you are moving from VB or VB.NET and can't figure out a simple way to replace the Space() function, maybe it's a help.

Have you tried our newest product, Visual Class Organizer?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.