I wish to create an object of a class that will be accesible in multiple forms. Basically I have a form that creates an object, but want this object accesible in whole program. Any way to do it?
Asked
Active
Viewed 40 times
0
-
What kind of object? What is this *object* for? Should it be read-only after it's created? -- Post the definition of your object and add a description of its functionality (click the [Edit](https://stackoverflow.com/posts/73035094/edit) link to update your question) – Jimi Jul 19 '22 at 10:22
1 Answers
0
Well, there are many ways to do it.
-creating a new instance in the main form and accessing it from there
(Creating a new instance of form1 and change that forms property)
(i don't really suggest this one, but if you want you can do it)
-creating a static class with a static property (i suggest you this, it's more schematic i think)
just like this:
in MyStaticClass.cs:
public static class MyClass { public static int MyProp { get; set; }}
and accessing the property like this:
MyClass.MyProp = 1;
//or
int MyInt = MyClass.MyProp;
you could also use one of these two ways to store it:
-saving it in text format in a .txt or .bin file
-saving it in a database (something simple is sqlite but for sure it's more difficult than the others)
these are all the ideas i got

Juri Mason
- 61
- 5
-
I am very thankful to you, static class method works like a wonder, thank you so much! – Krzysztof Kulka Jul 19 '22 at 11:06