where do I put global data?

G

Guest

I'm working on my first C# .NET app for PPC, coming from Palm development
with c++. So far I've got a simple app with a couple forms. I want to store
data in a class I've created which is accessible from all the different
forms. In C++ I did that with global variables. How do I do that in C#.NET?
 
I

Ilya Tumanov [MS]

It's not recommended, but you can do it like this:

public class MyGlobalData {
public static int MyGlobalInt1 = 10;
public static string MyGlobalString1 = "SomeString";
}



MyGlobalData.MyGlobalInt1 = 100;

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
G

Guest

I wish you could edit posts here, since the more I thought about this, I
would have re-phrased it.

I assume you can simply set public variables for a form, and access them
that way. So I think I'm confused by how forms are managed, and therefore
how a form would access public data from another form. I guess I could
simply pass the data I want to access to each form as it is displayed, but
that seems kludgy.
 
G

Guest

A form is just a class so you should be able to access it from other classes
either through properties or public variables within the class. You can also
create a data access class that manages the data and that all forms will have
access to.
 
D

Darren Shaffer

I encourage CF developers to create re-usable singleton classes that
encapsulate logic
and state that is typically required across many forms in a Windows Mobile
applications. Most of my CF apps
contain a Globals singleton, a DatabaseManager singleton, a MapPointWS
singleton, a SynchManager
singleton, a Scanner singleton, etc that allow me to quickly add
functionality to my CF projects that can be
leveraged from all forms and other classes in the app.

Regardless of what form you're on, you can then write code like this:

string sWorker = Globals.GetInstance().WorkerID;

The reason Ilya discouraged your initial idea is that good object-oriented
software engineering respects
the concepts of encapsulation and information hiding. You should only place
those members in the Globals
class that are *truly* global (i.e. needed across multiple forms in your
application or across the lifetime
of the execution of your app). Everything else should be contained in the
appropriate scope
and made private. I'd add that I've seen many CF apps where there is SQL
CE, MapPoint, and
Symbol Scanner code littered across every form in the app, much off it
copied and pasted. It is much cleaner
to expose interfaces to these "common components of mobility" in a single,
reusable class. And I've found
no better way to do that in the Compact Framework than the singleton
pattern.

If you are unfamiliar with the singleton OO design pattern, here's how to
implement it:

public class Globals
{
private static Globals instance;

// note that the members below are truly global across the app
private int _workerID;
private int _eventId;
private int _leadsUploaded = 0;
private int _leadsEntered = 0;
private bool _isInLeadEditMode;
private string _password;
private string _userID;
private string _eventName;
private string _lastSynch;

public Globals()
{
}

// singleton pattern
public static Globals GetInstance()
{
if ( instance == null )
{
instance = new Globals();
}
return instance;
}

--Darren Shaffer
Principal Architect
Connected Innovation
www.connectedinnovation.com
 
C

Chris Tacke, eMVP

I'll bite - why's this not recommended? I do it in almost every app I write
to control app entry, singleton instances, etc.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


"Ilya Tumanov [MS]" said:
It's not recommended, but you can do it like this:

public class MyGlobalData {
public static int MyGlobalInt1 = 10;
public static string MyGlobalString1 = "SomeString";
}

.

MyGlobalData.MyGlobalInt1 = 100;

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
Thread-Topic: where do I put global data?
thread-index: AcT477IVkkulTkb+TKqIe30dtxFV4A==
X-WBNR-Posting-Host: 143.115.159.56
From: "=?Utf-8?B?YnJpZm9yZ2U=?=" <[email protected]>
Subject: where do I put global data?
Date: Wed, 12 Jan 2005 13:43:04 -0800
Lines: 4
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:68517
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I'm working on my first C# .NET app for PPC, coming from Palm development
with c++. So far I've got a simple app with a couple forms. I want to store
data in a class I've created which is accessible from all the different
forms. In C++ I did that with global variables. How do I do that in C#.NET?
 
I

Ilya Tumanov [MS]

Let me rephrase: it should not be abused. Would that be satisfactory? :)

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

PS Yes, 'goto' is also fine in some cases though I did not hit such case in
like 5 years.

--------------------
From: "Chris Tacke, eMVP" <[email protected]>
References: <[email protected]>
Subject: Re: where do I put global data?
Date: Wed, 12 Jan 2005 17:34:46 -0500
Lines: 66
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <eyFhsbP#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: nat3.applieddata.net 64.72.200.62
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXS01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGXA0
1.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:68524
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I'll bite - why's this not recommended? I do it in almost every app I write
to control app entry, singleton instances, etc.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


"Ilya Tumanov [MS]" said:
It's not recommended, but you can do it like this:

public class MyGlobalData {
public static int MyGlobalInt1 = 10;
public static string MyGlobalString1 = "SomeString";
}

.

MyGlobalData.MyGlobalInt1 = 100;

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
Thread-Topic: where do I put global data?
thread-index: AcT477IVkkulTkb+TKqIe30dtxFV4A==
X-WBNR-Posting-Host: 143.115.159.56
From: "=?Utf-8?B?YnJpZm9yZ2U=?=" <[email protected]>
Subject: where do I put global data?
Date: Wed, 12 Jan 2005 13:43:04 -0800
Lines: 4
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:68517
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I'm working on my first C# .NET app for PPC, coming from Palm development
with c++. So far I've got a simple app with a couple forms. I want to store
data in a class I've created which is accessible from all the different
forms. In C++ I did that with global variables. How do I do that in C#.NET?
 
