简体   繁体   中英

Working with strings in visual basic 2010

I have to create a class in Visual Basic called StringWork.

Public Class StringWork

Now i wrote a shared function in the class called Working that can take a string or a string and Boolean.

Public Shared Function Working(ByVal SingleString as string, optional BValu  as Boolean = true)as string
    if(working(SingleString))then
    'The handling of the string
    else if (working(SingleValue, BValue) then
    'do something else with string 
    end if
end function

The function I wrote is returning a string. Can I access the string passed and edit characters in the string or change the position of characters?

You use that optional parameter to determine how you handle that string:

Public Shared Function Working(ByVal singleString as string, _
                               Optional bValue as Boolean = True) As String
  If bValue Then
    'Handle the true part manipulating the result string
  Else
    'Handle the false part manipulating the result string
  End If

End Function

If you call this function like this:

Dim test As String = StringWork.Working("I am Spartacus")

it will call that Working function with bValue = true.

What bValue is suppose to represent isn't very clear from the code nor the post.

In VB.NET, and other .NET languages, strings are immutable. Typically, if you need to modify a string that is passed to your method, you would return the modified string. If however, you need it to modify the parameter, you can specify that it is a "ByRef" argument, in which case you will be able to set it to point to a new string object which will affect the variable that was passed into the method as a parameter. If you need a truly mutable string, you will need a character array or a StringBuilder object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM