Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
So I am trying to improve my knowledge about C# and I have found a website called https://www.dotnetperls.com/ which has many examples on data structues and algorithms. Also about datatables: https://www.dotnetperls.com/datatable. The secound example uses the same DataTable from the first example and the comment in the code says to just //(please paste it in)
Now I tried to avoid that by importing the whole file with the DataTable. But no matter where I look, I don't find an explanation. These are the 2 examples.
C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable table = GetTable();
        
        // Step 4: print the first cell.
        Console.WriteLine("FIRST ROW, DOSAGE: {0}", table.Rows[0]["Dosage"]);
    }
    
    static DataTable GetTable()
    {
        // Step 2: here we create a DataTable.
        // ... We add 4 columns, each with a Type.
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Diagnosis", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        
        // Step 3: here we add rows.
        table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
        table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
        table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
        table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
        return table;
    }
}


C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // This uses the GetTable method (please paste it in).
        DataTable data = GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }
}


What I have tried:

I tried looking up on dotnet.microsoft or stackoverflow, but no answer gives me an error free solution. I have put them into the same namespace in the same folder and renamed the Main functions, I have imported via first_example.Program.Main(args);and
first_example.Program.GetTable();
I tried aproaches from other languages:
using first_example.Program;
But I just can't find a good explanation how to import in C# a program with its DataTable into another program. Could anyone with better knowledge point me into the right direction please?
Posted
Updated 6-Dec-22 6:02am
v2

If you want to share code between applications, then you'll need to create a "class library" project, and reference it from both applications.

Create a .NET class library using Visual Studio - .NET | Microsoft Learn[^]

In the class library:
C#
using System;
using System.Data;

namespace MyClassLibrary
{
    public static class MySampleData
    {
        public static DataTable GetTable()
        {
            // Step 2: here we create a DataTable.
            // ... We add 4 columns, each with a Type.
            DataTable table = new DataTable();
            table.Columns.Add("Dosage", typeof(int));
            table.Columns.Add("Drug", typeof(string));
            table.Columns.Add("Diagnosis", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));
            
            // Step 3: here we add rows.
            table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
            table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
            table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
            table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
            return table;
        }
    }
}

In application 1:
C#
using System;
using System.Data;
using MyClassLibrary;

static class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable table = MySampleData.GetTable();
        
        // Step 4: print the first cell.
        Console.WriteLine("FIRST ROW, DOSAGE: {0}", table.Rows[0]["Dosage"]);
    }
}

In application 2:
C#
using System;
using System.Data;
using MyClassLibrary;

static class Program
{
    static void Main()
    {
        // Step 1: get the DataTable.
        DataTable data = MySampleData.GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }
}
 
Share this answer
 
Comments
Harry Pachty 6-Dec-22 8:58am    
Thank you for your solution, this helps me understand what I was looking for. I extract the DataTable into the file defining the namespace. All I had to do now was rename class Program and Main() in the secound application to avoid collision. Thank you again, now I know how to use the remaining 4 examples on that webpage.
I think maybe the detail was not clear. What you need to do is to copy the actual text of the GetTable method from the first source file, and paste it into the second. So your second source file should then look like:
C#
using System;
using System.Data;

class Program
{
    static void Main()
    {
        // This uses the GetTable method (please paste it in).
        DataTable data = GetTable();
        
        // ... Loop over all rows.
        foreach (DataRow row in data.Rows)
        {
            // ... Write value of first field as integer.
            Console.WriteLine(row.Field<int>(0));
        }
    }

// copied from program 1 and pasted here
    static DataTable GetTable()
    {
        // Step 2: here we create a DataTable.
        // ... We add 4 columns, each with a Type.
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Diagnosis", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));
        
        // Step 3: here we add rows.
        table.Rows.Add(25, "Drug A", "Disease A", DateTime.Now);
        table.Rows.Add(50, "Drug Z", "Problem Z", DateTime.Now);
        table.Rows.Add(10, "Drug Q", "Disorder Q", DateTime.Now);
        table.Rows.Add(21, "Medicine A", "Diagnosis A", DateTime.Now);
        return table;
    }
// end of pasted code
}
 
Share this answer
 
Comments
Harry Pachty 6-Dec-22 6:59am    
Thank you for your quick reply Richard MacCutchan but I have explained this in the first paragraph.

The secound example uses the same DataTable from the first example and the comment in the code says to just
//(please paste it in)
Now I tried to avoid that by importing the whole file with the DataTable.


As I said I want to improve my knowledge of C# because I know this is very easy in other languages.
Richard MacCutchan 6-Dec-22 7:20am    
No it does not say to import a DataTable, it is telling you to copy and paste some lines of source code. You cannot import anything into that program since the actual DataTable does not exist anywhere, it is created dynamically by the call to the GetTable method. I suggest you download a copy of .NET Book Zero by Charles Petzold[^], which is an excellent introduction to .NET and C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900