Results 1 to 18 of 18

Thread: [3] again , VB.NET equivalent ?[Resolved]

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    [3] again , VB.NET equivalent ?[Resolved]

    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:
    1. Private Const MAX_PATH As Short = 260
    2.  
    3. Dim sBuffer As String
    4. sBuffer = [B]Space[/B](MAX_PATH * 2)
    Last edited by Pirate; Jul 28th, 2003 at 11:52 AM.

  2. #2
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Uhm... Space.

    VB Code:
    1. Dim str As String
    2. str = Space(500)
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    No , that's imported from VisualBasic namespace . Did you actually read my post ??? . 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 .

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    try this
    VB Code:
    1. Imports System.Text
    2. '/// as usual at the top.
    3. '///////////////////////////
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         Dim strString As New StringBuilder(256)
    7.         strString.Append("A", strString.Capacity)
    8.  
    9.         MessageBox.Show(strString.ToString)
    10.  
    11.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a better example , using an api call but , showing how the stringbuilder works inplace of the Space thing from vb6 :
    VB Code:
    1. Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer
    2. '/// i modified it to StringBuilder ^^^
    3. '///////////////////////////////////////////////
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim strString As New StringBuilder(256)
    6.         GetClassName(Me.Handle, strString, strString.Capacity)
    7.  
    8.         MessageBox.Show(strString.ToString)
    9.  
    10.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

    Thanks .

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you can make a string have a capacity by doing something like this .....
    VB Code:
    1. <VBFixedString(256)> Public strString As String
    but it'd have to be in a public area.
    however the correct .NET method is StringBuilder and it's far faster than String.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I converted stringbuilder to string but this won't be converted ! How the heck can I modify this to return string ??
    VB Code:
    1. sBuffer = CType(sBuffer.ToString.Substring(0, lNullPos - 1), String)

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    ToString , CType , DirectCast won't work either . . Seems I'm stuck there no other options left ?

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Pirate it seems to me that you are using a conversion program. Anyway try this.
    Code:
    sBuffer = (string)sBuffer.ToString.Substring(0, lNullPos - 1);

  11. #11

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ?

  12. #12

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    We're here talking about VB.NET code .

  13. #13
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Nevermind what I said. I thought you wanted it converted to C#.

  14. #14
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Sorry 'bout my first response, Pirate. Try this:

    VB Code:
    1. Dim str As New String(" ", 25)

    That will initialize a string object with 25 spaces.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  15. #15

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    It doesn't work .

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This works

    VB Code:
    1. Dim str As New String(" ", 260)


    Thanks Sheppe

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can also use padleft and padright if you need to add spaces or any character to an existing string.
    VB Code:
    1. Const MAX_PATH As Short = 260
    2.         Dim str As String = "Something" 'or String.empty
    3.         str = str.PadLeft(MAX_PATH * 2, " ")
    4.  
    5.         MsgBox("'" & str & "'")

  18. #18

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I don't want to add spaces , I wanted to get a string consisting of the specified number of spaces (260 chars) .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width