Click here to Skip to main content
15,911,715 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hi ,

I have class named Testdata like
C#
public class Testdata {

    public string x;
    public string y;
    public test[] z;
}

and another class
C#
public class test{
    public string m;
    public string d;
}

I need to send the Testdata class as the input parameter which should have values of class test also .

I created object like
C#
Testdata obj = new Testdata();

and again
C#
obj.m = new test();

I got the error like
Quote:
cannot implicitly convert test to test[]


any help in thiss?????
Posted
Updated 20-May-14 6:49am
v2
Comments
[no name] 20-May-14 12:53pm    
Help with what? The error message is clear. You have an array of "test" and a "new test" is not an array is it? You would have to say something like obj.z[0] = new test()
CHill60 20-May-14 12:55pm    
When I put your code into a C# project I get the error "'ComparisonHelper.Testdata' does not contain a definition for 'm' and no extension method 'm' accepting a first argument of type 'ComparisonHelper.Testdata' could be found (are you missing a using directive or an assembly reference?) "
Have you missed some code here
[no name] 20-May-14 13:05pm    
If you replace "obj.m" with "obj.z" you would probably get that error.
CHill60 20-May-14 13:16pm    
True ... then obj.z[0] = new test(); will stop the compile errors but give a runtime exception of "Object reference not set to an instance of an object." ... I really don't get what the OP is trying to do :(
[no name] 20-May-14 14:19pm    
Well that makes you and everyone else that has read it not understanding. Methinks that the OP does not quite understand working with arrays.

Other than the typo in the question:
C#
obj.m = new test();
// should be
obj.z = new test();
// to reproduce the error.


The test array in Testdata.z must be initialized to a correctly sized array before you can put any content in the array.
C#
public class Testdata {
    public string x;
    public string y;
    public test[] z = new test[named constant for correct size of the array];
}

So the next question is: At the point where you create the Testdata instance, do you know how many entries the Testdata.z array must contain or is its size dynamic?
It seems most likely that the size is not pre-determined, so changing the declaration of z from an array of test to a List<test> would be a good solution:
C#
public class Testdata {
    public string x;
    public string y;
    public List<test> z = new List<test>();
}

Then the test instances can be added to the z collection with:
C#
test theTest = new test();
// initialize theTest appropriately
obj.z.Add(new theTest);


See List< T > class[^] for more information about using that collection type.

Edit - Matt T Heffron: followup from comments.

1. initialize the z array with the appropriate size when Testdata is created, from outside of the Testdata class right after it is created with new Testdata()
C#
Testdata obj = new Testdata();
obj.z = new test[named constant for correct size of the array];

2. initialize the array as in #1 to an empty array and then manage the size of the array manually whenever you add items to the array (probably using System.Array.Resize() )
C#
Testdata obj = new Testdata();
obj.z = new test[0];

Then everywhere you need to add a new element to the array:
C#
Array.Resize(ref obj.z, obj.z.Length+1);
obj.z[obj.z.Length-1] = new test();

This is very inefficient, so don't do this unless you have no other choice!

3. If you can, create and collect all of the instances of the test class for a given Testdata in a List and when they are all there, then set the Testdata.z to the collection converted to an array. E.g., using Linq this would be obj.z = testsList.ToArray();
C#
Testdata obj = new Testdata();
List<test> testsList = new List<test>();
// here construct/initialize all of the instances of test for this instance of Testdata
test nt = new test();
// initialize this test
testsList.Add(nt);
// after all of the instances of test are created and collected into testsList:
obj.z = testsList.ToArray();
 
Share this answer
 
v2
Comments
vishnugettam 20-May-14 13:59pm    
Hii,thnks for ur response
Here the situation for me is we should not change the class object ,so we need to maintain the array class only
Matt T Heffron 20-May-14 14:13pm    
Then you have three options:
1. initialize the z array with the appropriate size when Testdata is created, from outside of the Testdata class right after it is created with new Testdata()
2. initialize the array as in #1 to an empty array and then manage the size of the array manually whenever you add items to the array (probably using System.Array.Resize() )
3. If you can, create and collect all of the instances of the test class for a given Testdata in a List<test> and when they are all there, then set the Testdata.z to the collection converted to an array. E.g., using Linq this would be obj.z = testsList.ToArray();
vishnugettam 20-May-14 14:17pm    
Please can you eloborate your comments....
Matt T Heffron 20-May-14 14:57pm    
See edited solution above.
vishnugettam 20-May-14 15:24pm    
it WOrked ..i will apply the same in the Code and Very thanks for your respose ....Thnk U so much
If i understand you well, you want to store the array of test class inside Testdata class. If i'm not wrong, please see this article: Walkthrough: Creating Your Own Collection Class[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900