|
||
|
|||||||
| FAQ Forum A place to find answers to the most frequently asked questions, and a place to post said answers. Do NOT use this forum to ask questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Oct 2004
Location: Chesterton, IN
Age: 29
Posts: 288
|
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;
}
}
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;
}
}
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);
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;
}
}
To return a string value: Code:
string strValue = Tools.GetSessionObject<string>("MySessionObject");
Code:
int intValue = Tools.GetSessionObject<int>("MySessionObject");
Code:
double dblValue = Tools.GetSessionObject<double>("MySessionObject");
Code:
MyClass[] objValue = Tools.GetSessionObject<MyClass[]>("MySessionObject");
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");
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|