logo
down
shadow

How to return null for a method whose return type is enum?


How to return null for a method whose return type is enum?

By : Reishou Kirisame
Date : November 19 2020, 12:41 AM
Any of those help I have an enum which have some values , Make your method return a nullable CompareOperators:
code :
public static CompareOperators? GetTypeForOperator(string strType)
public static Nullable<CompareOperators> GetTypeForOperator(string strType)
public static CompareOperators? GetTypeForOperator(string strType)
{
    switch (strType)
    {
        case "=":
            return ...
        default:
            return null;
    }
}
var x = GetTypeForOperator("strType");
if (x != null)
{ ... }
public static CompareOperators GetTypeForOperator(string strType)
{
    switch (strType)
    {
        case "=":
            return ...
        default:
            return CompareOperators.Unknown;
    }
}
public static CompareOperators GetTypeForOperator(string strType)
{
    switch (strType)
    {
        case "=":
            return ...
        default:
            throw new ArgumentException("strType has a unparseable value");
    }
}


Share : facebook icon twitter icon
Parameterizing the return type of a method using an enum -- possible?

Parameterizing the return type of a method using an enum -- possible?


By : Dave
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , In java, I know it's possible to e.g.: , dacc, would something like this work for you?
code :
public static class ClientSetting<T> {
   private T setting;

   ClientSetting(T setting) {
      this.setting = setting;
   }

   public T get() {
      return setting;
   }
}

// Old school, I know.
public static final ClientSetting<String> SOME_STRING_SETTING = 
   new ClientSetting<String>("My string value.");
public static final ClientSetting<Integer> SOME_INTEGER_SETTING = 
   new ClientSetting<Integer>(2);

public static <T> T get(ClientSetting<T> clientSetting) {
   // delegation, this method is not really needed
   // you can go for SOME_STRING_SETTING.get()
   return clientSetting.get();
}

public static void main(String[] args) {
   String stringValue = get(SOME_STRING_SETTING);
   Integer integerValue = get(SOME_INTEGER_SETTING);
   // Won't compile
   // String wrong = get(SOME_INTEGER_SETTING);
}
Generic return type of enum-method

Generic return type of enum-method


By : Mohamad
Date : March 29 2020, 07:55 AM
this one helps. Unfortunately enums cannot be generic. This is because enum is an ordinary class that extends Enum, i.e. its definition looks like the following:
class MyEnum> extends Enum {}
MyBatis: Attempted to return null from a method with a primitive return type

MyBatis: Attempted to return null from a method with a primitive return type


By : Dani Ardhyanth
Date : March 29 2020, 07:55 AM
it should still fix some issue Your Mapper XML specifies resultType="Integer", but getUpdateTaskStateTask() wants to return a primitive int, not the Integer wrapper class.
Change it to resultType="int".
Return Enum of null dynamically using the Type

Return Enum of null dynamically using the Type


By : Afsar
Date : March 29 2020, 07:55 AM
it helps some times Returning null from the ReadJson method should be all you need to do. Nullable is a value type (even though you can test for and assign null to it). When it gets boxed into a referenced type (object for example), then it does one of two things:
If the value represented by Nullable is NOT null, then the Value is boxed just like any other value type would be. If Nullable is reprsenting a null value, then the result is actually null.
How to return error message/null from a method whose return type is int?

How to return error message/null from a method whose return type is int?


By : user6036761
Date : October 10 2020, 12:00 AM
wish helps you You can return multiple values from a method using the concept of Tuple class and change your method in this way:
code :
public (int id, string message) GetDataFromDB()
{
    if(dataFromDB == null)
        return ( 0, "Data not found");
    else
        return (dataFromDB.Id, "");
}
var result = GetDataFromDB();
if(string.IsNullOrEmpty(result.message))
    Console.WriteLine("Success, found ID=" + result.id);
else
    Console.WriteLine("Failure: " + result.message);
Related Posts Related Posts :
  • I am trying to do a while loop with a string conditional statement in C#
  • C# 'Cannot access a disposed object. Object name: 'SslStream'.'
  • How to make Gecko use seperate CookieContainer per instance?
  • C# Advanced form "please wait"
  • Send and Receive data C# using network stream
  • How to discover that appsettings changed in C#?
  • Check what needs full trust
  • What is the execution order of an MVC Razor view/layout
  • Table designer (Entity Framework) is too resource intense
  • How to clean up an exception string so it can be displayed via Javascript Alert?
  • Configuring Amazon SES Feedback Notifications via Amazon SNS in ASP.NET MVC (C#)
  • C# api responce and request
  • Dynamodb putitem function not working properly
  • theme in windows phone(light or dark) using c#
  • Backup attached database file(.mdf) using c# and SQL Server
  • What is 'TextFile' and where it is uses in WPF project
  • Using same alias for multiple namespaces
  • how to move mails into separate folders
  • Multithreaded Uploader
  • Memory fragmentation?
  • C# correct exception handling
  • "Could not open macro storage" when accessing using file on another machine
  • How to access other directories of hosted server
  • C# Jagged Array check if value exists/true
  • Why can't I type Clone() properly?
  • exception on accessing dictionary from list
  • Getting the immediate response from server without waiting to 200 message
  • Why am I getting exception Directory Is Not empty?
  • Could not load file or assembly 'CefSharp.dll' or one of its dependencies
  • Sending Email By Using C# in unity3D?
  • Correct usage of await async in webapi
  • Program update code issue
  • Marshal.Copy attempted to read or write protected memory At Random Times
  • Restrict Type variable to specific class or subclass
  • Horizontal text alignment in a PdfPCell
  • C# crashing with Form.show() command, ObjectDisposedException - Deeper look / explanation please
  • Will the result of a LINQ query always be guaranteed to be in the correct order?
  • "Could not find default endpoint element that references contract"
  • Umbraco Request.QueryString is null if it's the first time the page is loaded
  • Error inconsistent accessibility method C#
  • How to program Intel Xeon Phi with C#?
  • remove nested element using regular expression
  • Is there a C# alternative to Java's vararg parameters?
  • Clear particular column values in DataTable
  • how to add event handler to programatically created checkboxes
  • Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection'
  • Check for key in pre-existing dictionary in case insensitive manner
  • How to remove year from datetime object?
  • Accessing Settings in different ways
  • "This project is empty" error in Sonarqube
  • How to create reusable icon menu in Xamarin
  • Value Cannot be null in Ado.Net connectivity
  • Adding a custom/dynamic attribute when using XSD.exe
  • How to convert object to correct type
  • Automatically sign out from Forms Authentication in ASP.NET when browser is closed
  • Can a WCF service support both Buffered and Streamed transfer modes?
  • Verify a CA Certificate with a public key in C#
  • How to invoke a Web Service that requires the "patch" verb using the C# WebClient wrapper?
  • Proper way a implementing property based on generic type
  • Closing a form that is created in another thread
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org