Is is possible to list our the variable names from the instance and it value.
public class Car
{
public string Color;
public string Model;
public string Made;
}
protected void Page_Load(object sender, EventArgs e)
{
//Create new instance
Car MyCar = new Car();
MyCar.Color = "Red";
MyCar.Model = "NISSAN";
MyCar.Made = "Japan";
//SOMETHING HERE
foreach (MyCar Variable in MyCar)
{
Response.Write("<br/>Variable Name"+ "XXX"+ "Variable Value");
}
}
Try something like this:
using System;
class Car
{
public string Color;
public string Model;
public string Made;
}
class Example
{
static void Main()
{
var car = new Car
{
Color = "Red",
Model = "NISSAN",
Made = "Japan"
};
foreach (var field in typeof(Car).GetFields())
{
Console.WriteLine("{0}: {1}", field.Name, field.GetValue(car));
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With