Connecting Tech Pros Worldwide Forums | Help | Site Map

How to access this static variable

LamSoft
Guest
 
Posts: n/a
#1: Jul 18 '07
Class B { public B() {} }


Class A : B {
public static string ABC = "myABC";
public A() {}
}



main Program:

B myObject = new A();


and now is it possible to access "ABC" through "myObject" without modifying
the source code in Class A and Class B.

Thanks



Hans Kesting
Guest
 
Posts: n/a
#2: Jul 18 '07

re: How to access this static variable


Class B { public B() {} }
Quote:
>
Class A : B {
public static string ABC = "myABC";
public A() {}
}
main Program:
>
B myObject = new A();
>
and now is it possible to access "ABC" through "myObject" without
modifying the source code in Class A and Class B.
>
Thanks
>

As it's a static variable (in type A), you can only access it through the
Type A:
A.ABC, not through an instance.

Type B doesn't know anything about things declared in derived types (like A),
so your myObject (which is "just a B") doesn't know anything about that ABC.

*if* B declares a (virtual) method:
public virtual string MyMethod() { return "nil"; }
and A overrides it:
public override string MyMethod() { return A.ABC; }
then your myObject can still access the contents of ABC, using this MyMethod()

Hans Kesting


LamSoft
Guest
 
Posts: n/a
#3: Jul 18 '07

re: How to access this static variable


But there are not only 1 static variable in Class A, .... i don't wanna make
every function for each static variable





"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
news:c04e80a71b5bb8c9975e161250ae@news.microsoft.c om...
Quote:
Quote:
>Class B { public B() {} }
>>
>Class A : B {
>public static string ABC = "myABC";
>public A() {}
>}
>main Program:
>>
>B myObject = new A();
>>
>and now is it possible to access "ABC" through "myObject" without
>modifying the source code in Class A and Class B.
>>
>Thanks
>>
>
>
As it's a static variable (in type A), you can only access it through the
Type A:
A.ABC, not through an instance.
>
Type B doesn't know anything about things declared in derived types (like
A),
so your myObject (which is "just a B") doesn't know anything about that
ABC.
>
*if* B declares a (virtual) method:
public virtual string MyMethod() { return "nil"; }
and A overrides it:
public override string MyMethod() { return A.ABC; }
then your myObject can still access the contents of ABC, using this
MyMethod()
>
Hans Kesting
>
>

Patrice
Guest
 
Posts: n/a
#4: Jul 18 '07

re: How to access this static variable


You may want to explain what you are trying to do so that we can understand
why not using A.ABC is not something that fit your needs and why accessing
static members through an instance member would make sense. It could raise
alternate approach to the problem you are trying to solve..

---
Patrice


"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
eFWxziTyHHA.1100@TK2MSFTNGP06.phx.gbl...
Quote:
But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable
>
>
>
>
>
"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
news:c04e80a71b5bb8c9975e161250ae@news.microsoft.c om...
Quote:
Quote:
>>Class B { public B() {} }
>>>
>>Class A : B {
>>public static string ABC = "myABC";
>>public A() {}
>>}
>>main Program:
>>>
>>B myObject = new A();
>>>
>>and now is it possible to access "ABC" through "myObject" without
>>modifying the source code in Class A and Class B.
>>>
>>Thanks
>>>
>>
>>
>As it's a static variable (in type A), you can only access it through the
>Type A:
>A.ABC, not through an instance.
>>
>Type B doesn't know anything about things declared in derived types (like
>A),
>so your myObject (which is "just a B") doesn't know anything about that
>ABC.
>>
>*if* B declares a (virtual) method:
> public virtual string MyMethod() { return "nil"; }
>and A overrides it:
> public override string MyMethod() { return A.ABC; }
>then your myObject can still access the contents of ABC, using this
>MyMethod()
>>
>Hans Kesting
>>
>>
>
>

LamSoft
Guest
 
Posts: n/a
#5: Jul 18 '07

re: How to access this static variable


I am trying to make a language file and put all Static variables into each
different cs.
For example:


Class english : language{
public static string TEST = "test";
}

Class chinese : language {
public static string TEST = "代刚";
}

Class language {}




and now i wanna access the TEST variable, but according to which language is
being selected ..

and I expected the following and something like that...

Language myLanguage = new english();
myLanguage.TEST // output will be "test"

Language myLanguage = new chinese();
myLanguage.TEST // output will be "代刚"


Thank you very much.



"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ohwcf%23TyHHA.5484@TK2MSFTNGP03.phx.gbl...
Quote:
You may want to explain what you are trying to do so that we can
understand why not using A.ABC is not something that fit your needs and
why accessing static members through an instance member would make sense.
It could raise alternate approach to the problem you are trying to solve..
>
---
Patrice
>
>
"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
eFWxziTyHHA.1100@TK2MSFTNGP06.phx.gbl...
Quote:
>But there are not only 1 static variable in Class A, .... i don't wanna
>make every function for each static variable
>>
>>
>>
>>
>>
>"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
>news:c04e80a71b5bb8c9975e161250ae@news.microsoft. com...
Quote:
>>>Class B { public B() {} }
>>>>
>>>Class A : B {
>>>public static string ABC = "myABC";
>>>public A() {}
>>>}
>>>main Program:
>>>>
>>>B myObject = new A();
>>>>
>>>and now is it possible to access "ABC" through "myObject" without
>>>modifying the source code in Class A and Class B.
>>>>
>>>Thanks
>>>>
>>>
>>>
>>As it's a static variable (in type A), you can only access it through
>>the Type A:
>>A.ABC, not through an instance.
>>>
>>Type B doesn't know anything about things declared in derived types
>>(like A),
>>so your myObject (which is "just a B") doesn't know anything about that
>>ABC.
>>>
>>*if* B declares a (virtual) method:
>> public virtual string MyMethod() { return "nil"; }
>>and A overrides it:
>> public override string MyMethod() { return A.ABC; }
>>then your myObject can still access the contents of ABC, using this
>>MyMethod()
>>>
>>Hans Kesting
>>>
>>>
>>
>>
>
>

Masudur
Guest
 
Posts: n/a
#6: Jul 19 '07

re: How to access this static variable


On Jul 18, 6:21 pm, Hans Kesting <news.2.han...@spamgourmet.com>
wrote:
Quote:
Quote:
Class B { public B() {} }
>
Quote:
Class A : B {
public static string ABC = "myABC";
public A() {}
}
main Program:
>
Quote:
B myObject = new A();
>
Quote:
and now is it possible to access "ABC" through "myObject" without
modifying the source code in Class A and Class B.
>
Quote:
Thanks
>
As it's a static variable (in type A), you can only access it through the
Type A:
A.ABC, not through an instance.
>
Type B doesn't know anything about things declared in derived types (like A),
so your myObject (which is "just a B") doesn't know anything about that ABC.
>
*if* B declares a (virtual) method:
public virtual string MyMethod() { return "nil"; }
and A overrides it:
public override string MyMethod() { return A.ABC; }
then your myObject can still access the contents of ABC, using this MyMethod()
>
Hans Kesting
Hi...

What is the utility of access a variable via a method when you have
direct access to that variable...
but still Hans provided a very good way to access static variable
through a method...

Masudur

Patrice
Guest
 
Posts: n/a
#7: Jul 19 '07

re: How to access this static variable


First, have you looked at localization capabilities offered by the.NET
framework ?

If you want to access those members through an instance variable, why making
them static ? (I'm not sure but I see nowhere that you would really declare
them static on purpose ?).
Another option is to make them static but to have a single class and provide
the language they should provide. Remember that being a static member you
can stuff whatever you want (ie. your labels could come from any data
structure filled with strings for each supported language).

Or drop the static keyword and have your classes inherits and overrides your
default language class (could be abstract or an interface to make sure you
provide the appropriate text for each class).

For now it llklks like the code would direct to choose a design while you
choosed anothe design for the declarations. You have to match both aspects
(and have a llok at localization features if not already done).

--
Patrice

"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
uefBMnUyHHA.4824@TK2MSFTNGP02.phx.gbl...
Quote:
>I am trying to make a language file and put all Static variables into each
>different cs.
For example:
>
>
Class english : language{
public static string TEST = "test";
}
>
Class chinese : language {
public static string TEST = "代刚";
}
>
Class language {}
>
>
>
>
and now i wanna access the TEST variable, but according to which language
is being selected ..
>
and I expected the following and something like that...
>
Language myLanguage = new english();
myLanguage.TEST // output will be "test"
>
Language myLanguage = new chinese();
myLanguage.TEST // output will be "代刚"
>
>
Thank you very much.
>
>
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ohwcf%23TyHHA.5484@TK2MSFTNGP03.phx.gbl...
Quote:
>You may want to explain what you are trying to do so that we can
>understand why not using A.ABC is not something that fit your needs and
>why accessing static members through an instance member would make sense.
>It could raise alternate approach to the problem you are trying to
>solve..
>>
>---
>Patrice
>>
>>
>"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
>eFWxziTyHHA.1100@TK2MSFTNGP06.phx.gbl...
Quote:
>>But there are not only 1 static variable in Class A, .... i don't wanna
>>make every function for each static variable
>>>
>>>
>>>
>>>
>>>
>>"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
>>news:c04e80a71b5bb8c9975e161250ae@news.microsoft .com...
>>>>Class B { public B() {} }
>>>>>
>>>>Class A : B {
>>>>public static string ABC = "myABC";
>>>>public A() {}
>>>>}
>>>>main Program:
>>>>>
>>>>B myObject = new A();
>>>>>
>>>>and now is it possible to access "ABC" through "myObject" without
>>>>modifying the source code in Class A and Class B.
>>>>>
>>>>Thanks
>>>>>
>>>>
>>>>
>>>As it's a static variable (in type A), you can only access it through
>>>the Type A:
>>>A.ABC, not through an instance.
>>>>
>>>Type B doesn't know anything about things declared in derived types
>>>(like A),
>>>so your myObject (which is "just a B") doesn't know anything about that
>>>ABC.
>>>>
>>>*if* B declares a (virtual) method:
>>> public virtual string MyMethod() { return "nil"; }
>>>and A overrides it:
>>> public override string MyMethod() { return A.ABC; }
>>>then your myObject can still access the contents of ABC, using this
>>>MyMethod()
>>>>
>>>Hans Kesting
>>>>
>>>>
>>>
>>>
>>
>>
>
>

Hans Kesting
Guest
 
Posts: n/a
#8: Jul 19 '07

re: How to access this static variable


On Jul 18, 6:21 pm, Hans Kesting <news.2.han...@spamgourmet.com>
Quote:
wrote:
>
Quote:
Quote:
>>Class B { public B() {} }
>>>
>>Class A : B {
>>public static string ABC = "myABC";
>>public A() {}
>>}
>>main Program:
>>B myObject = new A();
>>>
>>and now is it possible to access "ABC" through "myObject" without
>>modifying the source code in Class A and Class B.
>>>
>>Thanks
>>>
>As it's a static variable (in type A), you can only access it through
>the
>Type A:
>A.ABC, not through an instance.
>Type B doesn't know anything about things declared in derived types
>(like A), so your myObject (which is "just a B") doesn't know
>anything about that ABC.
>>
>*if* B declares a (virtual) method:
>public virtual string MyMethod() { return "nil"; }
>and A overrides it:
>public override string MyMethod() { return A.ABC; }
>then your myObject can still access the contents of ABC, using this
>MyMethod()
>Hans Kesting
>>
Hi...
>
What is the utility of access a variable via a method when you have
direct access to that variable...
but still Hans provided a very good way to access static variable
through a method...
Masudur
>

Not much use, granted. But if you really want to have a static variable
that you still want to be able to access through an instance of an inherited
class, you need tricks like these.

Hans Kesting


Brandon Gano
Guest
 
Posts: n/a
#9: Jul 19 '07

re: How to access this static variable


Static members aren't what you want here. Read-only properties sound like a
better approach. You could try something like this (not tested):

public interface ILanguage
{
string Test { get; }
}

public class English : ILanguage
{
public string Test
{
get { return "test"; }
}
}

public class French : ILanguage
{
public string Test
{
get { return "le test"; }
}
}

A different approach might be:

public class Localization
{
private Dictionary<string, stringStrings;

static Localization(string lang)
{
// Load appropriate data into this.Strings from XML, SQL, etc.
}
}

You could then provide an indexer to access the values as follows:

Localization loc = new Localization("en");
loc["test"] // output would be "test"


"LamSoft" <[nospam]lamsoft@lamsoft.netwrote in message
news:uefBMnUyHHA.4824@TK2MSFTNGP02.phx.gbl...
Quote:
>I am trying to make a language file and put all Static variables into each
>different cs.
For example:
>
>
Class english : language{
public static string TEST = "test";
}
>
Class chinese : language {
public static string TEST = "代刚";
}
>
Class language {}
>
>
>
>
and now i wanna access the TEST variable, but according to which language
is being selected ..
>
and I expected the following and something like that...
>
Language myLanguage = new english();
myLanguage.TEST // output will be "test"
>
Language myLanguage = new chinese();
myLanguage.TEST // output will be "代刚"
>
>
Thank you very much.
>
>
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ohwcf%23TyHHA.5484@TK2MSFTNGP03.phx.gbl...
Quote:
>You may want to explain what you are trying to do so that we can
>understand why not using A.ABC is not something that fit your needs and
>why accessing static members through an instance member would make sense.
>It could raise alternate approach to the problem you are trying to
>solve..
>>
>---
>Patrice
>>
>>
>"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
>eFWxziTyHHA.1100@TK2MSFTNGP06.phx.gbl...
Quote:
>>But there are not only 1 static variable in Class A, .... i don't wanna
>>make every function for each static variable
>>>
>>>
>>>
>>>
>>>
>>"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
>>news:c04e80a71b5bb8c9975e161250ae@news.microsoft .com...
>>>>Class B { public B() {} }
>>>>>
>>>>Class A : B {
>>>>public static string ABC = "myABC";
>>>>public A() {}
>>>>}
>>>>main Program:
>>>>>
>>>>B myObject = new A();
>>>>>
>>>>and now is it possible to access "ABC" through "myObject" without
>>>>modifying the source code in Class A and Class B.
>>>>>
>>>>Thanks
>>>>>
>>>>
>>>>
>>>As it's a static variable (in type A), you can only access it through
>>>the Type A:
>>>A.ABC, not through an instance.
>>>>
>>>Type B doesn't know anything about things declared in derived types
>>>(like A),
>>>so your myObject (which is "just a B") doesn't know anything about that
>>>ABC.
>>>>
>>>*if* B declares a (virtual) method:
>>> public virtual string MyMethod() { return "nil"; }
>>>and A overrides it:
>>> public override string MyMethod() { return A.ABC; }
>>>then your myObject can still access the contents of ABC, using this
>>>MyMethod()
>>>>
>>>Hans Kesting
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Brandon Gano
Guest
 
Posts: n/a
#10: Jul 19 '07

re: How to access this static variable


Static members aren't what you want here. Read-only properties sound like a
better approach. You could try something like this (not tested):

public interface ILanguage
{
string Test { get; }
}

public class English : ILanguage
{
public string Test
{
get { return "test"; }
}
}

public class French : ILanguage
{
public string Test
{
get { return "le test"; }
}
}

A different approach might be:

public class Localization
{
private Dictionary<string, stringStrings;

static Localization(string lang)
{
// Load appropriate data into this.Strings from XML, SQL, etc.
}
}

You could then provide an indexer to access the values as follows:

Localization loc = new Localization("en");
loc["test"] // output would be "test"


"LamSoft" <[nospam]lamsoft@lamsoft.netwrote in message
news:uefBMnUyHHA.4824@TK2MSFTNGP02.phx.gbl...
Quote:
>I am trying to make a language file and put all Static variables into each
>different cs.
For example:
>
>
Class english : language{
public static string TEST = "test";
}
>
Class chinese : language {
public static string TEST = "代刚";
}
>
Class language {}
>
>
>
>
and now i wanna access the TEST variable, but according to which language
is being selected ..
>
and I expected the following and something like that...
>
Language myLanguage = new english();
myLanguage.TEST // output will be "test"
>
Language myLanguage = new chinese();
myLanguage.TEST // output will be "代刚"
>
>
Thank you very much.
>
>
>
"Patrice" <http://www.chez.com/scribe/wrote in message
news:Ohwcf%23TyHHA.5484@TK2MSFTNGP03.phx.gbl...
Quote:
>You may want to explain what you are trying to do so that we can
>understand why not using A.ABC is not something that fit your needs and
>why accessing static members through an instance member would make sense.
>It could raise alternate approach to the problem you are trying to
>solve..
>>
>---
>Patrice
>>
>>
>"LamSoft" <[nospam]lamsoft@lamsoft.neta 閏rit dans le message de news:
>eFWxziTyHHA.1100@TK2MSFTNGP06.phx.gbl...
Quote:
>>But there are not only 1 static variable in Class A, .... i don't wanna
>>make every function for each static variable
>>>
>>>
>>>
>>>
>>>
>>"Hans Kesting" <news.2.hansdk@spamgourmet.comwrote in message
>>news:c04e80a71b5bb8c9975e161250ae@news.microsoft .com...
>>>>Class B { public B() {} }
>>>>>
>>>>Class A : B {
>>>>public static string ABC = "myABC";
>>>>public A() {}
>>>>}
>>>>main Program:
>>>>>
>>>>B myObject = new A();
>>>>>
>>>>and now is it possible to access "ABC" through "myObject" without
>>>>modifying the source code in Class A and Class B.
>>>>>
>>>>Thanks
>>>>>
>>>>
>>>>
>>>As it's a static variable (in type A), you can only access it through
>>>the Type A:
>>>A.ABC, not through an instance.
>>>>
>>>Type B doesn't know anything about things declared in derived types
>>>(like A),
>>>so your myObject (which is "just a B") doesn't know anything about that
>>>ABC.
>>>>
>>>*if* B declares a (virtual) method:
>>> public virtual string MyMethod() { return "nil"; }
>>>and A overrides it:
>>> public override string MyMethod() { return A.ABC; }
>>>then your myObject can still access the contents of ABC, using this
>>>MyMethod()
>>>>
>>>Hans Kesting
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Closed Thread