Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:28:02+00:00 2026-05-27T13:28:02+00:00

In my code I’ve got a generic, getProperty like so: public T getProperty<T>(int GUID,

  • 0

In my code I’ve got a generic, “getProperty” like so:

public T getProperty<T>(int GUID, string property)
{
    PropertyComponent prop;
    prop = propDict[GUID];
    if(property.Equals("visible")) return (T) (Boolean) prop.visible;
    if(property.Equals("enabled")) return prop.enabled;
    if(property.Equals("position")) return (T) (Object) prop.position;
}

Visual Studio’s compiler gives me no error for the 3rd term, as prop.position is a Vector2. Prop.visible and prop.enabled, however, are bool s, so when I try to return them in this way, I get an error of “Cannot convert type bool to T” (and “Cannot implicitly convert type bool to T” for the 2nd).

What’s the right way to return a bool in this situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:28:03+00:00Added an answer on May 27, 2026 at 1:28 pm

    You can constrain a generic type parameter to be a class or a struct (using where clauses) but in your example you’re treating the T type as both a class and a struct (the bool value type).

    If you want to return reference type object instances as well as value type data (like bool), you’re going to have to “box” the value types into objects, as the reference type object is the only common denominator between reference types and value types. Boxing happens automatically when you typecast a value type to Object. So, all your return values should be typecast to (T)(object).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Code goes first: class A { public: ... int *foo() const { return _px;
CODE:1 class Ajay { private void display() { System.out.println(Ajay); } public static void main(String
Code #include <OOLua/oolua.h> class foo { public: int bar(); }; OOLUA_CLASS_NO_BASES(foo)//class has no bases
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code like this often happens: l = [] while foo: # baz l.append(bar) #
Code with iterators looks pretty much like code with pointers. Iterators are of some
I would like to count the length of a string with PHP. The string
Code: public class SMH extends Activity { public void onCreate(Bundle b) { super.onCreate(b); setContentView(R.layout.main);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.