473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

global class (simple question!)

ma
Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it using
VC2005)
3 - in global class generated by VC2005, I introduced test class as follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 'System.Web.HttpApplicationState' does not contain a definition for
'myclass'

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method, or
property 'Global.myclass'

What is wrong wioth my code?

Regards


Aug 11 '07 #1
10 3545
You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...
Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 'System.Web.HttpApplicationState' does not contain a definition
for 'myclass'

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property 'Global.myclass'

What is wrong wioth my code?

Regards


Aug 11 '07 #2
ma
Thanks, but I don 't want that my variable be static. It is an example but
in realworld I want a variable which is not static. What should I do?

Regards

"John Mott" <jo********@hotmail.comwrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...
You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...
>Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 'System.Web.HttpApplicationState' does not contain a definition
for 'myclass'

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property 'Global.myclass'

What is wrong wioth my code?

Regards



Aug 11 '07 #3
If you're trying to create global variables you sorta do want them static.
You can have something thats modifiable, not just a constant.

static string _mystring = "";

public static string mystring {
get { return _mystring; }
set { _mystring = value;}
}

The other option is to create an instance of the global class

public class Global {
public string mystring = "a default value";
};

public Global GlobalInstance = new Global();

and then you can say

GlobalInstance.mystring = "a new value";
John
nice clean examples at www.nicecleanexamples.com


"ma" <ma@nowhere.comwrote in message
news:OF**************@TK2MSFTNGP04.phx.gbl...
Thanks, but I don 't want that my variable be static. It is an example but
in realworld I want a variable which is not static. What should I do?

Regards

"John Mott" <jo********@hotmail.comwrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...
>You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...
>>Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 'System.Web.HttpApplicationState' does not contain a definition
for 'myclass'

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property 'Global.myclass'

What is wrong wioth my code?

Regards




Aug 11 '07 #4
ma
Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring);

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World" and
the next time that I download the page ( or refresh it) it should show "new
string" but it always show "hello world"

Any suggestion?

Regards


"John Mott" <jo********@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
If you're trying to create global variables you sorta do want them static.
You can have something thats modifiable, not just a constant.

static string _mystring = "";

public static string mystring {
get { return _mystring; }
set { _mystring = value;}
}

The other option is to create an instance of the global class

public class Global {
public string mystring = "a default value";
};

public Global GlobalInstance = new Global();

and then you can say

GlobalInstance.mystring = "a new value";
John
nice clean examples at www.nicecleanexamples.com


"ma" <ma@nowhere.comwrote in message
news:OF**************@TK2MSFTNGP04.phx.gbl...
>Thanks, but I don 't want that my variable be static. It is an example
but in realworld I want a variable which is not static. What should I do?

Regards

"John Mott" <jo********@hotmail.comwrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...
>>You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...
Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 'System.Web.HttpApplicationState' does not contain a definition
for 'myclass'

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field,
method, or property 'Global.myclass'

What is wrong wioth my code?

Regards






Aug 11 '07 #5

"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring);

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World" and
the next time that I download the page ( or refresh it) it should show
"new string" but it always show "hello world"

Any suggestion?

Regards
Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com
Aug 11 '07 #6
ma
Thanks.
Is there any other way to instantiate an object with application scope?
Regards

"John Mott" <jo********@hotmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...
>
"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
>Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring) ;

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World" and
the next time that I download the page ( or refresh it) it should show
"new string" but it always show "hello world"

Any suggestion?

Regards

Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com

Aug 11 '07 #7
You can create a static member of the Application class (a static member can
be an object that can be modified, it doesn't have to be read-only). That
was my first idea but you didn't want a static variable. You may have
thought from that context that static meant 'readonly', it doesn't.

Does that help?

here's a link as well:

http://support.microsoft.com/default...;en-us;Q312607

john
"ma" <ma@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Thanks.
Is there any other way to instantiate an object with application scope?
Regards

"John Mott" <jo********@hotmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...
>>
"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
>>Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring );

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World"
and the next time that I download the page ( or refresh it) it should
show "new string" but it always show "hello world"

Any suggestion?

Regards

Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com


Aug 11 '07 #8
Hi there,

Usually, when the class cannot be static and you require one global instance
of a class, singleton pattern should be used (this is thread safe variant):
public class Global
{
private Global()
{
}

private static object sync = new object();
private static Global instance = null;

public Global Instance
{
get
{
lock(sync)
{
if (instance == null)
{
instance = new Global();
}
}
return instance;
}
}
}

// usage
Global global = Global.Instance;

In addition, in ASP.NET there's build-in mechanism for such scenarios called
Application state (instance can be initialized in the Global.asax
Application_Start event) or Caching (you'd have to make sure race condition
is eliminated).

