Solved

C# General Question

Posted on 2013-12-24
6
320 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
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 13

Accepted Solution

by:
Naman Goel earned 300 total points
Comment Utility
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 74

Expert Comment

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

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now