none
How do I create and load a matrix with strings from a class? RRS feed

  • Question

  • Hello! I am trying to create a string matrix in the Form1 app that loads a series of strings from a class. Here is the class. Thank you for any help!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Pome_Generator_App
    {
        class Verbs
        {
            public string words = "first||second";
        }
         
    }

    Wednesday, May 29, 2019 10:28 PM

Answers

  • I wrote my last post before I saw your post with the sample code including the text file. It's still not completely clear where you're heading with this.

    If you don't want a text file, there should be no need to read any input from comma separated strings (or piping separated). You could just code them into the list of verbs to start with.

                    // Instead of this...
                    //Verbs newVerbs = new Verbs();
                    //newVerbs.Normal = entries[0];
                    //newVerbs.Irregular = entries[1];
                   // readverbs.Add(newVerbs);
    
    // Do this.
    readVerbs.Add(new Verbs("Normal1", "Irregular1"));
    readVerbs.Add(new Verbs("Normal2", "Irregular2"));
    
    // etc.

    Thursday, May 30, 2019 12:05 AM

All replies

  • I have no idea what you mean by "create a string matrix".

    This isn't a homework assignment, by any chance? We're not supposed to do those here

    Wednesday, May 29, 2019 11:25 PM
  • No, it's just my own project. I am trying create a list or array of strings that can be accessed by index number.

    Wednesday, May 29, 2019 11:29 PM
  • I'm trying to do this, but with the data in a class instead of a text file.

                string filePathv = @"C:\RBPG\Verbs.txt";
                List<Verbs> readverbs = new List<Verbs>();
                List<string> linesv = File.ReadAllLines(filePathv).ToList();
                foreach (string linev in linesv)
                {
                    string[] entries = linev.Split(',');
                    Verbs newVerbs = new Verbs();
                    newVerbs.Normal = entries[0];
                    newVerbs.Irregular = entries[1];
                    readverbs.Add(newVerbs);
                }

    Wednesday, May 29, 2019 11:39 PM
  • If I understand you, there's probably no need for a new class. You can use a List.

    List<string> verbs = new List<string>();
    
    verbs.Add("run");
    verbs.Add("hop");
    verbs.Add("skip");
    verbs.Ad("jump");
    
    
    // Use like so.
    string sentence = "The boy likes to " + verbs[1]; // "hop"

    Wednesday, May 29, 2019 11:41 PM
  • That seems like a lot of extra coding. I am a new programmer, so I'm still learning this. I've got hundreds of words, so I thought it seemed tidy to have them separated in classes with only the ||.

    Wednesday, May 29, 2019 11:51 PM
  • I wrote my last post before I saw your post with the sample code including the text file. It's still not completely clear where you're heading with this.

    If you don't want a text file, there should be no need to read any input from comma separated strings (or piping separated). You could just code them into the list of verbs to start with.

                    // Instead of this...
                    //Verbs newVerbs = new Verbs();
                    //newVerbs.Normal = entries[0];
                    //newVerbs.Irregular = entries[1];
                   // readverbs.Add(newVerbs);
    
    // Do this.
    readVerbs.Add(new Verbs("Normal1", "Irregular1"));
    readVerbs.Add(new Verbs("Normal2", "Irregular2"));
    
    // etc.

    Thursday, May 30, 2019 12:05 AM
  • Okay, thanks.
    Thursday, May 30, 2019 12:17 AM
  • No worries.

    I get a bit of a feeling that I might not have really answered your question, and you just marked my post as the answer to be polite. If that's the case, please don't hesitate to ask for more clarification. Even if I can't help, someone else surely will.

    Thursday, May 30, 2019 12:29 AM