HTH
--
Milosz
"ma" wrote:
Thanks.
Is there any other way to instantiate an object with application scope?
Regards

"John Mott" <jo********@hotmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...

"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring);

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World" and
the next time that I download the page ( or refresh it) it should show
"new string" but it always show "hello world"

Any suggestion?

Regards
Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com


Aug 11 '07 #9
ma
Thanks,
Where can I read more about application instance or caching? Any good
tutorial on the web?

Regards

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:00**********************************@microsof t.com...
Hi there,

Usually, when the class cannot be static and you require one global
instance
of a class, singleton pattern should be used (this is thread safe
variant):
public class Global
{
private Global()
{
}

private static object sync = new object();
private static Global instance = null;

public Global Instance
{
get
{
lock(sync)
{
if (instance == null)
{
instance = new Global();
}
}
return instance;
}
}
}

// usage
Global global = Global.Instance;

In addition, in ASP.NET there's build-in mechanism for such scenarios
called
Application state (instance can be initialized in the Global.asax
Application_Start event) or Caching (you'd have to make sure race
condition
is eliminated).

HTH
--
Milosz
"ma" wrote:
>Thanks.
Is there any other way to instantiate an object with application scope?
Regards

"John Mott" <jo********@hotmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...
>
"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring) ;

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World"
and
the next time that I download the page ( or refresh it) it should show
"new string" but it always show "hello world"

Any suggestion?

Regards
Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com



Aug 12 '07 #10
Hi,

There are many tutorials out there, ie:
http://samples.gotdotnet.com/quickst...eoverview.aspx
Google is your friend

Regards
--
Milosz
"ma" wrote:
Thanks,
Where can I read more about application instance or caching? Any good
tutorial on the web?

Regards

"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:00**********************************@microsof t.com...
Hi there,

Usually, when the class cannot be static and you require one global
instance
of a class, singleton pattern should be used (this is thread safe
variant):
public class Global
{
private Global()
{
}

private static object sync = new object();
private static Global instance = null;

public Global Instance
{
get
{
lock(sync)
{
if (instance == null)
{
instance = new Global();
}
}
return instance;
}
}
}

// usage
Global global = Global.Instance;

In addition, in ASP.NET there's build-in mechanism for such scenarios
called
Application state (instance can be initialized in the Global.asax
Application_Start event) or Caching (you'd have to make sure race
condition
is eliminated).

HTH
--
Milosz
"ma" wrote:
Thanks.
Is there any other way to instantiate an object with application scope?
Regards

"John Mott" <jo********@hotmail.comwrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...

"ma" <ma@nowhere.comwrote in message
news:Oo**************@TK2MSFTNGP06.phx.gbl...
Thanks John, But it doesn't work!

I did this in the page load event:

protected void Page_Load(object sender, EventArgs e)

{

Global GlobalInstance=new Global();

Response.Write(GlobalInstance.myclass.mystring);

GlobalInstance.myclass.mystring = "new string";

}

so the first time that I load this page, it should show "Hello World"
and
the next time that I download the page ( or refresh it) it should show
"new string" but it always show "hello world"

Any suggestion?

Regards


Make the Global class itself static, like this

public static class Global {
public static string myString = "default";
}

Then you should be able to just refer to it without creating it with

Global.myString = "set me";

john

nice clean examples at www.nicecleanexamples.com



Aug 12 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
1
by: Chris | last post by:
Sorry to ask such a simple question but here it is, and I'm am new to ASP/WEB I am designing a site and I want to make it general so I can easily change the font/color/sizes of the...
2
by: Ricardo Corsi | last post by:
hi, I build one global class to store user data wich is logged in that moment. Every thing is work fine , and this class is accessed by all other pages from my project. But, the problem is:...
1
by: Ron | last post by:
Hello, I need to create/instantiate a global class library project so that 2 EXE's can write to the same class library form in the same instance of the class libary. I am thinking something...
2
by: Adrian | last post by:
Hi sorry for such a simple question! I want to be able to create a structure or is call a class in VB.net? eg test1 I want it to have a number of elements that I refer to as test1.username,...
3
by: Adrian | last post by:
Hi sorry for such a simple question! I want to be able to create a structure or is call a class? eg test1 I want it to have a number of elements that I refer to as test1.username,...
1
by: paulo | last post by:
Hello, I have a DLL library containing some web services which are declared in each .asmx file in the following way: <%@ WebService Language="C#" Class="LibraryName.WebService" %> I would...
8
by: Dom | last post by:
Can I have a consensus. Is it common to start every variable, control, etc, with "this"? I tend to do it, because I like the Intellisense window. But is it considered amaturish? Told you it...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.