Solved

C# General Question

Posted on 2013-12-24
6
335 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
Independent Software Vendors: 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 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

Technology Partners: 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!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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