473,472 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access this static variable

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
Jul 18 '07 #1
9 2034
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
Jul 18 '07 #2
But there are not only 1 static variable in Class A, .... i don't wanna make
every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@news.microsoft.c om...
>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


Jul 18 '07 #3
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]la*****@lamsoft.neta écrit dans le message de news:
eF**************@TK2MSFTNGP06.phx.gbl...
But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@news.microsoft.c om...
>>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



Jul 18 '07 #4
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:Oh****************@TK2MSFTNGP03.phx.gbl...
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]la*****@lamsoft.neta écrit dans le message de news:
eF**************@TK2MSFTNGP06.phx.gbl...
>But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@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




Jul 18 '07 #5
On Jul 18, 6:21 pm, Hans Kesting <news.2.han...@spamgourmet.com>
wrote:
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

Jul 19 '07 #6
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]la*****@lamsoft.neta écrit dans le message de news:
ue**************@TK2MSFTNGP02.phx.gbl...
>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:Oh****************@TK2MSFTNGP03.phx.gbl...
>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]la*****@lamsoft.neta écrit dans le message de news:
eF**************@TK2MSFTNGP06.phx.gbl...
>>But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@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




Jul 19 '07 #7
On Jul 18, 6:21 pm, Hans Kesting <news.2.han...@spamgourmet.com>
wrote:
>>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
Jul 19 '07 #8
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]la*****@lamsoft.netwrote in message
news:ue**************@TK2MSFTNGP02.phx.gbl...
>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:Oh****************@TK2MSFTNGP03.phx.gbl...
>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]la*****@lamsoft.neta écrit dans le message de news:
eF**************@TK2MSFTNGP06.phx.gbl...
>>But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@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



Jul 19 '07 #9
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]la*****@lamsoft.netwrote in message
news:ue**************@TK2MSFTNGP02.phx.gbl...
>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:Oh****************@TK2MSFTNGP03.phx.gbl...
>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]la*****@lamsoft.neta écrit dans le message de news:
eF**************@TK2MSFTNGP06.phx.gbl...
>>But there are not only 1 static variable in Class A, .... i don't wanna
make every function for each static variable

"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0**************************@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



Jul 19 '07 #10

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

Similar topics

2
by: Thomas Mlynarczyk | last post by:
Hello, Is there a way to access a static variable defined inside a function from outside that function? Something like the following: function foo() { static $bar = 'stuff'; // more code }
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
8
by: Mike Turco | last post by:
This is a strange one. I have an app that needs to know who is using the program but there's no need for security. When the program opens a form comes up with a listbox. The user double-clicks...
7
by: Antonio | last post by:
I'm developing the firmware for a slave in a comunication channel. Now there is certain information (namely the addresses of the slave and the master) that must be changeable while the device is up...
3
by: Ray Stevens | last post by:
How do you access properties of the main program's class from another form? There does not apear to be an instance variable that can be used.
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
5
by: Roy Gourgi | last post by:
Hi, Is it possible to declare a variable or array in the class that is accessible to all the methods in the class? For the example below, I would like to make the variable lnVar1 accessible to...
4
by: javialmeida | last post by:
Hello everybody. I've been having a look to the c++ FAQ but couldn't get any help there. Does anybody know if is it possible to access a static variable declared within a function?. Something...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.