VB.NET to C#

Asked

Viewed 92 times

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 way of study I need the 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.

  • 1

    Give a check if the Telerik help you.

  • Even if you are trying to convert from this site, the code shows an error. http://prntscr.com/f8i7aa

  • This is not VB6.

  • Use if in everything that will work. And don’t put things that the original code doesn’t have. @Mathias?

3 answers

4


That code is really 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 put in the Github for future reference.

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

In his attempt the while makes no sense, least of all .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 were a array another enumerable type, could use the while to make the 3 comparisons in a single if. Something tells me that code should be quite different from what was put.

I took advantage and improved some more secondary things.

See this too: Nomenclature standard in code for C#

2

This construction of VB really won’t work with C#, you’ll really need to use if if you want to test using Contains this way. You can adapt the code using an expression to evaluate the Contains before the switch, something like that:

// 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;
} 

2

You can use the operator switch():

{
    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";
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.