Solved

C# General Question

Posted on 2013-12-24
6
330 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 200 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 13

Accepted Solution

by:
Naman Goel earned 300 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Silverlight ERP To Angularjs,HTML5 3 62
Build a string of emails from a gridview 2 20
Convert VB web project to C# 3 41
Adjust the codes 3 37
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.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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