none
How to acces this class? RRS feed

  • Question

  • So I have a winform with a listbox. I want to read a text file and display the text in a listbox.

    I wrote the code for readTextFile in a different class to get familiarized with SoC. Here is the code:

    namespace TextFiles_2
    {
        public class Class1
        {
            public static void ReadText()
            {
                string[] lines = File.ReadAllLines(@"C:\Users\user\Desktop\Document text nou.TXT");
    
            }
    
        }
    }
    

    And now i want to acces it here(I guess i need to acces it here. If not correct me)

            public void userListBox_SelectedIndexChanged(object sender, EventArgs e)
            {
                ReadText readText = new ReadText();
                userListBox.Items.Add()
            }
    
        

    But since I am a begginer nothing works for me(I've worked in the console for last 2 months). Also if I want to read an array of words(John,Cena,26,is happy) and then I want to appear in list box likee: John Cena is 26 and is happy, howww should i do that?

    Thanks in advance programmers!

    Saturday, October 12, 2019 8:12 PM

Answers

  • If the form and class are in the same namespace then simply create a instance of Class1 e.g.

    var class1 = new Class1();

    Change the method from static void to public string[]

    var lines = class1.ReadText();

    public class Class1
    {
        public string[] ReadText()
        {
            var fileName = @"C:\Users\user\Desktop\Document text nou.TXT";
            if (File.Exists(fileName))
            {
                return File.ReadAllLines(@"C:\Users\user\Desktop\Document text nou.TXT");
            }
            else
            {
                return new string[] {};
            }
        }
    }
    Note also I did an assertion to see if the file exists, if so read it, if not return an empty string array. For a real application you need to consider what to do if the file did not exists, could it be read and were there lines.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by horatiu_alex Sunday, October 13, 2019 7:16 AM
    Saturday, October 12, 2019 11:57 PM
    Moderator

All replies

  • If the form and class are in the same namespace then simply create a instance of Class1 e.g.

    var class1 = new Class1();

    Change the method from static void to public string[]

    var lines = class1.ReadText();

    public class Class1
    {
        public string[] ReadText()
        {
            var fileName = @"C:\Users\user\Desktop\Document text nou.TXT";
            if (File.Exists(fileName))
            {
                return File.ReadAllLines(@"C:\Users\user\Desktop\Document text nou.TXT");
            }
            else
            {
                return new string[] {};
            }
        }
    }
    Note also I did an assertion to see if the file exists, if so read it, if not return an empty string array. For a real application you need to consider what to do if the file did not exists, could it be read and were there lines.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by horatiu_alex Sunday, October 13, 2019 7:16 AM
    Saturday, October 12, 2019 11:57 PM
    Moderator
  • I wonder if this VB can help...

    Dim lines() As String = IO.File.ReadAllLines(fn)
    ListBox1.DataSource = lines

    Please refrain from posting VB.NET code in this C# forum. It's easy enough to show a concept with mocked data.

    string[] mimicDataFromFile = {"1", "2"};
    listBox1.DataSource = mimicDataFromFile;

    Which is basically the same as my first reply minus assigning items to the listBox.

    var class1 = new Class1();
    listBox1.DataSource = class1.ReadText();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, October 13, 2019 2:12 AM
    Moderator