8 Replies - 237 Views - Last Post: 09 June 2019 - 10:50 PM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

string array random output

Posted 09 June 2019 - 08:07 AM

Hi,

I want to make an random output of my arrays, so far I can only get arrays written but no sign of any random arrays

Here is the 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;

namespace WindowsFormsApp2
{
    
    public partial class Form1 : Form
    {
        //List<string> Spara = new List<string>();
        List<string[,]> Spara = new List<string[,]>();
        string[,] animals2 = new string[ 3 , 3] { { "one", "two" ,"three" }, { "three", "four","Five" },
                                        { "five", "six","Seven" } };

        public static Random rng;
        public static int siffra;

        public Form1()
        {
          
            InitializeComponent();
            siffra = 1;


            Spara.Add(animals2);
            textBox1.Text = Convert.ToString(siffra);
            rng = new Random();

            
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            int index = rng.Next(Spara.Count);
            textBox1.Text = Convert.ToString(siffra);
            foreach (string[,] item in Spara)
            {
                textBox1.Text = Convert.ToString(item[0, 1] + index);
                
            }
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}



What am I doing wrong here?

Is This A Good Question/Topic? 0
  • +

Replies To: string array random output

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: string array random output

Posted 09 June 2019 - 08:46 AM

Quote

so far I can only get arrays written but no sign of any random arrays

This makes no sense. Explain this more.
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: string array random output

Posted 09 June 2019 - 08:52 AM

Okay, So what I mean is that I need my list to be written when I run the program BUT I want it to write random days every time I press the button, Right now, it only shows the same day "sun" and does not refresh to new day when I press the button. Here is the 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;

namespace WindowsFormsApp2
{
    
    public partial class Form1 : Form
    {
        //List<string> Spara = new List<string>();
        List<string[]> Spara = new List<string[]>();
        string[] animals2 = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        public static Random rng;
        public static int siffra;

        public Form1()
        {
          
            InitializeComponent();
            siffra = 1;


            Spara.Add(animals2);
           // textBox1.Text = Convert.ToString(siffra);
            rng = new Random();

            
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            
            int index = rng.Next(Spara.Count);
            textBox1.Text = Convert.ToString(siffra);
            foreach (string[] item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item[1] + item[index] );
                
            }
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: string array random output

Posted 09 June 2019 - 09:23 AM

17	        List<string[]> Spara = new List<string[]>();
18	        string[] animals2 = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
30	            Spara.Add(animals2);


Why is the list of an array of strings? Do you expect more array of strings to be added to it?

Have you put your break point on top to see what the contents are and how that array, as an item inside the list, works?

I think you mean for each element, of the string array, to be added to the list. That is most certainly _NOT_ happening.
Was This Post Helpful? 0
  • +
  • -

#5 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: string array random output

Posted 09 June 2019 - 09:28 AM

Okay the problem is solved for one dimensional array to be added to the list. Bellow is answer to your question, here is the following:

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;

namespace WindowsFormsApp2
{
    
    public partial class Form1 : Form
    {
        //List<string> Spara = new List<string>();
        List<string[]> Spara = new List<string[]>();
        string[] animals2 = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

        public static Random rng;
        

        public Form1()
        {
          
            InitializeComponent();

           

            Spara.Add(animals2);
           // textBox1.Text = Convert.ToString(siffra);
            rng = new Random();

            
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            int index = rng.Next(1,7);

            foreach (string[] item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item[index] );
                
            }
           
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}



regarding why there is array in list is because I will add books to the list with (Author, category, type)
This will be added with streamreader, streamwriter, that way I can have its own category to get it written out as a whole. This streamreader will rea from a text file.

You see my point?
I need to make this 2 dimensional so that I can have its own category for each book

This post has been edited by Realme: 09 June 2019 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#6 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: string array random output

Posted 09 June 2019 - 09:44 AM

This is the new problem I am stumbling upon with 2 dimensional array. "Random Next" I cant give correct value from lowest to high. Here is code example:

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;

namespace WindowsFormsApp2
{
    
    public partial class Form1 : Form
    {
        
        List<string[,]> Spara = new List<string[,]>();
        string[,] animals2 = new string[3, 2] { { "one", "two" }, { "three", "four" },
                                        { "five", "six" } };

        public static Random rng;
        

        public Form1()
        {
          
            InitializeComponent();

           

            Spara.Add(animals2);
            rng = new Random();

            
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            int index = rng.Next(3,2);

            foreach (string[,] item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item[index,index] );
                
            }
           
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}



How can I make so that also Next becomes 2 dimensional? This one here:
 int index = rng.Next(3,2);

Something like this? int index = rng.NextBytes(0,2 , 0,2);

This post has been edited by Realme: 09 June 2019 - 09:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: string array random output

Posted 09 June 2019 - 10:08 AM

Here is the problem I am facing. How do I get the random to reach this?

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;

namespace WindowsFormsApp2
{
    
    public partial class Form1 : Form
    {
        
        List<string[][]> Spara = new List<string[][]>();
        string[][] animals2 = new string[][] {
    new string[] { "a1", "b1", "c1" },
    new string[] { "a2", "b2", "c2" },
    new string[] { "a3", "b3", "c3" },
    new string[] { "a4", "b4", "c4" } };

        public static Random rng;
        

        public Form1()
        {
          
            InitializeComponent();

           

            Spara.Add(animals2);
            rng = new Random();

            
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            int index = rng.Next(0,0);

            foreach (string[][] item in Spara)
                
            {
                
                textBox1.Text = Convert.ToString(item[index][index] );
                
            }
           
            
        }

        private void SökRuta(object sender, EventArgs e)
        {
            
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: string array random output

Posted 09 June 2019 - 10:36 AM

Okat problem solved... I now realize that I could just add extra text in single array.... Ehm
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: string array random output

Posted 09 June 2019 - 10:50 PM

OMG... Although not the root cause of your random number generation problem, but that's a terrible design. Presumably, the intent of the static random number generator was so that you would only have one instance of a generator to be used through out the lifetime of your program. Unfortunately, you are shooting yourself in the foot because you instantiate a new random number generator in your class instance constructor. It's not a likely an issue right now because you are only likely using one instance of your form, but it will be down the road if you persist with this pattern for other variables. Either declare the generator as static and initialize it in a class type constructor, or declare it as an instance variable and initialize it in a class instance constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1