What's the replacement of Space function (VB6 Function) in .NET . It's used to return a string consisting of the specified number of spaces.
VB Code:
Private Const MAX_PATH As Short = 260 Dim sBuffer As String sBuffer = [B]Space[/B](MAX_PATH * 2)
Printable View
What's the replacement of Space function (VB6 Function) in .NET . It's used to return a string consisting of the specified number of spaces.
VB Code:
Private Const MAX_PATH As Short = 260 Dim sBuffer As String sBuffer = [B]Space[/B](MAX_PATH * 2)
Uhm... Space. ;)
VB Code:
Dim str As String str = Space(500)
No , that's imported from VisualBasic namespace . Did you actually read my post ???:confused: . I said I don't want to continue using Space function , otherwise I want something similar to it by string or whatever obj . Pure .NET way . :confused:
try this ;)
:)VB Code:
Imports System.Text '/// as usual at the top. '/////////////////////////// Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strString As New StringBuilder(256) strString.Append("A", strString.Capacity) MessageBox.Show(strString.ToString) End Sub
here's a better example , using an api call but , showing how the stringbuilder works inplace of the Space thing from vb6 :
VB Code:
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer '/// i modified it to StringBuilder ^^^ '/////////////////////////////////////////////// Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strString As New StringBuilder(256) GetClassName(Me.Handle, strString, strString.Capacity) MessageBox.Show(strString.ToString) End Sub
Can't this be done with string class , because I'm getting a lot of errors in the subsequent code . They're all based on string objs . :rolleyes:
Thanks .
you can make a string have a capacity by doing something like this .....
but it'd have to be in a public area.VB Code:
<VBFixedString(256)> Public strString As String
however the correct .NET method is StringBuilder and it's far faster than String.
I converted stringbuilder to string but this won't be converted ! How the heck can I modify this to return string ??
VB Code:
sBuffer = CType(sBuffer.ToString.Substring(0, lNullPos - 1), String)
ToString , CType , DirectCast won't work either . :confused: . Seems I'm stuck there no other options left ?
Pirate it seems to me that you are using a conversion program. Anyway try this.
Code:sBuffer = (string)sBuffer.ToString.Substring(0, lNullPos - 1);
error :
'String' is a class type, and so is not a valid expression.
No I'm not using any program . But why you said that ?
We're here talking about VB.NET code . :D
Nevermind what I said. I thought you wanted it converted to C#.
Sorry 'bout my first response, Pirate. Try this:
VB Code:
Dim str As New String(" ", 25)
That will initialize a string object with 25 spaces.
It doesn't work .:confused:
This works :D
VB Code:
Dim str As New String(" ", 260)
Thanks Sheppe :)
You can also use padleft and padright if you need to add spaces or any character to an existing string.
VB Code:
Const MAX_PATH As Short = 260 Dim str As String = "Something" 'or String.empty str = str.PadLeft(MAX_PATH * 2, " ") MsgBox("'" & str & "'")
I don't want to add spaces , I wanted to get a string consisting of the specified number of spaces (260 chars) .