access base class variable in derived class - c#

class Program
{
static void Main(string[] args)
{
baseClass obj = new baseClass();
obj.intF = 5;
obj.intS = 4;
child obj1 = new child();
Console.WriteLine(Convert.ToString(obj.addNo()));
Console.WriteLine(Convert.ToString(obj1.add()));
Console.ReadLine();
}
}
public class baseClass
{
public int intF = 0, intS = 0;
public int addNo()
{
int intReturn = 0;
intReturn = intF + intS;
return intReturn;
}
}
class child : baseClass
{
public int add()
{
int intReturn = 0;
intReturn = base.intF * base.intS;
return intReturn;
}
}
I want to access intF and intS in child class whatever i input.. but i always get the values of both variables 0. 0 is default value of both variables.
Can anyone tell me that how can i get the value???
thx in adv..

Yes, Zero is what you should get, since you made single instance of child class and did not assign any value to its inherited variables,
child obj1 = new child();
rather you have instantiated another instance of base class separately and assign value to its members,
baseClass obj = new baseClass();
both runtime both the base class instance and child instance are totally different objects, so you should assign the child values separately like
obj1.intF = 5;
obj1.intS = 4;
then only you shall get the desired result.

You've got two separate objects - that's the problem. The values of the base class variables in the object referred to by obj1 are 0, because they haven't been set to anything else. There's nothing to tie that object to the object referred to by obj.
If you need to access the variables of another object, you'd have to make that object available to the one trying to access the data. You could pass obj as an argument to a method, or perhaps make it a property. There are lots of different approaches here, but we don't know what the bigger picture is - what you're really trying to do. Once you understand why it's not working as you expect it to, you can start thinking about what you're really trying to do.
Just to be clear, this has nothing to do with inheritance. It has everything to do with you creating two distinct objects. You'd get the same effect if you just used a single class, but created two different instances of that class.

Related

Overriding Base Property and Accessing Reference in Base

In the code below I have a property in base class which returns a list of custom objects. In the parent class I override this property and in the definition of the override I access the reference to the list of custom objects from the base class and add 2 objects to it.
Before returning I put a breakpoint in the code and check the content of the base property and notice that the two new objects are not there. Then I tried storing the reference to the list of objects in the base class locally and added two objects in the list again. I notice that in the local reference the 2 new objects have been added.
However, using both methods I'm pointing to the same reference so I should be able to add objects by referring to the base.TestProperty. Any idea why that won't work?
public override List<CustomObject> TestProperty
{
get
{
List<CustomObject> temp = base.TestProperty;
CustomObject obj1 = new CustomObject()
{
Name = "My Name"
};
CustomObject obj2 = new CustomObject()
{
Name = "Your Name"
};
// Adding to the base list
base.TestProperty.Add(obj1);
base.TestProperty.Add(obj2);
// Adding to temp list, which still points to the base list
temp.Add(obj1);
temp.Add(obj2);
// Base object doesnot contain obj1 and obj2, but the temp object does.
return base.TestProperty;
}
}
This isn't really the specific answer you're looking for, but... you should really reconsider your design.
You've got a property in your subclass... and getting that property changes your class' values. That's extremely counterintuitive. It's not like you'd expect:
Color bgCol = Color.Red;
int red = bgCol.R;
... that second statement to change values of your variable just by accessing one of its properties! How confused would you be if, when running that second statement, it changed the contents of bgCol to yellow?
My advice? Have the base class return what it's supposed to - forgetting about the subclass. And if your subclass needs to add values to that result? Then have it add the values when the subclass's property is called - but only to the result it's passing back - don't have it mess with the base object's properties at all.
public override List<CustomObject> TestProperty
{
get
{
List<CustomObject> objectsFromBase = base.TestProperty;
List<CustomObject> objectsFromThisClass = GetMySubclassCustomObjects();
List<CustomObject> retVal = new List<CustomObject>();
retVal.AddRange(objectsFromBase);
retVal.AddRange(objectsFromSubclass);
return retVal;
}
}
private List<CustomObject> GetMySubclassCustomObjects()
{
// your code for those two CustomObjects, and returning them from a list
}

Data type for a variable that can be different types

