Reputation: 213
I have the following method and i am trying to get data from a column called ApptDateTime
and set it to a public class so that i can pass the value into another method.
This is what i was trying to set the value in the public class:
public class ApptData
{
public DateTime _currentAppointment { get; set; }
}
I select the value _currentAppointment
and i want to be able to call it from public class ApptData
And then pass that into a Method.
EX: history.LoadHistory(ApptData appointment, daysOfHistory);
private void dgvPendingSB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
DataRowView row = (DataRowView)e.AddedItems[0];
int accNum = -1;
int resourceId = Convert.ToInt32(row["ResourceId"]);
int locationId = Convert.ToInt32(row["LocationId"]);
int patientId = Convert.ToInt32(row["PatientId"]);
_currentAppointment = Convert.ToDateTime(row["ApptDate"]);
m_objPatient = Convert.ToInt32(row["PatientId"]);
_stringPatientName = row["PatientName"].ToString();
// ...
}
}
Upvotes: 2
Views: 152
Reputation: 1919
Not really sure I understand you question. Do you want to use the class itself instead of an instance (is that what you mean by "public class?")? If so... You could use a default object to carry your property:
public class ApptData
{
private static ApptData _defaultInstance;
public static ApptData Default
{
if (_defaultInstance == null)
{
_defaultInstance = new ApptData();
}
return _defaultInstance;
}
public DateTime CurrentAppointment { get; set; }
}
You can access the property like this:
ApptData.Default.CurrentAppointment = Convert.ToDateTime(row["ApptDate"]);
history.LoadHistory(ApptData.Default, daysOfHistory);
But to me that is a very awkward way of passing data. The only use this could serve is that you can access the value of that property from anywhere that ApptData class is visible. Of course, you could only process one value at a time. I would do what the previous readers suggested and have everything as instantiated objects.
Upvotes: 0
Reputation: 91598
If you're trying to pass a new instance of ApptData
into LoadHistory
with a value from your DataRowView
set, you'll want something like:
history.LoadHistory(new ApptData()
{
_currentAppointment = Convert.ToDateTime(row["ApptDate"])
}, daysOfHistory);
Also, I strongly recommend you don't start public
properties, fields or methods with an underscore. It's just weird.
Upvotes: 3
Reputation: 34846
You need to instantiate an instance of ApptData
, set the value for __currentAppointment
and then pass that to the LoadHistory
method, like this:
var theAppointmentData = new ApptData();
theAppointmentData._currentAppointment = Convert.ToDateTime(row["ApptDate"]);
Then you can call your LoadHistory
method, like this:
history.LoadHistory(theAppointmentData, daysOfHistory);
Upvotes: 0