1

Hi i have created a Generic Array that works fine for Int,String, Float or even my Own type named Customers.

Generic Array has functions Add(), Sort(), ShowAll() thats working fine for Int, String, and even Customer Type except when i try to showAll() method for CustomerType that shows all the values that i have added through ADD() method.

output is something like GenericArray.Customer

not the values where as i wanted to have the values .

i have solved it through

public class GArray<T> where T : Customer

but now i cant create Generic Array of type Int,Float .

here is the ADD and ShowAll method of Class

public void Add(T temp)
        {

            if (index >= values.Length)
            {
                T[] tempArray = new T[values.Length + 1];
                Array.Copy(values, tempArray, values.Length);
                values = tempArray;
            }
            values[index] = temp;
            index++;  
        }

 public void ShowAll()
    {
        for (int i = 0; i < values.Length; i++)
        {
            Console.WriteLine(values[i]);                
        }
    }

the values m adding

 static void Main(string[] args)
        {                        
            GArray<Customer> customers = new GArray<Customer>(3);
            customers.Add(new Customer(101, "xyz"));
            customers.Add(new Customer(59, "abc"));

            customers.ShowAll();
            }

i have talked with my frnd and he said that i have to create indexer my self . can some one help me how can i create indexer in this case that works fine for customerType or any Type.

4
  • lmgtfy? msdn.microsoft.com/en-us/library/aa288465(v=vs.71).aspx Commented May 13, 2011 at 8:11
  • By Indexer, do you mean enumerator or some GetValueByIndex(int) function? Commented May 13, 2011 at 8:16
  • 1
    Why didn't you extend List<T>? It already does dynamic allocation under the hood (and a bit more efficiently, since you are reallocating in 1 byte increments every time). Commented May 13, 2011 at 8:26
  • Groo i wish i cld have extended List but there was restriction from my teacher so i have to extend simple Array :) Commented May 13, 2011 at 8:32

4 Answers 4

2

I think,If I understand the question (output is something like GenericArray.Customer, not the values where as i wanted to have the values) you should add in Customer definition:

public override string ToString()
{
    // return something you want to show to identify your customer
    // e.g. return Name;  
    return ...           
}

I explain: when you use Console.WriteLine(values[i]) you tell C# to write to console Customer object... and it writes out then name of the class, as it's the default behaviour.
Defining in Customer class the default string to be converted to makes what you please...

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to add the ToString call to the Console.WriteLine call -- Console.WriteLine is already calling ToString. The default implementation, in System.Object, simply displays the class name. All the OP needs to do is add the override.
U understood question Rite and ur Solution solved my Problem thankz for Your Help .. Thank You very Much :) u saved my Life :d
@user751959: if I could really save some life just by typing on my keyboard, it will be really great!!! ;) Glad to help you :)
0
public T this[int index]
{
  get {return values[index]; }
}

1 Comment

i have tried ur solution but it didnt helped me can u plz elaborate more. it wld be good if i be able to solve it through indexers
0

I think your problem is that you have not overridden ToString in your customer class. Do that -- it will define how the objects should be displayed in the console.

2 Comments

I think the same and I've already posted some code... this makes me think I've understood the question :)
Yeah, I'm typing on my phone so it took me a long time to get that short answer out. :-(
0

Your actual problem aside for a moment, I would like to mention that there is no place for a ShowAll method in an array implementation. Why should an array be tied to a console application? Wouldn't you want to reuse it for a Windows Forms application oneday without the need to rewrite it?

Next, .NET already has a List<T> which does dynamic allocation as necessary. If you do want to write it again yourself, at least allocate the array in bigger steps (n*2 each time).

To remove the ShowAll method from the array (where it doesn't belong), you should consider taking one of the following approaches:

a) Create an extension method which works for any IEnumerable<T> (a List, Array, Collection, whatever):

 public static class EnumExt
{
     public static void ShowAll<T>(this IEnumerable<T> list)
     {
         foreach (T item in list)
            Console.WriteLine(item);
     }
}

Usage:

int[] array = new int[] { 1,2,3};
array.ShowAll();

b) Or, be even more abstract and create a ForEach extension method where you will pass an arbitrary delegate to perform actual work:

public static class EnumExt
{
     public static void ForEach<T>(this IEnumerable<T> list, Action<T> action)
     {
         foreach (T item in list)
            action(item);
     }
}

Usage:

int[] array = new int[] { 1,2,3};
// now you are reusing the iterator
// for any action you want to execute
array.ForEach(Console.WriteLine);
// or
array.ForEach(item => Console.WriteLine("My item is: " + item));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.