I am new to C# but seem to have noticed a limitation from what I am wanting to do with my code.
I have a class which i want to store a reference to other classes which i plan to store in a list. Something like this:
myList.Add(new Node(1,1,referenceToClassA));
myList.Add(new Node(1,2,referenceToClassB));
So my class would look like this:
public class Node : IHeapItem<Node> { //IHeapItem is for a heap i use for pathfinding
public int x;
public int y;
public ??? reference;
// constructor
public Node(int a, int b , ??? r){
x = a;
y = b;
reference = r;
}
// other unrelated stuff
}
So as you can probably guess, i have no idea what data type reference would be in my class given that it could be assigned to different classes.
I can't seem to find if there is a data type that is flexible for this in C# (i started in JavaScript so am not used to strict behavior on variable types).
What are my options here, what data type should I use, or will i have to implement this in a totally different way?
Hope you can help.
IF you only have one type of item in each list, then you could use this:
public class Node<T> : IHeapItem<Node> { //IHeapItem is for a heap i use for pathfinding
public int x;
public int y;
public T reference;
// constructor
public Node(int a, int b , T r){
x = a;
y = b;
reference = r;
}
// other unrelated stuff
}
If you don't know what you're going to get, and you will have more than one item in a list, then you're forced to use object. With a little reflection, that can work out pretty well.
It is also possible that you will have sets of different items, each set could implement the same interface, then that interface could be what you hold in the list.
If you're trying to create a generic, use T as the parameter type.
If you use T as the parameter type, you'll have to modify your class to be Node<T> as well.
Otherwise, you could use dynamic.
There is a class called Object that can reference any other class. If you want to make it just to a little group of classes, you may want to create an abstract class or an interface.

Type confusion: A type is class, a variable and an object?

I'm currently working through the Pluralsight C# 5.0 course, and I'm relatively new to programming.
I previously thought I understood the concept of Data Types on a basic level, Int/Array/Strings etc.
In this course it starts to introduce C#'s huge emphasis on Types, and creating your own custom types.
One of the course code snippets which I've included below, is refusing to sink in and I was hoping
someone could provide some clarity or a different way of thinking about it.
Program.cs:
GradeStatistics stats = book.ComputeStatistics();
GradeStatistics.cs:
namespace Grades
{
public class GradeStatistics
{
public GradeStatistics()
{
HighestGrade = 0;
LowestGrade = float.MaxValue;
}
public float AverageGrade;
public float HighestGrade;
public float LowestGrade;
}
}
Grades:
public GradeStatistics ComputeStatistics()
{
GradeStatistics stats = new GradeStatistics();
float sum = 0f;
foreach (float grade in grades)
{
stats.HighestGrade = Math.Max(grade, stats.HighestGrade);
stats.LowestGrade = Math.Min(grade, stats.LowestGrade);
sum += grade;
}
stats.AverageGrade = sum / grades.Count;
return stats;
}
I'm finding it particularly difficult to understand what exactly GradeStatistics is.
In the course it is referred to as not only a class, but as a variable, and furthermore also
being returned as an object in Grades.
Any clarity is appreciated, as I'm finding it a little difficult to follow with all of the
above terms being thrown around.
GradeStatistics is a class, from this declaration:
public class GradeStatistics
stats is a variable of type GradeStatistics, from this declaration:
GradeStatistics stats = new GradeStatistics();
The return type of ComputeStatistics is GradeStatistics, from this declaration:
public GradeStatistics ComputeStatistics()
So, it's a class. It is being used to declare a variable with a particular type, and it is used to declare what a particular method will return, an object of the type.
If it helps, you can sort of think of a type as a blueprint. You can have a blueprint of a house. This will tell you that "if you had a house", this is what it would look like.
When you construct an instance of the type, ie. build the house, you get an instance. It has a type (it follows the blueprint), but it may be different from other instances, having other property values (like the color of the paint used, or the style of doors).
First, GradeStatistics is a Class.
Second, it is not referred as a variable anywhere. But stats is actually a variable of type GradeStatistics in this line.
GradeStatistics stats = new GradeStatistics();
Third, ComputeStatistics is a function which return GradeStatistics.
And if you have read OOP than you should know that any object of any class type can be returned as a function return value.
GradeStatics is a class by definition. When you use the new keyword you are creating an instance of that class (an object) which you can pass around or assign it to a variable.
So your snippets above, GradeStatistics is defined as a class in the gradestatics.cs file. Grades.cs creates an instance of this class in the computestatics method and assigns it to the stats variable which is populated in yhat method and then returned
GradeStatistics is a class. From this snippet:
public class GradeStatistics
{
public GradeStatistics()
{
HighestGrade = 0;
LowestGrade = float.MaxValue;
}
public float AverageGrade;
public float HighestGrade;
public float LowestGrade;
}
However, in the method ComputeStatistics, the return type is GradeStatistics, not void as you are probably accustomed to. This means that ComputeStatistics returns a GradeStatistics. Lets look at an easier example.
Suppose we have a class Foo. It is constructed like:
public class Foo
{
public int a;
public int b;
public Foo()
{
a = 0;
b = 0;
}
}
Now we can create Foos and access them normally.
Foo foo = new Foo();
foo.a = 4;
foo.b /= 2;
Now suppose we want to do this, except many times, so we create a method.
public Foo IncrementFoo(int amount, int divideBy)
{
Foo obj = new Foo();
obj.a = amount;
obj.b /= divideBy;
return obj;
}
This method creates a new object of type Foo, and returns it. You can then use this code like:
Foo newFoo = IncrementFoo(4, 2);
//foo == newFoo
I would recommend you read more here, especially about return types.
Note: In this case, it may be better to write an Extension method, especially if we want to modify a single instance of a Foo, but we don't want to get into that. Also if we were really wanting to create a new instance of Foo like that, it would be better to use a constructor.

