0

Why does this code not give me the right output? I am expecting the output to be decomptxt = aa".

I typed in 02a (RLEText)" "Function 'Compressdata' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used."

Expected result: decomptxt = aa

This is my code:

x = 0
    For i As Integer = 1 To Len(RLEText)
        Dim st As String = RLEText
        NoChars = st.Substring(x, 2)
        Chars = st.Substring(x + 2, 1)
        decomptxt = String.Join("", Enumerable.Repeat(Chars, NoChars))


        Return decomptxt
        x = x + 3
Om Sao
  • 7,064
  • 2
  • 47
  • 61
  • 1
    Set Option Strict On and Option Explicit On, correct what's wrong with the code (e.g., `NoChars = Convert.ToInt32(st.Substring(x, 2))`) then post the whole procedure (a `Return` statement implies a Function, so what is `x = x + 3` doing there? It's unreachable. etc.) – Jimi Oct 06 '19 at 16:25
  • For i As Integer = 1 To Len(RLEText) Dim st As String = RLEText NoChars = Convert.ToInt32(st.Substring(x, 2)) Chars = st.Substring(x + 2, 1) decomptxt = String.Join("", Enumerable.Repeat(Chars, NoChars)) Return decomptxt I am new to programming and this is my first project. So I don't fully understand how to fix this problem. For example, if i input RLEText = 02a, then decomptxt should be aa, then if i input RLEText = 04w, then decomptxt should be wwww., Thanks for your help! – HTironman Oct 06 '19 at 17:57
  • 3
    You should better edit your post rather than adding things like code in comments... – B. Go Oct 06 '19 at 18:00
  • 1
    It's clear that you're new to this, that's why I suggested to set `Option Strict` and `Option Explicit On` (you'll find these settings in your Project's Properties, in the `Compile` panel). It's really important that you set these option. You can do the same using Visual Studio's general settings (`Tools -> Option -> Project and Solutions -> VB defaults`). Once you do, it will be also clear what you need to correct and your method will return what you expect it to. Post the whole code editing (see the [edit](https://stackoverflow.com/posts/58258977/edit) link) your question, though. – Jimi Oct 06 '19 at 18:23
  • x = 0 For i As Integer = 1 To Len(RLEText) Dim st As String = RLEText NoChars = st.Substring(x, 2) Chars = st.Substring(x + 2, 1) decomptxt = String.Join("", Enumerable.Repeat(Chars, NoChars)) Return decomptxt x = x + 3 It won't let me add it elsewhere so I'll post it here. I've switched on the strict and explicit options- what should happen now? – HTironman Oct 06 '19 at 19:54
  • 1
    Are there any red squiggles in your code? Hold the cursor over the squiggle to find out what is wrong and fix it. All `For` blocks require a `Next` . Where is the `Next` in your code? Have you tried clicking edit on the SO page? There is a line below the tags "share edit close flag" You will be able to add to or change your question. – Mary Oct 06 '19 at 20:15
  • What is `RLEText` ? Is it the name of a text box? – Mary Oct 06 '19 at 20:22
  • Please do not put code in comments. Click the [edit](https://stackoverflow.com/posts/58258977/edit) link beneath your question and add the code there. – Chris Dunaway Oct 07 '19 at 13:42

0 Answers0