C

Chris Tacke, eMVP

Sure, I was just worried for a minute that there was some pattern or
practice that did it better. :)

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


"Ilya Tumanov [MS]" said:
Let me rephrase: it should not be abused. Would that be satisfactory? :)

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

PS Yes, 'goto' is also fine in some cases though I did not hit such case
in
like 5 years.

--------------------
From: "Chris Tacke, eMVP" <[email protected]>
References: <[email protected]>
Subject: Re: where do I put global data?
Date: Wed, 12 Jan 2005 17:34:46 -0500
Lines: 66
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <eyFhsbP#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: nat3.applieddata.net 64.72.200.62
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXS01.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGXA0
1.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.compactframework:68524
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I'll bite - why's this not recommended? I do it in almost every app I write
to control app entry, singleton instances, etc.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


"Ilya Tumanov [MS]" said:
It's not recommended, but you can do it like this:

public class MyGlobalData {
public static int MyGlobalInt1 = 10;
public static string MyGlobalString1 = "SomeString";
}

.

MyGlobalData.MyGlobalInt1 = 100;

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
Thread-Topic: where do I put global data?
thread-index: AcT477IVkkulTkb+TKqIe30dtxFV4A==
X-WBNR-Posting-Host: 143.115.159.56
From: "=?Utf-8?B?YnJpZm9yZ2U=?=" <[email protected]>
Subject: where do I put global data?
Date: Wed, 12 Jan 2005 13:43:04 -0800
Lines: 4
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
Path:
cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA0
3.phx.gbl
Xref: cpmsftngxa10.phx.gbl
microsoft.public.dotnet.framework.compactframework:68517
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I'm working on my first C# .NET app for PPC, coming from Palm development
with c++. So far I've got a simple app with a couple forms. I want
to
store
data in a class I've created which is accessible from all the
different
forms. In C++ I did that with global variables. How do I do that in
C#.NET?
 
E

Ed Kaim

To build on this idea, you could architect your app into multiple tiers,
thereby avoiding the need to store (and retrieve) data from the presentation
layer (where your forms would be). All data would be stored in a data tier
and each form would store and retrieve its data from there. With VS it's
very easy to do so by partitioning across separate projects.
 
G

Guest

But if you have a global class with static properties and methods to access
information then those methods (and a static constructor) are inherently
singletons and you never have to have an instance of the class. In what
ways is the Singleton pattern better for this (I really want to know, that's
not sarcasm)?

-Chris
 
J

Jan Yeh_eMVP

Hi, Chris

As Darren said, I used AppManager for global things, and DbManager for
database stuffs
in Singleton pattern. However, I don't use that much classes as Darren
uses...

IMK, the singleton instance will be created in heap memory, and static class
will
be put in stack. In some extreme situations, the stack could be full... and
cause
a StackOverflowException. I met that once or twice before...
Maybe there are some other good reasons for using Singleton , but this is
the first one
came into my mind.

And I use singleton as a property, instead of a method... so I don't have to
type brackets.
Just use AppManager.Singleton.xxx to get the global stuff.

public class AppManager
{
private static AppManager uniqueInstance = null;
private AppManager() {} // Hide the constructor
public static AppManager Singleton
{
get
{
if( null == uniqueInstance ) uniqueInstance = new AppManager();
return uniqueInstance;
}
}
}

--

Best Regards,

Jan Yeh
MVP - Windows CE.NET, MCSD.NET, .NETcf consultant
Web - http://www.mobilemind.com.tw , Blog - http://blog.mvpcn.net/janyeh
 
D

Daniel Moth

Also Jon Skeet has a good article here:
http://www.yoda.arachsys.com/csharp/singleton.html

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Sergey Bogdanov said:
Have to notice that to use singleton as a property it's Microsoft
Singleton recomendation which is written here [1]. This article covers
common Singleton, Multithreaded Singleton, Static Initialization
Singleton.

[1]
http://msdn.microsoft.com/library/d...n-us/dnpatterns/html/ImpSingletonInCsharp.asp

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
Hi, Chris

As Darren said, I used AppManager for global things, and DbManager for
database stuffs
in Singleton pattern. However, I don't use that much classes as Darren
uses...

IMK, the singleton instance will be created in heap memory, and static
class will
be put in stack. In some extreme situations, the stack could be full...
and cause
a StackOverflowException. I met that once or twice before...
Maybe there are some other good reasons for using Singleton , but this is
the first one
came into my mind.

And I use singleton as a property, instead of a method... so I don't have
to type brackets.
Just use AppManager.Singleton.xxx to get the global stuff.

public class AppManager
{
private static AppManager uniqueInstance = null;
private AppManager() {} // Hide the constructor
public static AppManager Singleton
{
get
{
if( null == uniqueInstance ) uniqueInstance = new
AppManager();
return uniqueInstance;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top