5
Answers

initialize class once

Photo of lemoinek

lemoinek

20y
2.5k
1
i have a windows application with several forms. I want to initialize a class in 1 form soo that its is also accissible for the other forms. The class may only once be initialized. how does i do this?

Answers (5)

0
Photo of bilnaad
NA 686 0 20y
the static dataSet will be the same for every form. That's the fun part of static members. The static member doesn't dependon an instance of that class. It can be called directly. Just try it and you will see.
0
Photo of lemoinek
NA 19 0 20y
Quote: "You could write in any Form object: OneInstance.Data = new DataSet(); Wich will provide the class a new DataSet. And as you see you don't even need a instance of the object. " then you make on each form a new dataset? because i want to use 1 dataset that stays te same for each form
0
Photo of bilnaad
NA 686 0 20y
You can create a class with all static members. The internal variables are private static and functions and properties are public static. Even the constructor is static. Working with a static constructor has one disadvantage. It can't take any parameters. Because everything is static it doesn't hold any reference to the class it's in. But they are accessible from everywhere. You could write in any Form object: OneInstance.Data = new DataSet(); Wich will provide the class a new DataSet. And as you see you don't even need a instance of the object. public class OneInstance { static DataSet ds = null; public static OneInstance() { //TODO: constructor logic } public static DataSet Data { get{return ds;} set{ds = value;} } } I hope this helps.
0
Photo of lemoinek
NA 19 0 20y
the class keeps a dataset that he loads on the initialization
0
Photo of bilnaad
NA 686 0 20y
What's the functionality of that class? Else you could simple use a class with only static members.