Using a referenced C# object in a seperate class/form
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#
|
show 12 more comments
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#
Please provide where you need the config and how you update it.
– HimBromBeere
Nov 23 '18 at 11:11
2
IsConfiga class? If so you don't need to pass it byref. 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 therefand every variable referencing your config is updated.
– HimBromBeere
Nov 23 '18 at 11:45
|
show 12 more comments
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#
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#
c#
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
IsConfiga class? If so you don't need to pass it byref. 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 therefand every variable referencing your config is updated.
– HimBromBeere
Nov 23 '18 at 11:45
|
show 12 more comments
Please provide where you need the config and how you update it.
– HimBromBeere
Nov 23 '18 at 11:11
2
IsConfiga class? If so you don't need to pass it byref. 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 therefand 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
|
show 12 more comments
1 Answer
1
active
oldest
votes
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.
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
|
show 4 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
|
show 4 more comments
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.
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
|
show 4 more comments
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.
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.
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
|
show 4 more comments
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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please provide where you need the config and how you update it.
– HimBromBeere
Nov 23 '18 at 11:11
2
Is
Configa class? If so you don't need to pass it byref. 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
refand every variable referencing your config is updated.– HimBromBeere
Nov 23 '18 at 11:45