Home > front end >  c#: Random Choose from web and ignore blank lines
c#: Random Choose from web and ignore blank lines

Time:04-24

I'm fresh in c# forms i have an important question.

I want to make randomly text writer one line from url "list"

Let Me explain, i wanna to get a random line from pastebin, and rename a label with the random line chosed, and ignore blank lines.

This is a error:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'randomline' does not exist in the current context SNPCorp C:\Users\GhostStru\Desktop\SNP\Tester\Generator.cs  45  Active

i have that code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net.Security;
using System.Threading;
using System.Net.Http;
using System.Web;
using System.Net.WebSockets;
using System.Net;
using System.IO;

namespace Tester
{
    public partial class Generator : Form
    {
        public Generator()
        {
            InitializeComponent();
        }

        private void Generator_Load(object sender, EventArgs e)
        {
            WebClient web = new WebClient();
            string text = web.DownloadString("https://pastebin.com/raw/test");
            var lines = text.Split(new string[] { "." }, StringSplitOptions.None);
            Random rnd = new Random();
            int randomLineIndex = rnd.Next(0, lines.Count());
            var randomline = lines[randomLineIndex];
        }


        private void button3_Click(object sender, EventArgs e)
        {
            label11 = (randomline);
        }
    }
}

CodePudding user response:

Your question has some things that aren't clear but I'll try with it. First, I define a method to get a random line:

private static string GetRandomLine()
{
    using (var web = new WebClient())
    {
        var html = web.DownloadString("https://pastebin.com/raw/test");
        var lines = html.Split(new string[] { "." }, StringSplitOptions.None);
            
        var rnd = new Random();
        int index = rnd.Next(0, lines.Length);
            
        return lines[index];
    }
}

Then, you can use that method to set Text property of your label. You must set the Text property, not the label variable

private void button3_Click(object sender, EventArgs e)
{
    label11.Text = GetRandomLine();
}

The DownloadString is not working for me. Is that the Url? In any case, if you use a lot this method, is interesting save into a variable the HTML and use it some time (one hour... depending of your needs) as a cache instead of make a request each time. WebClient implements IDisposable so you must invoke Dispose after use it or allow to "using" do it automatically.

CodePudding user response:

I'll try to answer your question even though you haven't explained some points clearly.

  1. Create a function to return the random line

  2. In button3_Click event, call a function that will return a random line, then set the text value of button3 to that returned value.

     public static string GetRanLine()
     {
           using (var WC = new WebClient())
           {
                        var Response = WC.DownloadString("https://pastebin.com/raw/test");
                        var AllLines = Response.Split(new string[] { "."}, StringSplitOptions.None);
                        var RanLine = AllLines[new Random().Next(0, AllLines.Length - 1)];
                        return RanLine;
           }
      }     
    
    private void button3_Click(object sender, EventArgs e)
    {
           label11 = GetRanLine();
    }
    
  •  Tags:  
  • c#
  • Related