-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#

15
  • 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 to learn how to write effective questions. Commented Nov 13, 2018 at 15:11
  • 2
    Possible duplicate of Pick Random String from Array Commented Nov 13, 2018 at 15:18
  • 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. Commented Nov 13, 2018 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 :) Commented Nov 13, 2018 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 Commented Nov 13, 2018 at 15:43

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

You used Console.WriteLine, how could i just write it to label?
string lines = Label.Text?
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. stackoverflow.com/questions/10704020/…
Ehh, i dont understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.