Step: 1I have a class Mytest

class MyTest
{
   public string Name { get; set; }
}

Step2: Created a class Demo to use the member variables of the class myTest

class Demo
    {
       
        
        public void GetNames<T>(IList<myTest> info)
          {
              foreach (myTest test in info)
              {
                  Console.WriteLine(test.Name.ToString());
              }

            
        }

    }

Now my question is:

How can call the method GetNames in the main method(C#.net console application) .my requirement is very simple.I just want to access the values of myTest class using the method GetNames and print the value in the main method.Please help me on this.

class Program
          {
        static void Main(string[] args)
        {
            //code to call the method Getnames and print the value of name here..
        }

     }

Recommended Answers

All 5 Replies

Please use code tag :)

static void Main(string[] args)
{
//code to call the method Getnames and print the value of name here..
Demo d = new Demo();
List<MyTest> m = new List<MyTest>();
d.GetNames<MyTest>(m);            
}

Please use code tag :)

static void Main(string[] args)
{
//code to call the method Getnames and print the value of name here..
Demo d = new Demo();
List<MyTest> m = new List<MyTest>();
d.GetNames<MyTest>(m);            
}

Thnx! Ramy..I have tried the same way..but the result is not getting displayed when I call GetNames method..Any sgstns pls...

Did you add any items to the collection before calling GetNames()?

using System;
using System.Collections.Generic;
using System.Text;

namespace selectionsort
{
  public sealed class MyTest
  {
    public string Name { get; set; }
    public MyTest()
    {
      this.Name = string.Empty;
    }
    public MyTest(string Name)
      : this()
    {
      this.Name = Name;
    }
  }

  public class Demo
  {
    public Demo()
    {
    }
    public void GetNames<T>(IList<MyTest> info)
    {
      foreach (MyTest test in info)
      {
        Console.WriteLine(test.Name.ToString());
      }
    }
  }

  public static class MainClass
  {
    public static void Main(string[] args)
    {
      List<MyTest> lst = new List<MyTest>();
      lst.Add(new MyTest("Scott"));
      lst.Add(new MyTest("Ramy"));
      lst.Add(new MyTest("Danny"));
      lst.Add(new MyTest("Serkan"));
      Demo d = new Demo();
      d.GetNames<MyTest>(lst);
    }
  }
}

Hi Scott,
Thnx! for your quick reply.This is exactly I was searching for..I have tested and it works..

Cheers!!

I'm glad we could help you out. I added on to Ramy's code to populate the list :)

Please mark this thread as solved if you have found a solution and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.