Reputation: 291
I have a custom class defined:
class Car
{
public string a;
public string b;
public string c;
public static void GetCar()
{
var car = new Car[4];
for(int i=0; i<4; ++i)
{
novica[i]= new Novica();
novica[i].a="abc";
novica[i].b="abc";
novica[i].c="abc";
}
}
}
This fills the array with values, and now I would like to use this array with the values it gets (loading string from HTML) in a function that is not part of this class. Is it possible and if so, how?
Upvotes: 0
Views: 7413
Reputation: 112324
I suggest a slightly different construction. Provide an array containing all cars as static property
public class Car
{
public static Car[] AllCars { get; private set; }
public Car()
{
// Class and constructor somewhat simplyfied.
AllCars = new Car[4];
for (int i = 0; i < 4; ++i) {
AllCars[i] = new Novica();
}
}
}
Now you can work with cars like this
foreach (Car car in Car.AllCars) {
// do something with car
}
Or access a specific car with
string a = Car.AllCars[i].a;
Upvotes: 0
Reputation: 245399
You can't. Your method doesn't actually return anything.
If you were to change the method signature to return the array the method creates:
public static Car[] GetCar()
{
// Body
return car;
}
The call would become as simple as:
var cars = Car.GetCar();
Upvotes: 2
Reputation: 60498
In order to use it elsewhere, you would need to return the array from your function, like so:
public static Car[] GetCar()
{
var car = new Car[4];
for(int i=0; i<4; ++i)
{
car[i]= new Novica();
car[i].a="abc";
car[i].b="abc";
car[i].c="abc";
}
return car;
}
Upvotes: 5