C
C#15mo ago
Wisch10

Is there a way to return or access a class object created inside a public partial class (form) from

I have a process that allows expenses to be added from a form, and a separate process to add an asset from a form. I would like to add a combination of the two if the expense is for an asset but to run the db inserts in the same function so that if one fails, both are rolled back. I created my second asset form to accept the values for price, purchase date, and payment type and have that only being generated when the expense type is an asset. FrmAssetFromExpense frmAssetFromExpense = new(expenseDate, amount, type); fromAssetFromExpense.ShowDialog(); Inside the new form for asset addition, all the items are in place to create the Asset class object from the values entered on the form. Is there a way to return that Asset object to the main expense form to run a single sql insert?
13 Replies
Pobiega
Pobiega15mo ago
Yeah. The easiest way here would be for FrmAssetFromExpense to expose the created object as a property you could then do
FrmAssetFromExpense frmAssetFromExpense = new(expenseDate, amount, type);
if(fromAssetFromExpense.ShowDialog() == DialogResult.Ok)
{
var asset = fromAssetFromExpense.CreatedAsset;
...
}
FrmAssetFromExpense frmAssetFromExpense = new(expenseDate, amount, type);
if(fromAssetFromExpense.ShowDialog() == DialogResult.Ok)
{
var asset = fromAssetFromExpense.CreatedAsset;
...
}
Wisch10
Wisch1015mo ago
ok, so then inside the FrmAssetFromExpense form, I would need to add this CreatedAsset as a property by adding public Assets CreatedAsset {get; set;}?
Pobiega
Pobiega15mo ago
yeah, but private set 🙂 dont allow it to be changed from the outside.
Wisch10
Wisch1015mo ago
do I do that inside the public form class or the public partial?
Pobiega
Pobiega15mo ago
never touch designer files yourself
Wisch10
Wisch1015mo ago
to the designer, the cs file for the actions and values on the form not*
Pobiega
Pobiega15mo ago
yes thats where your custom code goes. I don't understand the question tbh, your form is a public partial class FormWhatever : Form there are really no alternatives to that :p
Wisch10
Wisch1015mo ago
one second, pulling up discord on my other machine to show you
Wisch10
Wisch1015mo ago
Pobiega
Pobiega15mo ago
what is the error for the CreatedAsset prop?
Wisch10
Wisch1015mo ago
that my class Assets was less accessible, so I set that to public as well and that went away, but I think I need to turn a few more classes public to resolve the cascade of errors, since it impacted dropdowns, etc.
Pobiega
Pobiega15mo ago
sure.
Wisch10
Wisch1015mo ago
thank you for your assistance