Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
New to C# and want to know if there is a way to pass in all occurrences of of an array declared class element?

In other words -

class something()
{
string a;
string b;
}


class abc()
{
protected void main()
{
something s[] = new something[5]
... initialize array

CallRoutine(something.a[]) or
CallRoutine(something[].a)
}

protected void CallRoutine(string[] receiver)
{
loop through string array...
}
}
Posted

It should be like this:

C#
public class Something()
{
    public string a;
    piblic string b;
}


public class Abc()
{
    public  void main()
    {
        Something[] s = new Something[5];
        //... initialize array

        CallRoutine(s);
    }

    protected void CallRoutine(Something[] receiver)
    {
        foreach(Something s in receiver)
        {
            // do something with s an instance of Something here
        }
    }
}


Understandable? If not leave a comment.

Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 19:12pm    
My 5. I suspect OP's idea was deeper; he did not explain the "higher goal".
--SA
Nish Nishant 9-Feb-11 20:04pm    
Voted 5. Good response.
Steve van Niman 10-Feb-11 9:39am    
Manfred, I'm just trying to pass in one of the elements of the class, not the entire class. Basically I'm trying to map data on input from an Excel spreadsheet into the class. Based on the column in the sheet determines what needs to be done in the class, thus I have to know what the column is. Right now I have a big switch section and then for each column type, have a loop. There are 30 different column types, thus I have 30 loops. Would love to have one loop in a subroutine.
Espen Harlinn 10-Feb-11 17:21pm    
Good effort, a 5
So are you trying to pass the 'a' members of the array? If so, there is nothing you can get build-in. You have to create an array of all a's to pass. But that defeats the whole purpose. How would you know which a belong to which object? While you can use the index why go all the hard way.

Pass your array and in your method you can do what ever you want to do.

something s[] = new something[5]
... initialize array

CallRoutine(something)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Feb-11 19:10pm    
If you look at Manfred's answer, he did this already.
--SA
Yusuf 9-Feb-11 19:25pm    
Oops, blame it on slow typing. Come on who is going to check if an answer is posted after typing few lines. :-)
Sergey Alexandrovich Kryukov 9-Feb-11 20:31pm    
I know, it happens to me all the time :-)
--SA
Here's an answer where I've interpreted your requirement differently to what Manfred did. I am going to assume you want to pass a string array consisting of all the strings obtained by extracting either something.a or something.b. If so, you can do this using LINQ. Here's an example:

C#
class something
{
    public string a;
    public string b;
}

class abc
{
    public static void CallRoutine(string[] receiver)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        something[] s = new something[5];
        // init data

        abc.CallRoutine(s.Select(it => it.a).ToArray());
    }
}
 
Share this answer
 
Comments
Steve van Niman 10-Feb-11 9:44am    
Nishant,

This is what I am attempting to do and it almost works... I do like the creativity of using Linq. The Array is passed in correctly and I can do work on the array inside of the CallRoutine; however, upon exit it loses the changes (as it should). I tried using ref to pass a pointer, but I'm getting compiler errors. "A ref or out argument must be an assignable variable".
Nish Nishant 10-Feb-11 9:47am    
Well, are you trying to change the member strings? That will not work since strings are immutable.
Espen Harlinn 10-Feb-11 17:22pm    
Good answer, my 5
Nish Nishant 10-Feb-11 17:27pm    
Thank you.

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