Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# General Question

Posted on 2013-12-24
6
Medium Priority
?
342 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 45

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 45

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

604 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