vendredi 3 juin 2022

How can I work with Char Array and StringBuilder?

I'm trying to create random character combination. Then I'd like to output it on the console. The following code does work as long as I'm using string instead of char datatypes.

The problem with using just sting datatype is, that the output will only be in upper case and I can't simply lowercase everything and only uppercase the first string.

So I tried to do it with a char array appending every character to a stringBuilder. But somehow I can't Console.WriteLine what I coded. and I don't know why... Do you guys maybe have a hint for solving the issue?

cheers

    using System;

namespace WinGen
{
    public class NamenGenerator
    {
        private int NamenLaenge { get; set; }
        private char[] BuchstabenKombination { get; set; }
        private int CharShifter { get; set; }
        private int CharPicker { get; set; }
        private int RangeMin { get; set; }
        private int RangeMax { get; set; }

        public NamenGenerator()
        {
            Random _namenLaenge = new Random();
            this.NamenLaenge = _namenLaenge.Next(4, 10);
            CharShifter = 0;
            RangeMin = 0;
            RangeMax = 25;
            BuchstabenKombination = new char[this.NamenLaenge];
        }

        public void StringGenerator()
        {
            try
            {
                while (this.NamenLaenge != 0)
                {
                    Random charakter = new Random();
                    CharPicker = charakter.Next(RangeMin, RangeMax);
                    BuchstabenKombination[CharShifter] = Convert.ToChar((Buchstaben)CharPicker);
                    this.NamenLaenge--;
                    this.CharShifter++;
                }

                StringBuilder sb = new StringBuilder();
                string t = string.Empty;

                foreach (char c in BuchstabenKombination)
                {
                    t = Convert.ToString(c);
                    sb.Append(t);
                    Console.WriteLine(t);
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
            
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire