Home All Groups Group Topic Archive Search About

how do you use Class to hold global variables?

Author
17 Oct 2006 5:06 AM
dotnetnoob
in the old VB, you can use global variables to hold commonly use data. i'll
like to pass a variables selected by user in the combobox, how do you hold
this variable for other object to use. does anyone have an example?

Author
17 Oct 2006 5:19 AM
Steven Nagy
You can use a static property.

For example:

public class Global {
   private Global() {}

   public static string UserName;

}
Author
17 Oct 2006 5:37 AM
Steven Nagy
Steven Nagy wrote:
> You can use a static property.
>
> For example:
>
> public class Global {
>    private Global() {}
>
>    public static string UserName;
>
> }

Sorry I thought I was in the C# group.
Here's the VB code:

public class Global
    public shared string UserName
end class
Author
17 Oct 2006 5:22 AM
Cyril Gupta
Hello dotnetnoob,

You can create a module, and declare Global variables in the module just
the way you did in the old VB. Classes are not actually meant as holders
of global variables.

Just add a module to your project and declare a variable like this

Public myVariable as Integer

Regards
Cyril Gupta

You can do anything with a little bit of 'magination.
I have been programming so long, that my brain is now soft-ware.
http://www.cyrilgupta.com/blog

Show quoteHide quote
> in the old VB, you can use global variables to hold commonly use data.
> i'll like to pass a variables selected by user in the combobox, how do
> you hold this variable for other object to use. does anyone have an
> example?
>
Author
17 Oct 2006 5:31 AM
dotnetnoob
thank all for reply.

how do you pass commonly use variable in C# then?

so i can add a module in my program to hold the data selected by user in
combobox and use it for other object to access, am i correct?

Show quoteHide quote
"Cyril Gupta" wrote:

> Hello dotnetnoob,
>
> You can create a module, and declare Global variables in the module just
> the way you did in the old VB. Classes are not actually meant as holders
> of global variables.
>
> Just add a module to your project and declare a variable like this
>
> Public myVariable as Integer
>
> Regards
> Cyril Gupta
>
> You can do anything with a little bit of 'magination.
> I have been programming so long, that my brain is now soft-ware.
> http://www.cyrilgupta.com/blog
>
> > in the old VB, you can use global variables to hold commonly use data.
> > i'll like to pass a variables selected by user in the combobox, how do
> > you hold this variable for other object to use. does anyone have an
> > example?
> >
>
>
>
Author
17 Oct 2006 2:37 PM
NickP
Hi there,

    Sorry but that is just not true.  You can object orientate anything.  In
fact I'd rather do that than use modules any day.

    In my honest opinion a shared class is much better than a module,

    -------------------------

    Public Class GlobalObjects

        '//Hide the constructor as it is not needed
        Private Sub New()
        End Sub

        Public Shared gBlnDebugMode As Boolean

    End Class

    -------------------------

Nick.

Show quoteHide quote
"Cyril Gupta" <cyril@nospam.com> wrote in message
news:a95a8c81bae98c8bff169eff700@msnews.microsoft.com...
> Hello dotnetnoob,
>
> You can create a module, and declare Global variables in the module just
> the way you did in the old VB. Classes are not actually meant as holders
> of global variables.
>
> Just add a module to your project and declare a variable like this
>
> Public myVariable as Integer
>
> Regards
> Cyril Gupta
>
> You can do anything with a little bit of 'magination.
> I have been programming so long, that my brain is now soft-ware.
> http://www.cyrilgupta.com/blog
>
>> in the old VB, you can use global variables to hold commonly use data.
>> i'll like to pass a variables selected by user in the combobox, how do
>> you hold this variable for other object to use. does anyone have an
>> example?
>>
>
>
Author
17 Oct 2006 3:54 PM
rowe_newsgroups
Just as an addition: Depending on how you plan to use the variable, you
may want to consider wrapping the variable in a public shared property.
This comes in handy for things like validating entries, error trapping,
etc...

Thanks,

Seth Rowe


