Home > Software design >  How to pass multiple parameters from .Net console app to Class Library?
How to pass multiple parameters from .Net console app to Class Library?

Time:07-27

I have a .Net solution. There are 2 projects inside it - Console App and Class Library.

I have followed this docs https://docs.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?pivots=dotnet-6-0.

However, this solution works only with a single input. How can I pass multiple inputs from console to my function in a library?

Console app:

using StringLibrary;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter your data");
            string firstline = Console.ReadLine();
            string secondline = Console.ReadLine();
            string thirdline = Console.ReadLine();

            string input = string.Concat(firstline, secondline, thirdline);
            Console.WriteLine("This triangle exists?"  
                              $"{(input.CheckTriangleExistance())}");
            Console.WriteLine();
        
    }
}

Library

namespace StringLibrary;

public static class StringLibrary

{
    public static bool CheckTriangleExistance(string f, string s, string t)
    {
        int a = int.Parse(f);
        int b = int.Parse(s);
        int c = int.Parse(t);

        return a > 0 && b > 0 && c > 0
               && a   b > c
               && a   c > b
               && b   c > a;
    }
}

CodePudding user response:

I strongly suggest you keep the "computation" seperate from the display.

static void Main(string[] args)
{
    Console.WriteLine("Enter your data");
        string firstline = Console.ReadLine();
        string secondline = Console.ReadLine();
        string thirdline = Console.ReadLine();

 
        bool resultOne = StringLibrary.CheckTriangleExistance(firstline, secondline, thirdline);

        Console.WriteLine("This triangle exists? = '{0}'", resultOne);
        Console.WriteLine();
    
}

CodePudding user response:

This way?

string firstline = Console.ReadLine();
string secondline = Console.ReadLine();
string thirdline = Console.ReadLine();

Console.WriteLine("This triangle exists?"  
    $"{(StringLibrary.CheckTriangleExistance(firstline, secondline,thirdline))}");
  • Related