Using a referenced C# object in a seperate class/form












0














I've got a Windows Form program that creates a Config object which contains various configuration variables used by my program.
Within the main form, it contains a button to open a new configuration form, where it passes the Config object as a reference -



FormConfig button = new FormConfig(ref config);
button.ShowDialog();


Now in the FormConfig class, I can access the Config object within the main constructor



public FormConfig(ref Config config)
{
InitializeComponent();
// can access config.xyz OK here
}


However within the new form, I've got a button that calls another function that needs to access the reference Config object, however I'm struggling to find a clean way to do so.
I can create another Config object as part of the FormConfig class, and then copy the referenced Config to it in the main constructor, however then the original config object doesn't get updated.



How can I achieve this?



PS apologies in advance if this is already answered, but my searches have so far failed to find a solution, possibly because I'm not sure what the correct search terms should be.










share|improve this question






















  • Please provide where you need the config and how you update it.
    – HimBromBeere
    Nov 23 '18 at 11:11








  • 2




    Is Config a class? If so you don't need to pass it by ref. Does that help?
    – Enigmativity
    Nov 23 '18 at 11:12










  • What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
    – ikerbera
    Nov 23 '18 at 11:12










  • Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
    – Fabio
    Nov 23 '18 at 11:12






  • 2




    You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
    – HimBromBeere
    Nov 23 '18 at 11:45


















0














I've got a Windows Form program that creates a Config object which contains various configuration variables used by my program.
Within the main form, it contains a button to open a new configuration form, where it passes the Config object as a reference -



FormConfig button = new FormConfig(ref config);
button.ShowDialog();


Now in the FormConfig class, I can access the Config object within the main constructor



public FormConfig(ref Config config)
{
InitializeComponent();
// can access config.xyz OK here
}


However within the new form, I've got a button that calls another function that needs to access the reference Config object, however I'm struggling to find a clean way to do so.
I can create another Config object as part of the FormConfig class, and then copy the referenced Config to it in the main constructor, however then the original config object doesn't get updated.



How can I achieve this?



PS apologies in advance if this is already answered, but my searches have so far failed to find a solution, possibly because I'm not sure what the correct search terms should be.










share|improve this question






















  • Please provide where you need the config and how you update it.
    – HimBromBeere
    Nov 23 '18 at 11:11








  • 2




    Is Config a class? If so you don't need to pass it by ref. Does that help?
    – Enigmativity
    Nov 23 '18 at 11:12










  • What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
    – ikerbera
    Nov 23 '18 at 11:12










  • Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
    – Fabio
    Nov 23 '18 at 11:12






  • 2




    You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
    – HimBromBeere
    Nov 23 '18 at 11:45
















0












0








0







I've got a Windows Form program that creates a Config object which contains various configuration variables used by my program.
Within the main form, it contains a button to open a new configuration form, where it passes the Config object as a reference -



FormConfig button = new FormConfig(ref config);
button.ShowDialog();


Now in the FormConfig class, I can access the Config object within the main constructor



public FormConfig(ref Config config)
{
InitializeComponent();
// can access config.xyz OK here
}


However within the new form, I've got a button that calls another function that needs to access the reference Config object, however I'm struggling to find a clean way to do so.
I can create another Config object as part of the FormConfig class, and then copy the referenced Config to it in the main constructor, however then the original config object doesn't get updated.



How can I achieve this?



PS apologies in advance if this is already answered, but my searches have so far failed to find a solution, possibly because I'm not sure what the correct search terms should be.










share|improve this question













I've got a Windows Form program that creates a Config object which contains various configuration variables used by my program.
Within the main form, it contains a button to open a new configuration form, where it passes the Config object as a reference -



FormConfig button = new FormConfig(ref config);
button.ShowDialog();


Now in the FormConfig class, I can access the Config object within the main constructor



public FormConfig(ref Config config)
{
InitializeComponent();
// can access config.xyz OK here
}


However within the new form, I've got a button that calls another function that needs to access the reference Config object, however I'm struggling to find a clean way to do so.
I can create another Config object as part of the FormConfig class, and then copy the referenced Config to it in the main constructor, however then the original config object doesn't get updated.



How can I achieve this?



PS apologies in advance if this is already answered, but my searches have so far failed to find a solution, possibly because I'm not sure what the correct search terms should be.







c#






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 11:09









EmmCeeEmmCee

12




12












  • Please provide where you need the config and how you update it.
    – HimBromBeere
    Nov 23 '18 at 11:11








  • 2




    Is Config a class? If so you don't need to pass it by ref. Does that help?
    – Enigmativity
    Nov 23 '18 at 11:12










  • What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
    – ikerbera
    Nov 23 '18 at 11:12










  • Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
    – Fabio
    Nov 23 '18 at 11:12






  • 2




    You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
    – HimBromBeere
    Nov 23 '18 at 11:45




















  • Please provide where you need the config and how you update it.
    – HimBromBeere
    Nov 23 '18 at 11:11








  • 2




    Is Config a class? If so you don't need to pass it by ref. Does that help?
    – Enigmativity
    Nov 23 '18 at 11:12










  • What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
    – ikerbera
    Nov 23 '18 at 11:12










  • Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
    – Fabio
    Nov 23 '18 at 11:12






  • 2




    You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
    – HimBromBeere
    Nov 23 '18 at 11:45


















Please provide where you need the config and how you update it.
– HimBromBeere
Nov 23 '18 at 11:11






Please provide where you need the config and how you update it.
– HimBromBeere
Nov 23 '18 at 11:11






2




2




Is Config a class? If so you don't need to pass it by ref. Does that help?
– Enigmativity
Nov 23 '18 at 11:12




