VB.NET conversion to C #

2

I found this line of code:

Try
        Dim WebReq As HttpWebRequest = HttpWebRequest.Create(GET_Data)
        Using WebRes As HttpWebResponse = WebReq.GetResponse
            Using Reader As New StreamReader(WebRes.GetResponseStream)
                Dim Str As String = Reader.ReadLine
                Select Case True
                    Case Str.Contains(Answer1)
                        Return "True"
                    Case Str.Contains(Answer2)
                        Return "Banned"
                    Case Str.Contains(Answer3)
                        Return "Invalid"
                    Case Else
                        Return "Invalid"
                End Select
            End Using
        End Using

As a form of study I need this same code in C #

 try
        {
            WebRequest WebReq = WebRequest.Create(Get_Data);
            using (WebResponse WebRes = WebReq.GetResponse())
            {
                using (StreamReader Reader = new StreamReader(WebRes.GetResponseStream()))
                {
                    string Str = Reader.ReadLine();

                    while (true)
                    {
                        if (Str.Contains(answer1).ToString()!= null)
                        {
                            MessageBox.Show("sadsads");
                            return "True";
                        }
                    }
                }
            }
        }

I tried to use case, but it did not work, this was the last attempt with if .

    
asked by anonymous 16.05.2017 / 16:40

3 answers

4

That code is very bad, I think the best way to do this would be with if :

var WebReq = HttpWebRequest.Create(GET_Data);
using (var WebRes = WebReq.GetResponse)
using (var Reader = new StreamReader(WebRes.GetResponseStream)) {
    var Str = Reader.ReadLine;
    if (Str.Contains(Answer1)) return "True";
    if (Str.Contains(Answer2)) return "Banned";
    if (Str.Contains(Answer3)) return "Invalid";
    return "Invalid";
}

I placed it on GitHub for future reference.

Switch should only be used when you have a variable with multiple values that need to be tested. This is a case where you have multiple variables to test a single value, which is the case for if .

In your attempt while does not make sense, even less .ToString()!= null , this was not in the original. If you want to do something different you would have to say what. In fact if the variable Answer was an array another enumerable type, it would be possible to use while to make the 3 comparisons in a single if . Something tells me that this code should look very different from what was put.

I took advantage of and improved some more secondary things.

See this also: Naming Pattern in Code for C #

    
16.05.2017 / 17:13
2

This construction of VB will not really work with C# , you will even need to use if if you want to test using Contains in this way. You can adapt the code using an expression to evaluate Contains before switch , something like this:

// Precisa adicionar esses using no começo do método
using System.Linq;
using System.Collections.Generic;

// Aqui vai o código:
string Str = Reader.ReadLine();
// Adicionar as strings que quer comparar em subStrings
List<string> subStrings = new List<string> { Answer1, Answer2, Answer3 };
switch (subStrings.FirstOrDefault(Str.Contains))
{
    case Answer1:
        Console.WriteLine("True");
        break;
    case Answer2:
        Console.WriteLine("Banned");
        break;
    case Answer3:
        Console.WriteLine("Invalid");
        break;
    default:
        Console.WriteLine("Invalid");
        break;
} 
    
16.05.2017 / 17:00
2

You can use the switch() operator:

{
    HttpWebRequest WebReq = HttpWebRequest.Create(GET_Data);
    using (HttpWebResponse WebRes = WebReq.GetResponse)
    {
        using (StreamReader Reader = new StreamReader(WebRes.GetResponseStream))
        {
            string Str = Reader.ReadLine;
            switch (true)
            {
                case Str.Contains(Answer1):
                    return "True";
                case Str.Contains(Answer2):
                    return "Banned";
                case Str.Contains(Answer3):
                    return "Invalid";
                default:
                    return "Invalid";
            }
        }
    }
}
    
16.05.2017 / 17:00