-4

My Class Structure is Like This.

Class A
{
}

Class B:A{..}

Class Demo
    {
    //Here Actual Parameter is instance of Class B. But in xyz function accepts
    //  parameter of type Class A. So in Below function can i get the Properties of 
    //Class B by using "((B)a).Property_Name", But i don want to specify the Name 
    //of Class B. So is ther any approach to meet my need.
    public void xyz(A a){ ....Here I Can i get }
    }
sandeep
  • 2,862
  • 9
  • 44
  • 54

3 Answers3

1

Point you missed is B is derived class of A.

Which means B already has access to A's properties so you don't need cast.

You can just pass B parameter to XYZ function which takes A and then you can just use

a

to call A's properties.

On the other hand if you are trying to access private properties of A... that is just not possible outside of class A and considered logical error.


Now different scenario is if you are passing B type parameter into XYZ and want to access B's properties then you will have to cast it to B as A was created before B so it has no knowledge of B's specific properties and methods.

Adnan Bhatti
  • 3,410
  • 4
  • 25
  • 38
1

It sounds like you want to use a member of B that doesn't exist in A in a method that takes in A as a parameter. You don't want to move the member to A because it probably does't make sense to do so and you want something special when a B is passed in.

The most straight forward way is to test for it

public void xyz(A a){


     if (a is B) 
     {
          ((B)a).dosomthing()
     } 

 }

However this is bad because maybe you have C that needs this to.

Then maybe its better to introduce an interface e.g.

B: A, ISomthing

public void xyz(A a){


     if (a is ISomthing) 
     {
          ((ISomthing)b)dosomthing()
     } 

 }

Another option is to use Double Dispatch

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
1

No you can't access the members of B from an obj of type A, think of this, if A had properties X and Y, and B had property Z, how would you cast it and provide the value of Z? The CLR would have no idea, you would have to change the method signature to take a param of type B to accomplish this. Alternatively if you absolutely know how all cases of A should cast to B, you could implement your own casting operation to handle that for you.

Jason
  • 3,844
  • 1
  • 21
  • 40
  • "how would you cast it and provide the value of Z" -- like this `((B)a).Property_Name` – Conrad Frix Sep 19 '11 at 05:42
  • Yes you are correct it will compile and *may* work, however, the method takes a type A, there is no guarantee that the type passed will be of type B (if there is then I'd think he should change the type to B instead). If he absolutely wanted to do it that way, imho, using the "as" cast operator and doing a null check or doing the interface implementation (as you suggested in the solution) would actually be better than the former – Jason Sep 19 '11 at 16:08
  • That's a good point about `as` however in this case I would probably go with adding `if (a == null) throw new ArgumentNullException("a");` So as not to conflate a null argument with a unable to cast. Typically I reserve `as` for when I want to treat nulls and unable to cast to type as the same. – Conrad Frix Sep 19 '11 at 16:33