Showing posts with label classes. Show all posts
Showing posts with label classes. Show all posts

Saturday, March 31, 2012

String to a Class name

Hi,

I've got some classes to access different types of databases and I have a string variable that define the type of db I want to access. What I want to do if is pssible use the value of this variable to access the class. I have the following code now.


switch (dbType)
{
case "MSSQL": return MSSQL.msSQLDataSet(sqlString);
case "ACCESS": return ACCESS.msSQLDataSet(sqlString);
case "ORACLE": return ORACLE.msSQLDataSet(sqlString);
case "MYSQL": return MYSQL.msSQLDataSet(sqlString);

default: return null;
}

I want to do something like


SomekindOfTransformation(dbType).msSQLDataSet(sqlString)

instead of using theswitch statement

Thanks,Hi Arty,

You can achieve it using Object.GetType and Type.InvokeMember. VisitObject.GetType andType.InvokeMember for complete details.

Here is a sample code:

using System;
using System.Reflection;

// This sample class has a field, constructor, method, and property.
class MyType
{
Int32 myField;
public MyType(ref Int32 x) {x *= 5;}
public override String ToString() {return myField.ToString();}
public Int32 MyProp
{
get {return myField;}
set
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", value, "value must be > 0");
myField = value;
}
}
}

class MyApp
{
static void Main()
{
Type t = typeof(MyType);
// Create an instance of a type.
Object[] args = new Object[] {8};
Console.WriteLine("The value of x before the constructor is called is {0}.", args[0]);
Object obj = t.InvokeMember(null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
Console.WriteLine("Type: " + obj.GetType().ToString());
Console.WriteLine("The value of x after the constructor returns is {0}.", args[0]);

// Read and write to a field.
t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetField, null, obj, new Object[] {5});
Int32 v = (Int32) t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetField, null, obj, null);
Console.WriteLine("myField: " + v);

// Call a method.
String s = (String) t.InvokeMember("ToString",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
Console.WriteLine("ToString: " + s);

// Read and write a property. First, attempt to assign an
// invalid value; then assign a valid value; finally, get
// the value.
try
{
// Assign the value zero to MyProp. The Property Set
// throws an exception, because zero is an invalid value.
// InvokeMember catches the exception, and throws
// TargetInvocationException. To discover the real cause
// you must catch TargetInvocationException and examine
// the inner exception.
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {0});
}
catch (TargetInvocationException e)
{
// If the property assignment failed for some unexpected
// reason, rethrow the TargetInvocationException.
if (e.InnerException.GetType() !=
typeof(ArgumentOutOfRangeException))
throw;
Console.WriteLine("An invalid value was assigned to MyProp.");
}
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {2});
v = (Int32) t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty, null, obj, null);
Console.WriteLine("MyProp: " + v);
}
}

Hope this helps,
Appan
Thank you Appana, that helped me a lot.

Tuesday, March 13, 2012

Struggling - 'static' vs 'void' etc?

I am really struggling to conceptually understand classes in C# especially
the use of the strange keywords 'static' 'void' and 'override'...
Is there a 'idiots' way of understanding these concepts broadly before even
touching a line of code...
Thanks
JasonGoogle: "C# Class Design"
WROX "C# Class Design - Coding Effective Classes Handbook" is what you need.
<%= Clinton Gallagher
METROmilwaukee "Regional Information Services"
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
> I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
even
> touching a line of code...
>
> Thanks
> Jason
>
Static = non-object
Void = a method that does not return a value
Override = when you decide to discard the origins and implenet your own.
The better definitions can be viewed here:
http://msdn.microsoft.com/library/d...keywords_pg.asp
John
<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
>I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
> even
> touching a line of code...
>
> Thanks
> Jason
>
This should help get you started.
http://www.geocities.com/jeff_louie/oop.htm
Regards,
Jeff
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!
Thanks guys...!
<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
> I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
even
> touching a line of code...
>
> Thanks
> Jason
>

Struggling - static vs void etc?

I am really struggling to conceptually understand classes in C# especially
the use of the strange keywords 'static' 'void' and 'override'...

Is there a 'idiots' way of understanding these concepts broadly before even
touching a line of code...

Thanks
JasonGoogle: "C# Class Design"
WROX "C# Class Design - Coding Effective Classes Handbook" is what you need.

--
<%= Clinton Gallagher
METROmilwaukee "Regional Information Services"
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/

<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
> I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
even
> touching a line of code...
>
> Thanks
> Jason
Static = non-object
Void = a method that does not return a value
Override = when you decide to discard the origins and implenet your own.

The better definitions can be viewed here:
http://msdn.microsoft.com/library/d...keywords_pg.asp

John

<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
>I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
> even
> touching a line of code...
>
> Thanks
> Jason
This should help get you started.

http://www.geocities.com/jeff_louie/oop.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Thanks guys...!
<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
> I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
even
> touching a line of code...
>
> Thanks
> Jason
I found this sample chapter on C# extremly helpful...as a newbie I have
been searching for a 'magic' way to instantly digesting the 'broad' concepts
in C# this tutorial helped me a lot:

http://www.joegrip.com/csharp-course.html (see flash C#)

- Jason
<jason@.catamaranco.com> wrote in message
news:eawJ2zs6EHA.1260@.TK2MSFTNGP12.phx.gbl...
> I am really struggling to conceptually understand classes in C# especially
> the use of the strange keywords 'static' 'void' and 'override'...
> Is there a 'idiots' way of understanding these concepts broadly before
even
> touching a line of code...
>
> Thanks
> Jason