• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Static properties, is this the right way of pasting data between classes?.

I have three class in the chain before instantiated within the window form1, which goes

=======================================
Class Form1                           //Window forms.
{
Xdata myTek = new Xdata()    //instantiated
.........
}

class Xdata:Xcontrol
{
private int m_Xdata_variable;
public int Xdata_Variable {(..get/set..)}
}

class Xcontrol:Xscope
{
Xdata_Variable=34;                       //Complier error, will not access variable to above class.
Xscope_Variable=40;                     // No error, access variable to class below.
}

class Xscope
{
private int m_Xscope_variable;
public int Xscope_Variable {(..get/set..)}
}
=====================================

Now, I can fix the above problem by using static properties, which mean this variable is no longer instantiated.

=====================================
class Xdata:Xcontrol
{
private static int m_Xdata_variable;
public static int Xdata_Variable {(..get/set..)}
}

class Xcontrol:Xscope
{
Xdata.Xdata_Variable=34;              // No error, static properties is used here
Xscope_Variable=40;                     // No error, access variable to class below.
}

class Xscope
{
private int m_Xscope_variable;
public int Xscope_Variable {(..get/set..)}
}
=======================================

The question are (very concern!):-
(a) Am I doing the right way?. I am breaking a rules of OOP somewhere?. Is this anything wrong about it?.
(b) The inherientance of the classes does not necessary make it available for properties or method, depending where you acess them, true?.
(c) Is there alternative?.
(d) Can interface method be used to transfer data between classes, please provides example program. I was under impression they do not, since it does not support properties and method is standalone program with no reference to above classes, right?.

Thanks

0
riscy
Asked:
riscy
  • 3
  • 2
  • 2
  • +2
3 Solutions
 
riscyAuthor Commented:
The other reason I was concern that I wish to reuse the class for other application which is why I keen to maintain OOP but not clear if static is perfectly allowed since it rely on linkage to other classes, which may cause problem in the future reuse.
0
 
3MannCommented:
just my comment:

using static variables will make your classes strongly linked with each other.
but i guess you want your variables accessible even without instantiating a Xdata object so its ok to use static.

i suggest you just provide an initial value just in case another calling class will not initialize the static variable.

e.g.

class Xdata:Xcontrol
{
private int m_Xdata_variable = 34;  //initial or default value
public int Xdata_Variable {(..get/set..)}
}
0
 
AgariciCommented:
you cant access a private var from the base class
if you want to use it in a derived class you have to delare it as "protected"

so, you are not doing it the right way. the right way is to declare the var as protected and use base.variableName to access it in derived class (it will not raise an error since it is not private to the base class any more )

interfaces are only declarations of how a class implementing that interface will "look like". so, if a class implements an interface wich has a member function XFunction you can be sure an object of that class will have a XFunction member function that you can call using this sintax: objectImplementingInteface.XFunction();

hth,
A.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Carl TawnSystems and Integration DeveloperCommented:
Your code is back to front.

You are actually trying to assign a value to a variable in a derived class (Xdata) from the base class (Xcontrol), which won't work. The fact that Xcontrol is defined higher up in the file to Xdata doesn't effect the inheritance.

There is no way to set a member of a derived class from the base class (well, you could probably use a virtual property to achieve this, but its a bad design).

If you really need to do this then you're probably better off moving the variable to the base class and making it "protected" so that it is also available to the derived class.

Hope this helps.
0
 
riscyAuthor Commented:
Carl_tawn
Yes...but, .this code is cut down version of much bigger code and hence structed in this way, few week later I added necessary function to resolve problem, these function requires access to variable and bang! here the problem!. Aplogise for not making clear.


0
 
ptmcompCommented:
With the information provided I cannot solve your design problem. The easiest way to solve the access problem is to move the variable to Xcontrol and either use the property in the derrived class to access it or changing the protection level to "protected" as carl_tawn wrote. When your extending code you often get to the point where you need "refactoring". If you only add functionality without adapting the design you'll soon have unmaintainable code. There are many good books about Refactoring, Xtreme Programming and Refactoring to Patterns. I know that you can't read them before accessing your variable but they'll help you for the next design issue...
0
 
Carl TawnSystems and Integration DeveloperCommented:
ptmcomp, my suggestion about "protected" member wouldn't work anyway. If you read the code carefully it is actually the member variable in the derived class that needs to be set by the base class and not the other way around.
0
 
riscyAuthor Commented:
I concluded that

(1) Static is okay, not a bad thing, it keep the code within the encapsulated class for relocation, hence easier to keep track of it.
(2) Moving the code to the base class with protected variable at expense of keep code encapsulated, not a best solution but avoid static approach.
(3) Interface cannot be used for accessing field variable.
(4) Refactoring is not an immediate solution but help design to cover the design requirement  to avoid this issue. Will look into this.

Riscy.
0
 
ptmcompCommented:
>> Static is okay, not a bad thing
But be aware that static members are not instance specific. Static members are ok, when the value is valid for all instances or there can only be one instance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now