RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

.NET 2.0 Generics: Generic Return Types

wieganka

Sorceror
.NET 2.0 Generics: Generic Return Types

Generic Return Types

Although this may have limited use in RunUO itself...I figured I'd offer a small extension to the wonderful Generics Tutorial that mordero wrote, and add on something about "Generic Return Types".

I have run into this situation in a number of places...but more often than not when doing Web Development (I am a Web Developer by profession).

Something new in .NET 2.0 made something a bit easier: Getting Session values (and QueryString values too).

Before, to make life easier at getting some values back...you had to do something along the lines of this:

Code:
public static class Tools
{
 public static string GetStringSessionObject(string strName)
 {
  string strReturn = "";
 
  if (HttpContext.Current.Session[strName] != null)
  {
   strReturn = HttpContext.Current.Session[strName].ToString();
  }
 
  return strReturn;
 }
 
 public static int GetIntSessionObject(string strName)
 {
  int intReturn = 0;
 
  if (HttpContext.Current.Session[strName] != null)
  {
   int.TryParse(HttpContext.Current.Session[strName].ToString(), out intReturn);
  }
 
  return intReturn;
 }
 
 public static MyClass[] GetMyClassSessionObject(string strName)
 {
  MyClass[] objMyClass = new MyClass[]{ };
 
  if (HttpContext.Current.Session[strName] != null)
  {
   try
   {
    objMyClass = (MyClass[])HttpContext.Current.Session[strName];
   }
   catch
   {
   }
  }
 
  return objMyClass;
 }
}

...And you'd have to do this for EVERY data type you'd ever want to return...how ridiculous! Granted, it's only 1 line of code to get your appropriate value, but I'd have to do one method for int, double, string, bool, and several user-defined classes as well...stupidly unmanageable!

OR, you could have just kept with a single procedure such as:

Code:
public static class Tools
{
 public static string GetSessionObject(string strName)
 {
  string strReturn = "";
 
  if (HttpContext.Current.Session[strName] != null)
  {
   strReturn = HttpContext.Current.Session[strName].ToString();
  }
 
  return strReturn;
 }
}

But, of course, you'd have to handle any data type casting later on (3 lines of code to get an integer value, with 1 procedure declaration in the Tools class):

Code:
 //Get the value as a string for now...
 string strValue = Tools.GetSessionObject("MySessionObject");
 
 //...but, I wanted an integer...
 int intValue = 0;
 
 //...so now I still have to do this...
 int.TryParse(strValue, out intValue);

BUT...there is hope now in .NET 2.0: Generic Return Types!

Try this one on for size:

Code:
public static class Tools
{
 public static T GetSessionObject<T>(string strName)
 {
  T objReturn = default(T);
 
  if (HttpContext.Current.Session[strName] != null)
  {
   try
   {
    objReturn = (T)HttpContext.Current.Session[strName];
   }
   catch
   {
   }
  }
 
  return objReturn;
 }
}

Looks meanacing at first...I know...but here's the cool catch - when you call this method, you tell it ON DEMAND what return type you want: And you can use ANYTHING, including arrays! Here's how you'd use it:

To return a string value:
Code:
 string strValue = Tools.GetSessionObject<string>("MySessionObject");

To return an int value:
Code:
 int intValue = Tools.GetSessionObject<int>("MySessionObject");

To return a double value:
Code:
 double dblValue = Tools.GetSessionObject<double>("MySessionObject");

To return a user-defined array'ed value:
Code:
 MyClass[] objValue = Tools.GetSessionObject<MyClass[]>("MySessionObject");

And as you can see - 1 line of code for whatever return type you'd ever possibly want...and on top of that..1 easily managed procedure in the Tools class, that frankly, you'd never have to touch ever again!

So...while the example I've show works well for websites..it CAN be used for other applications...such as reading int, double, bool values, etc from INI (or the like) files, and being able to cast them to the required type quickly and easily - for instance, if you had a configuration file with a bunch of values...you could effectively make a class that could handle this:

Code:
 string strValue = ConfigFile.GetValue<string>("Player Name");
 long lngValue = ConfigFile.GetValue<long>("Max Score");
 double dblValue = ConfigFile.GetValue<double>("Last X Position");
 bool bolValue = ConfigFile.GetValue<bool>("Completed Quest 1");

So with that - Good Luck, and Happy Coding!
 
Top