Is Config a class? If so you don't need to pass it by ref. Does that help?
– Enigmativity
Nov 23 '18 at 11:12












What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
– ikerbera
Nov 23 '18 at 11:12




What you need the config for? Maybe it's better for you to have a config file that can access through the whole application.
– ikerbera
Nov 23 '18 at 11:12












Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
– Fabio
Nov 23 '18 at 11:12




Save provided config object to the private member in the FormConfig, so button handlers will have access to it.
– Fabio
Nov 23 '18 at 11:12




2




2




You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
– HimBromBeere
Nov 23 '18 at 11:45






You should definitly have a look at how reference-types and ref works. A class-instance isn´t "copied", as you say. It´s reference is copied. However changing anything on that reference will be reflected in all references to the same instance, which is what you seem to want. So imit the ref and every variable referencing your config is updated.
– HimBromBeere
Nov 23 '18 at 11:45














1 Answer
1






active

oldest

votes


















-1














And the solution thanks to @cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -



public static class Config
{
public static bool SettingA = true;
}


Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -



Config.SettingA


Thanks to all those who helped point me in the right direction.






share|improve this answer





















  • Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
    – Fabio
    Nov 23 '18 at 23:17










  • Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
    – Hans Passant
    Nov 23 '18 at 23:39










  • This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
    – Enigmativity
    Nov 24 '18 at 4:45










  • I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
    – EmmCee
    Nov 24 '18 at 13:33










  • @Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
    – EmmCee
    Nov 24 '18 at 13:36











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445582%2fusing-a-referenced-c-sharp-object-in-a-seperate-class-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














And the solution thanks to @cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -



public static class Config
{
public static bool SettingA = true;
}


Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -



Config.SettingA


Thanks to all those who helped point me in the right direction.






share|improve this answer





















  • Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
    – Fabio
    Nov 23 '18 at 23:17










  • Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
    – Hans Passant
    Nov 23 '18 at 23:39










  • This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
    – Enigmativity
    Nov 24 '18 at 4:45










  • I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
    – EmmCee
    Nov 24 '18 at 13:33










  • @Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
    – EmmCee
    Nov 24 '18 at 13:36
















-1














And the solution thanks to @cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -



public static class Config
{
public static bool SettingA = true;
}


Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -



Config.SettingA


Thanks to all those who helped point me in the right direction.






share|improve this answer





















  • Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
    – Fabio
    Nov 23 '18 at 23:17










  • Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
    – Hans Passant
    Nov 23 '18 at 23:39










  • This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
    – Enigmativity
    Nov 24 '18 at 4:45










  • I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
    – EmmCee
    Nov 24 '18 at 13:33










  • @Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
    – EmmCee
    Nov 24 '18 at 13:36














-1












-1








-1






And the solution thanks to @cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -



public static class Config
{
public static bool SettingA = true;
}


Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -



Config.SettingA


Thanks to all those who helped point me in the right direction.






share|improve this answer












And the solution thanks to @cmos, is to declare the Config class as static, which negates the need to use any referencing or passing objects between classes/functions -



public static class Config
{
public static bool SettingA = true;
}


Which means I can access and modify the Config object from anywhere within the same namespace with the following code, without needing to have a class instance -



Config.SettingA


Thanks to all those who helped point me in the right direction.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 23:15









EmmCeeEmmCee

12




12












  • Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
    – Fabio
    Nov 23 '18 at 23:17










  • Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
    – Hans Passant
    Nov 23 '18 at 23:39










  • This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
    – Enigmativity
    Nov 24 '18 at 4:45










  • I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
    – EmmCee
    Nov 24 '18 at 13:33










  • @Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
    – EmmCee
    Nov 24 '18 at 13:36


















  • Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
    – Fabio
    Nov 23 '18 at 23:17










  • Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
    – Hans Passant
    Nov 23 '18 at 23:39










  • This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
    – Enigmativity
    Nov 24 '18 at 4:45










  • I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
    – EmmCee
    Nov 24 '18 at 13:33










  • @Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
    – EmmCee
    Nov 24 '18 at 13:36
















Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
– Fabio
Nov 23 '18 at 23:17




Good example where quick workaround will lead to the wrong path. But it depends on the project requirements.
– Fabio
Nov 23 '18 at 23:17












Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
– Hans Passant
Nov 23 '18 at 23:39




Its practical. Do make sure that dialog only has an OK button and not a Close or Cancel button.
– Hans Passant
Nov 23 '18 at 23:39












This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
– Enigmativity
Nov 24 '18 at 4:45




This is not a good long-term solution. This will make your code fragile to changes and make it harder to test. In general passing objects around is better that static variables.
– Enigmativity
Nov 24 '18 at 4:45












I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
– EmmCee
Nov 24 '18 at 13:33




I can see the potential pitfalls of this method, however it's only for some basic setting values. This area of code will get revisited again as I'm still figuring out exactly what I need and don't need. This program is talking to an embedded controller, and I'm still at the point of working out the intricacies of handling the data exchanges, so just need someway to change settings without having to edit the code.
– EmmCee
Nov 24 '18 at 13:33












@Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
– EmmCee
Nov 24 '18 at 13:36




@Enigmativity previously I've used Program.Form1.Method() to access methods in the parent class/form, however I feel that isn't an ideal way. Is there a better method? I still need to go and get a better understanding of references, but I'd be appreciative of any pointers to things I should be looking at.
– EmmCee
Nov 24 '18 at 13:36


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445582%2fusing-a-referenced-c-sharp-object-in-a-seperate-class-form%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Catalogne

Violoncelliste

Héron pourpré