2
Answers

I can't get value of base class field from child class object in c#

  1. using System;                     
  2. public class Program  
  3. {  
  4.     public static void Main()  
  5.     
  6.          CloudCollectionHelper cloudHelper = new CloudCollectionHelper();  
  7.          SlackHelper slackHelper = new SlackHelper();      
  8.         cloudHelper.DatabaseID=12345;  
  9.         Console.WriteLine(slackHelper.GetSlackPageTokens());
  10.     }
  11.     class CloudCollectionHelper  
  12.     {  
  13.         public  long DatabaseID { get; set; }     
  14.     }  
  15.     class SlackHelper:CloudCollectionHelper  
  16.     {  
  17.           
  18.         public long GetSlackPageTokens()  
  19.         {  
  20.             return DatabaseID;  
  21.         }  
  22.     }  
  23. }  
current output: 0 Expected Output: 12345
 
I need output 12345 because DatabaseID from the cloudhelper so i need that databaseID in the slackhelper.
 
this is my c# online compiler: https://dotnetfiddle.net/QNQeEX
 
I dont want to write something below because I have 1000+ field in the cloudhelper
  1. SlackHelper slackHelper = new SlackHelper();          
  2. slackHelper.DatabaseID=12345;  
  3. Console.WriteLine(slackHelper.GetSlackPageTokens());  
Thanks in advance.

Answers (2)

1
Photo of Sachin Singh
NA 55.8k 87.9k 4y
your code should be like below
  1. public static void Main()  
  2.        {  
  3.   
  4.   
  5.            SlackHelper sh = new SlackHelper();  
  6.   
  7.            Console.WriteLine(sh.GetSlackPageTokens());  
  8.            Console.ReadLine();  
  9.        }  
  10.   
  11.         
  12.  public  class CloudCollectionHelper    
  13.    {    
  14.        public  long DatabaseID { get; set; }       
  15.    }    
  16.  public  class SlackHelper:CloudCollectionHelper    
  17.    {    
  18.            
  19.        public long GetSlackPageTokens()    
  20.        {  
  21.            DatabaseID = 12345;  
  22.            return DatabaseID;    
  23.        }    
  24.    }   
 
-1
Photo of Mageshwaran R
NA 10.8k 1.1m 4y
Hi Talaviya,
Try this code:
 
  1. class Vehicle  // base class (parent)   
  2. {  
  3.   public string brand = "Ford";  // Vehicle field  
  4.   public void honk()             // Vehicle method   
  5.   {                      
  6.     Console.WriteLine("Tuut, tuut!");  
  7.   }  
  8. }  
  9.   
  10. class Car : Vehicle  // derived class (child)  
  11. {  
  12.   public string modelName = "Mustang";  // Car field  
  13. }  
  14.   
  15. class Program  
  16. {  
  17.   static void Main(string[] args)  
  18.   {  
  19.     // Create a myCar object  
  20.     Car myCar = new Car();  
  21.   
  22.     // Call the honk() method (From the Vehicle class) on the myCar object  
  23.     myCar.honk();  
  24.   
  25.     // Display the value of the brand field (from the Vehicle class) and the value of the modelName from the Car class  
  26.     Console.WriteLine(myCar.brand + " " + myCar.modelName);  
  27.   }  
  28. }