NickP wrote:
Show quoteHide quote
> Hi there,
>
>     Sorry but that is just not true.  You can object orientate anything.  In
> fact I'd rather do that than use modules any day.
>
>     In my honest opinion a shared class is much better than a module,
>
>     -------------------------
>
>     Public Class GlobalObjects
>
>         '//Hide the constructor as it is not needed
>         Private Sub New()
>         End Sub
>
>         Public Shared gBlnDebugMode As Boolean
>
>     End Class
>
>     -------------------------
>
> Nick.
>
> "Cyril Gupta" <cyril@nospam.com> wrote in message
> news:a95a8c81bae98c8bff169eff700@msnews.microsoft.com...
> > Hello dotnetnoob,
> >
> > You can create a module, and declare Global variables in the module just
> > the way you did in the old VB. Classes are not actually meant as holders
> > of global variables.
> >
> > Just add a module to your project and declare a variable like this
> >
> > Public myVariable as Integer
> >
> > Regards
> > Cyril Gupta
> >
> > You can do anything with a little bit of 'magination.
> > I have been programming so long, that my brain is now soft-ware.
> > http://www.cyrilgupta.com/blog
> >
> >> in the old VB, you can use global variables to hold commonly use data.
> >> i'll like to pass a variables selected by user in the combobox, how do
> >> you hold this variable for other object to use. does anyone have an
> >> example?
> >>
> >
> >
Author
17 Oct 2006 3:57 PM
NickP
Exactly, much more friendly than a simple module...

Show quoteHide quote
"rowe_newsgroups" <rowe_em***@yahoo.com> wrote in message
news:1161100463.848443.200570@e3g2000cwe.googlegroups.com...
> Just as an addition: Depending on how you plan to use the variable, you
> may want to consider wrapping the variable in a public shared property.
> This comes in handy for things like validating entries, error trapping,
> etc...
>
> Thanks,
>
> Seth Rowe
>
>
> NickP wrote:
>> Hi there,
>>
>>     Sorry but that is just not true.  You can object orientate anything.
>> In
>> fact I'd rather do that than use modules any day.
>>
>>     In my honest opinion a shared class is much better than a module,
>>
>>     -------------------------
>>
>>     Public Class GlobalObjects
>>
>>         '//Hide the constructor as it is not needed
>>         Private Sub New()
>>         End Sub
>>
>>         Public Shared gBlnDebugMode As Boolean
>>
>>     End Class
>>
>>     -------------------------
>>
>> Nick.
>>
>> "Cyril Gupta" <cyril@nospam.com> wrote in message
>> news:a95a8c81bae98c8bff169eff700@msnews.microsoft.com...
>> > Hello dotnetnoob,
>> >
>> > You can create a module, and declare Global variables in the module
>> > just
>> > the way you did in the old VB. Classes are not actually meant as
>> > holders
>> > of global variables.
>> >
>> > Just add a module to your project and declare a variable like this
>> >
>> > Public myVariable as Integer
>> >
>> > Regards
>> > Cyril Gupta
>> >
>> > You can do anything with a little bit of 'magination.
>> > I have been programming so long, that my brain is now soft-ware.
>> > http://www.cyrilgupta.com/blog
>> >
>> >> in the old VB, you can use global variables to hold commonly use data.
>> >> i'll like to pass a variables selected by user in the combobox, how do
>> >> you hold this variable for other object to use. does anyone have an
>> >> example?
>> >>
>> >
>> >
>
Author
17 Oct 2006 10:48 PM
Steven Nagy
I've always hated modules personally... no real scope and no
encapsulation.
Author
17 Oct 2006 11:28 PM
Dennis
Funny, I always thought modules were in fact Classes with all the variables
and procedures Shared!

Can you explain the difference?

--
Dennis in Houston


Show quoteHide quote
"Steven Nagy" wrote:

> I've always hated modules personally... no real scope and no
> encapsulation.
>
>
Author
18 Oct 2006 12:16 AM
rowe_newsgroups
While I could get out the old soapbox, I think I'll just advise readers
to search the ng for this heavily debated topic! But if you don't want
to search too much then the main differences I know of are that modules
are given an automatic project wide import statement (kinda), and can't
be instantiated.

Thanks,

Seth Rowe


Dennis wrote:
Show quoteHide quote
> Funny, I always thought modules were in fact Classes with all the variables
> and procedures Shared!
>
> Can you explain the difference?
>
> --
> Dennis in Houston
>
>
> "Steven Nagy" wrote:
>
> > I've always hated modules personally... no real scope and no
> > encapsulation.
> >
> >
Author
18 Oct 2006 12:32 AM
Steven Nagy
I'm not sure how they run under the hood. Chances are that they are
just a class.

> the main differences I know of are that modules
> are given an automatic project wide import statement (kinda),

... which means that you can't control scope. So its better to use a
class and implement it in a way that scope is important. Modules are
often misused. People stick everything in them. I've seen forms
declared in there, database connections, etc.

Implement a class instead. Control access as necessary. Implement a
singleton where relevant. There's always a better way to go.