locked
can i get a value from a Type? RRS feed

  • Question

  • User1034446946 posted

    i have a class which has a value

    public class foo
    {
       public string SomeValue => "999";
    }

    i only have the Type,is there a way to

    public void SomeWork(Type type)
    {
     var avariable = //get 999 from the Type
    }

    the value is fixed in the class, which will then be used to inherite from.

    i am trying to not need to manually put the value in the method.

    any suggestions appriciated.

    Thursday, May 21, 2020 11:57 AM

Answers

  • User475983607 posted

    i have a class which has a value

    public class foo
    {
       public string SomeValue => "999";
    }

    i only have the Type,is there a way to

    public void SomeWork(Type type)
    {
     var avariable = //get 999 from the Type
    }

    the value is fixed in the class, which will then be used to inherite from.

    i am trying to not need to manually put the value in the method.

    any suggestions appriciated.

    Not possible.  Generics solve this programming problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:04 PM
  • User753101303 posted

    Hi,

    Unclear though I can understand the confusion. The "value" of a Type variable is "it's a string", or an integer or whatever. A Type variable itself doesn't have any particular value such as "999' or 10 or whatever.

    As often which pseudo code that can't work I'm not sure to get your intent. If you want to create a base class that uses a default value for a single method you could perhaps use https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-parameters .

    Or you want to be ablet to change this default value only in a derived class. Or this value is not always a string (Generics maybe). Or this default value is used at multiple places within your class etc...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:15 PM
  • User475983607 posted

    thanks for the info, i need to get a fixed value based on a subclass, i was wondering if i could put it in the class, but i have had to create a method else where which looks for a the base class and sets the value based on that.

    Your requirement is too abstract to understand why this is necessary.  The derived class knows its inheritance chain and can set whatever value you like.  What is the purpose of reflecting over the type to get the base class?  And if you can query the meta data to find the base class then it seems redundant to set a value.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 1:42 PM
  • User-474980206 posted

    this is what interfaces are for. 

    public interface ISomeValue
    {
        string SomeValue {get;}
    }
    
    public class Foo : ISomeValue
    {
       public SomeValue => "999";
       ....
    }
    
    public class Foo2 : Foo
    {
       ....
    }
    
    public class Bar : ISomeValue
    {
       public SomeValue => "888";
       ....
    }
    
    public void SomeWork(ISomeValue data)
    {
       var value = data.SomeValue;
    }
    
    
    // now call
    
    SomeWork(new Foo());
    SomeWork(new Foo2());
    SomeWork(new Bar());
    
    
    
    

    if you want from Type, the use generic

    public static void SomeWork<T>() where T : ISomeValue, new()
    {
        var v = new T().SomeValue;
    }
    
    ...
    
    SomeWork<Foo>();
    SomeWork<Bar>();
    	
    
            

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:46 PM

All replies

  • User475983607 posted

    i have a class which has a value

    public class foo
    {
       public string SomeValue => "999";
    }

    i only have the Type,is there a way to

    public void SomeWork(Type type)
    {
     var avariable = //get 999 from the Type
    }

    the value is fixed in the class, which will then be used to inherite from.

    i am trying to not need to manually put the value in the method.

    any suggestions appriciated.

    Not possible.  Generics solve this programming problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:04 PM
  • User1034446946 posted

    thought so,thanksfor the help.

    Thursday, May 21, 2020 12:08 PM
  • User753101303 posted

    Hi,

    Unclear though I can understand the confusion. The "value" of a Type variable is "it's a string", or an integer or whatever. A Type variable itself doesn't have any particular value such as "999' or 10 or whatever.

    As often which pseudo code that can't work I'm not sure to get your intent. If you want to create a base class that uses a default value for a single method you could perhaps use https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-parameters .

    Or you want to be ablet to change this default value only in a derived class. Or this value is not always a string (Generics maybe). Or this default value is used at multiple places within your class etc...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 12:15 PM
  • User1034446946 posted

    thanks for the info, i need to get a fixed value based on a subclass, i was wondering if i could put it in the class, but i have had to create a method else where which looks for a the base class and sets the value based on that.

    Thursday, May 21, 2020 1:13 PM
  • User475983607 posted

    thanks for the info, i need to get a fixed value based on a subclass, i was wondering if i could put it in the class, but i have had to create a method else where which looks for a the base class and sets the value based on that.

    Your requirement is too abstract to understand why this is necessary.  The derived class knows its inheritance chain and can set whatever value you like.  What is the purpose of reflecting over the type to get the base class?  And if you can query the meta data to find the base class then it seems redundant to set a value.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 1:42 PM
  • User1034446946 posted

    at the time i didn't have different base classes, and wanted to just set a value,but created different base classes but now instead of just looking up a property I have to get the base class and then set a value.

    So yes now the value is redundant.

    Thursday, May 21, 2020 2:08 PM
  • User-474980206 posted

    this is what interfaces are for. 

    public interface ISomeValue
    {
        string SomeValue {get;}
    }
    
    public class Foo : ISomeValue
    {
       public SomeValue => "999";
       ....
    }
    
    public class Foo2 : Foo
    {
       ....
    }
    
    public class Bar : ISomeValue
    {
       public SomeValue => "888";
       ....
    }
    
    public void SomeWork(ISomeValue data)
    {
       var value = data.SomeValue;
    }
    
    
    // now call
    
    SomeWork(new Foo());
    SomeWork(new Foo2());
    SomeWork(new Bar());
    
    
    
    

    if you want from Type, the use generic

    public static void SomeWork<T>() where T : ISomeValue, new()
    {
        var v = new T().SomeValue;
    }
    
    ...
    
    SomeWork<Foo>();
    SomeWork<Bar>();
    	
    
            

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:46 PM
  • User1034446946 posted

    thanks for the info, i don't have an instance yet so i don't think I can use an interface.

    will be working on in for the next few days so have plenty of options now thanks all.

    Thursday, May 21, 2020 8:01 PM