Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:53:55+00:00 2026-06-09T15:53:55+00:00

In the C# code below, I am trying to execute the code in the

  • 0

In the C# code below, I am trying to execute the code in the consoleread method by instantiating it within the Main method. I am a complete beginner at OOP and it seems to work but I just want to be sure if this is the right way to do it?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;

namespace ConsoleApplication1
{
    class ConsoleRead
    {
        public void consoleread()
        {
            string[] sPorts = SerialPort.GetPortNames();
            foreach (string port in sPorts)
            {
                var serialPort = new SerialPort();
                serialPort.PortName = port;
                serialPort.Open();
                serialPort.WriteLine("ATI");
                var message = Console.ReadLine();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ConsoleRead c = new ConsoleRead();
            c.consoleread();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:53:56+00:00Added an answer on June 9, 2026 at 3:53 pm

    Yes it’s alright. You have a class with an instance method, so you instantiate that class and call the method against the instance.

    Now, for the question “is this the right way to do”, then it’s a little hard to answer without more background. In your particular example, if the only goal of your application is to write some string on serial ports without any reusability concerns, then creating a class that is instantiated only once is probably useless, you should just move the code from your consoleread method to the main method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get the code below to work but the success doesn't execute -
In the code below, I am trying to execute a method, which returns a
I am trying to run the code below but it keeps locking up my
I am trying to port the code below to mootools 1.1 code but am
I am trying to execute the code below to list the select item in
I am trying to execute PHP code in XML Below is the code is
I'm trying to get the code below to execute an event that triggers once
I'm trying to get the code below to work..... The error: ERRORSQLSTATE[42000]: Syntax error
I was trying to execute the below code in my javascript and got some
I am trying to execute the below code from a console app. ConfigurationSection aggregatedFileSection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.