2

I wanted to get familiar with the built-in functions of VB, specifically, the len() function. However, I think this may not be the right way to concatenate a string with a char.

Also, it may interest you that the error list says,

"Warning 1 Variable 'reverse' is used before it has been assigned a value. A null reference exception could result at runtime."

I executed the program but it ran fine. Here's the code:

Sub Main()
    Dim a As String
    Console.WriteLine("Enter the value of the string you want to reverse: ")
    a = Console.ReadLine()
    Dim reverse As String
    Dim temp As Char
    Dim str As Integer
    str = Len(a)
    For x = str To 1 Step -1
        temp = Mid(a, x)
        reverse = reverse + temp
    Next x
    Console.WriteLine(reverse)
    Console.ReadKey()
End Sub

I'm still learning this language and so far it's been really fun to make small programs and stuff.

Nick
  • 39
  • 1
  • 8
  • Possible duplicate of [Best way to reverse a string](http://stackoverflow.com/questions/228038/best-way-to-reverse-a-string) – Morcilla de Arroz Mar 28 '16 at 14:30
  • 1
    1, vba<>vb.net. 2. to get rid of the error simply make `reverse = ""` after you declare it. – Scott Craner Mar 28 '16 at 14:31
  • I think that's a different language? I'm specifically talking about Visual Basic 2010. Also, I don't understand the usage of ToCharArray function. – Nick Mar 28 '16 at 14:32
  • @Scott That does the trick, thank you. As an aside, why is it necessary to initialise it with nothing? And do you think the overall code is clean (if you will). Can I make it better, if yes, how? – Nick Mar 28 '16 at 14:34
  • 3
    `foo = New String(foo.Reverse().ToArray())` – Ňɏssa Pøngjǣrdenlarp Mar 28 '16 at 14:37
  • Look at the link provided by @Caveman, there are many many sites that will automatically translate the c# to vb.net. – Scott Craner Mar 28 '16 at 14:37
  • 1
    `Mid()`, `Len()` and more are outdated and are only there for backwards compability. Use `a.Length` and `a.Chars(x)` instead. Also, Visual Basic's native concatenation operator is `&` and not `+`. – Visual Vincent Mar 28 '16 at 14:58
  • 2
    It's definitely not the *clean/right way to do it*. You're mixing old VB and VB.Net, and you're failing to take advantage of enhancements in the language and framework. See the one liner posted by @Plutonix. – Ken White Mar 28 '16 at 15:03
  • Also, writing to a string multiple times is inefficient. For learning purposes, you should look into a `StringBuilder` – Steve Mar 28 '16 at 15:55
  • @Steve the efficiency of StringBuilder have been pulled into question many times... Notably here: http://stackoverflow.com/questions/34344148/stringbuilder-vs-ampersand-equals-concatentation – Jeroen Mar 28 '16 at 23:24
  • @everyone suggesting Plutonix' answer: I really don't understand what's happening here. I'm not that great with functions and the main intention was never to use any string reversing functions. – Nick Mar 29 '16 at 09:47
  • @Visual Vincent Why does VB allow the usage of + if it's not the native operator for concatenating? Edit: I found an MSDN article on it. Hopefully it answers the question. – Nick Mar 29 '16 at 09:56
  • 1
    Because VB has two concatenation operators. But `&` is the native one as it's specifically designed to concatenation only. My guess is that the `+` works in order to have backwards compability and simpler understanding between different languages. See this Stack Overflow answer to why you shouldn't use the `+`: http://stackoverflow.com/a/734631/3740093 – Visual Vincent Mar 29 '16 at 10:20
  • The `&` operator will try to convert every object that is not a string into a string. There are **many** questions on this forum where users have tried to concatenate a string with a number using the `+` operator, which causes the error `Conversion from string to type 'Double' is not valid.` --- For example when you do this: `"Your ID is: " + 5`. – Visual Vincent Mar 29 '16 at 10:26

2 Answers2

1
Dim TestString As String = "ABCDEFG" 
Dim revString As String = StrReverse(TestString)

ref: https://msdn.microsoft.com/en-us/library/e462ax87(v=vs.90).aspx

Rob
  • 3,488
  • 3
  • 32
  • 27
  • This requires adding a reference to Visual Basic, and is totally unnecessary. There is native String support for the same functionality, and it teaches something that is transferable to other .Net languages. – Ken White Mar 29 '16 at 01:38
1

You are getting the warning

"Warning 1 Variable 'reverse' is used before it has been assigned a value. A null reference exception could result at runtime."

Because String variable reverse is not yet initialized. Compiler will consider the scenario that For is not executing in such situation the reverse can be null. you can get out of the warning by assigning an empty value to the string: ie.,

 Dim reverse As String=String.Empty

You can do the functionality in the following ways too:

 Dim inputStr As String = String.Empty
 Console.WriteLine("Enter the value of the string you want to reverse: ")
 inputStr = Console.ReadLine()
 Dim reverse As String = String.Join("", inputStr.AsEnumerable().Reverse)
 Console.WriteLine(reverse)
 Console.ReadKey()

OR

  Dim reverse As String = String.Join("", inputStr.ToCharArray().Reverse)
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88