-6

I'm working hard on my new project, I'm totally new in C# so I don't really understand much, and I'm making a loading screen now, and do you know that "Loading message".

So I would like to make a few lines, and the code should be able to pick a random line and display when it is loading. Like this example:

var randomLoadingMessage = function() {
    var lines = new Array(
        "Locating the required gigapixels to render...",
        "Spinning up the hamster...",
        "Shovelling coal into the server...",
        "Programming the flux capacitor"
    );
    return lines[Math.round(Math.random()*(lines.length-1))];
}

I found it on the internet. How could I use it on c#?

I need help to do something similar in c#

  • Okay, what is your question? What problem are you having? –  Nov 13 '18 at 15:09
  • So what's the question? How to rewrite this in C#? – Martin Nov 13 '18 at 15:09
  • 2
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Arthur Attout Nov 13 '18 at 15:11
  • Yes, i need it in C# –  Nov 13 '18 at 15:12
  • Your question was already tagged with C#, so your comment and edit don't add anything to the question. Please show us your attempts to write this in C#, and we will be happy to help. –  Nov 13 '18 at 15:13
  • What kind of application? A Windows Forms app, a WPF app, a web app? Duplicating that code is easy. What you are going to do with it is more complicated – Flydog57 Nov 13 '18 at 15:14
  • I don't know how to rewrite it, that's the problem, i just know that i need to make something, and then input the string into a label.text –  Nov 13 '18 at 15:14
  • Windows Forms App –  Nov 13 '18 at 15:14
  • So you have no attempts at all how to create an array and initialize with values, and pick a random one, in C#? None at all? –  Nov 13 '18 at 15:16
  • 2
    Possible duplicate of [Pick Random String from Array](https://stackoverflow.com/questions/6695187/pick-random-string-from-array) –  Nov 13 '18 at 15:18
  • Guys, i just started to do something with c#, i don't really UNDERSTAND strings, other functions, btw I'm only 16y –  Nov 13 '18 at 15:22
  • 2
    I guess people expect you to learn the basics of c# before asking questions... Otherwise you'll have a hard time understanding the answers and do modifications to them to fit your needs. – Oram Nov 13 '18 at 15:27
  • 1
    Hi there, the thing you are trying to do can be achieved. quite easily. But, to use the C# language more efficiently, spend some time in MSDN for conceptualization. I am sure that will help you a lot :) – Md. Tazbir Ur Rahman Bhuiyan Nov 13 '18 at 15:27
  • 2
    If you want someone to write you some code there are paid platforms for that.. this is a Q&A site – Maor Refaeli Nov 13 '18 at 15:43
  • 1
    You really need to learn to break the problem down into smaller parts & solve each problem. You want to "make a few lines" - well your example suggests using an array - so find out how to create an array in C#. Then you want to pick a random value - so you need to know how to create a random number in C# - so search for that. Breaking the problem down into steps like that will also give you a good start in learning how to design your code properly. Do not try to do too much at once - you will only get discouraged. – PaulF Nov 13 '18 at 17:26

1 Answers1

1

Here are the pieces you need to solve this

  1. A Program. Below is the boilerplate code for a C# console application. Why's this needed? So your line of code has some context to run in. NB: You could run code via LinqPad without this, or you could use a different type of project to host your code; but for now I'm keeping things simple.
using System;
public class Program
{   
    public static void Main()
    {
        Console.WriteLine("Hello World");
    }   
}
  1. An array holding the strings you want to display. See Options for initializing a string array for additional info / options:
string[] lines = new [] {
    "Locating the required gigapixels to render...",
    "Spinning up the hamster...",
    "Shovelling coal into the server...",
    "Programming the flux capacitor"
);
  1. A way to pick one of these strings at random. You get a string from the array by putting a number (the index) in square brackets after the variable name, with each item in the array having consecutive numbers starting at 0. ; e.g. lines[1] would give you the string "Spinning up the hamster...". You can get a random number using an instance Random class's Next method. This method requires you to provide parameters to define the range in which the result should fall. The method returns a double but you need an int for your index, so you'll have to convert the result. See How do I generate a random int number in C#? for more.
int randomIndex = myRandom.Next(0, lines.Length);

For now I'll leave it there or I'd be giving you the full solution. If you have issues though, please post new questions here with what you've tried so far, and why it's not working (i.e. are you getting an error message, if so what, or is it doing something different to what you'd expect).

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
  • You used Console.WriteLine, how could i just write it to label? –  Nov 13 '18 at 17:27
  • string lines = Label.Text? –  Nov 13 '18 at 17:36
  • Close.... The left side of the equals is the assignee, the right side is the value to be assigned. Try searching this site or Google for clues initially, then post back if stuck. E.g. https://stackoverflow.com/questions/10704020/changing-a-labels-text-in-another-form-in-c – JohnLBevan Nov 13 '18 at 18:10
  • Ehh, i dont understand –  Nov 13 '18 at 18:22