C
C#•2y 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
Pobiega•2y 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
Wisch10OP•2y 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
Pobiega•2y ago
yeah, but private set 🙂 dont allow it to be changed from the outside.
Wisch10
Wisch10OP•2y ago
do I do that inside the public form class or the public partial?
Pobiega
Pobiega•2y ago
never touch designer files yourself
Wisch10
Wisch10OP•2y ago
to the designer, the cs file for the actions and values on the form not*
Pobiega
Pobiega•2y 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
Wisch10OP•2y ago
one second, pulling up discord on my other machine to show you
Wisch10
Wisch10OP•2y ago
Pobiega
Pobiega•2y ago
what is the error for the CreatedAsset prop?
Wisch10
Wisch10OP•2y 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
Pobiega•2y ago
sure.
Wisch10
Wisch10OP•2y ago
thank you for your assistance
Want results from more Discord servers?
Add your server