Access property of a object without needing to cast it

I have the following three classes
public class Base
{
string name;
}
public class Foo : Base
{
int value;
}
public class Bar : Base
{
double value;
}
This is what I'm attempting
Base current = null;
if (somecondition)
current = new Foo();
else
current = new Bar();
for (int i=0; i<5; i++)
{
current.value = i;
}
The problem is VS 2010 shows an error in the loop body because Base doesn't have a property value.
Now, I could workaround this issue by this way:
Base current = null;
bool isBar = true;
if (somecondition)
{
current = new Foo();
isBar = false;
}
else
current = new Bar();
for (int i=0; i<5; i++)
{
if (isBar)
(current as Bar).value = i;
else
(current as Foo).value = i;
}
But I was hoping for a better solution because once the loop starts iterating, the type of current isn't going to change, yet I am going to test the type and accordingly cast it for each iteration.
What would be the right way to do this?
After making the fields accessible (or exposing them as accessible properties instead), your options are:
Make your base class abstract, with an abstract method or property to set the value (and possibly return it) - the problem here is that you can't easily do that when the two types are different
Create an interface to do the same sort of thing, and cast to the interface
Just cast as you are now
Use dynamic if you're using C# 4 - just declare current as dynamic, and you can assign to current.value and the compiler will insert code to work it out at execution time
Redesign your code / inheritance hierarchy
Personally I would at least consider the last approach - is inheritance definitely appropriate here? Do the two types for value really need to be different? Do they have the same meaning, and if so would it make sense to push the value to the base class and potentially make it generic if you need different types?
You could instead of using a base class create an interface that has Value as a property and then have Foo and Bar implement that interface.
First, your "value" fields are not exposed as public.
Second, I don't think you won't get away from explicit casting here. Just imaging that your Bar class look like this:
public class Bar : Base
{
public string value;
}

Creating class instances at run-time and initializing simultaneously

I am attempting following code to create multiple instances of a class at run-time and want to initialize also, but it is giving error:
A local variable named 'inum' cannot be declared in this scope because
it would give a different meaning to 'inum', which is already used in
a 'parent or current' scope to denote something else.
public class MyClass
{
static int i=0;
class A
{
public A()
{
}
}
public static void Run()
{
string inum = "i";
for (int j=1;j<=5;j++)
{
inum = inum + j.ToString();
//Initialize Instance
A inum = new A();
}
}
}
You appear to be trying to use variable names "dynamically". That doesn't work in C#, and you should change how you think about variables. If you want to create several instances, declare an array:
public class MyClass
{
static A[] instances;
class A
{
public A()
{
}
}
public static void Run()
{
instances = new A[5];
for (int j=0;j<5;j++)
{
instances[j] = new A();
}
}
}
You cannot have dynamic variable in c#. The append you are trying is appending the value not the variable pointer.
rather use this way
Dictionary<int, A> inum = new Dictionary<int, A>();
for (int j=1;j<=5;j++)
{
//Initialize Instance and add to dictionary
inum.Add(j, new A());
}
You can get them by key name. There are several other way to store instances as collection
I'm not a C# programmer by any stretch of the imagination, but by the rules of Java and any other similarly syntaxed language I know anything about, what you are doing is attempting to redeclare 'inum' with a new type, after it has been declared as a string in the same scope.
The other point is that even if this were not the case, you are not creating multiple instances but filling the same variable with a new instance 5 times, which would only result in one instance (the last one).
From quickly reading a C# tutorial I think this is something like what you want. I'm not sure what you were trying to do with the 'inum' variable so it is gone, as is static variable 'i':
public class MyClass
{
class A
{
public A()
{
}
}
public static void Run()
{
// Declare array to hold instances
A[] instances;
// instances is now five elements long
instances = new A[5];
for (int j=0;j<5;j++)
{
//Initialize Instance
instances[j] = new A();
}
}
}
That should result in an array of 5 objects called 'instances' in the scope of the Run method - you may want this in the scope of the class itself, possibly as a static property.
As a side note, it's good practice to start at 0, not 1, for operations like this (with the var 'j') and the above code reflects this.
you cannot call the variable of type A "inum" (there exists already one called like that)
you have to give it another name like:
A anyOtherName = new A();
try to name the variable A with different name
A objA = new A();

Categories