?
Solved

C# General Question

Posted on 2013-12-24
6
Medium Priority
?
338 Views
Last Modified: 2013-12-27
I am new to the C# Language.
For example If I have 3 classes and each class has its own methods and print statements for example. If I plan on calling one of these methods how do I isolate the results from main so I get the output from just one of my classes not all of them. What is the best way to structure my code. If some one could give me an example that would be great.

Thanks In Advance
0
Comment
Question by:VitaminD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39738912
if(useClass1)  //something to choose which class you want to print from
{
Class1 c1 = new Class1();
c1.PrintOut();  //Only prints stuff from class 1
}
else
{
Class2 c2 = new Class2();
c2.PrintOut();  //only prints stuff from class 2
}
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39738923
If you wanted to take a more object oriented route, you could define an interface that all of your classes implement. Then just create an instance of the class you care about.

e.g.

public interface IPrintable
{
    void Print();
}

public class Class1 : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Class1 printing...");
    }
}

public class Class2 : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Class2 printing...");
    }
}

public class Class3 : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Class3 printing...");
    }
}

public class Program
{
    public static void Main()
    {
        IPrintable instance = new Class1();
        // or
        // IPrintable instance = new Class2();
        // or
        // IPrintable instance = new Class2();
        
        instance.Print();
    }
}

Open in new window

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 800 total points
ID: 39741103
I have a suspicion that we do not understand the author correctly.
My understanding is as follows. We may have a number of classes. These classes may have completely different interfaces. For some reasons, e.g., debugging, we may want only some class outputs would be printed.
Is this correct? In this case we may need some other approach.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
Naman Goel earned 1200 total points
ID: 39741450
what do you mean by isolating results from main? I didn't get what is the problem here. If you want to use some class then you can directly create object of that class and use it or if you want to conditionally call some method from one particular class you can do so directly by declaring that or putting condition as suggested by  AndyAinscow.

Even you can use reflection to achieve the same. Here user will pass class name as command line arguments and method from that class will be executed.

using System;
using System.Reflection;

namespace ConsoleApplication1
{
    public interface IInterface
    {
        void Print();
    }

    public class MyClass1:IInterface
    {

        public void Print()
        {
            Console.WriteLine(this.GetType().Name);
        }
    }

    public class MyClass2:IInterface
    {

        public void Print()
        {
            Console.WriteLine(this.GetType().Name);
        }
    }

    public class MyClass3 : IInterface
    {

        public void Print()
        {
            Console.WriteLine(this.GetType().Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
           IInterface objectInstance=null;
           Type[] types = Assembly.GetExecutingAssembly().GetTypes();
           Type callingType = null;
           foreach (var item in types)
           {
               if (String.Compare(item.Name, args[0], false) == 0)
               {
                   callingType = item;
                   break;
               }
           }
           objectInstance = Activator.CreateInstance(callingType) as IInterface;
           objectInstance.Print();
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39741645
Interesting answer selection, to say the least  : \
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39742314
For someone new to C# the selected answer is a rather complex technique - do you actually understand it ?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question