473,568 Members | 3,014 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class reference

How can I make a method that takes a class (not an instance) as
parameter?

And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows. Forms.Form.

Thanks!

--
Erick Sasse
Nov 17 '05 #1
9 1387
You cannot. Classes by themselves do not do anything. You can only pass
instances of classes.

What is it that you want to do?
"Erick Sasse" <es***********@ nospam.yahoo.co m.br> wrote in message
news:#K******** ******@TK2MSFTN GP10.phx.gbl...
How can I make a method that takes a class (not an instance) as
parameter?

And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows. Forms.Form.

Thanks!

--
Erick Sasse

Nov 17 '05 #2
Peter Rilling wrote:
You cannot. Classes by themselves do not do anything. You can only
pass instances of classes.

What is it that you want to do?


I want to create a method that receives a form class, create a instance
of it, configure some properties and then show the created form.

--
Erick Sasse
Nov 17 '05 #3
Erick Sasse wrote:
How can I make a method that takes a class (not an instance) as
parameter?

And I want this parameter to accept only Windows Form classes, ie
classes that inherits System.Windows. Forms.Form.


You can't; C# is not Delphi.

What you can do is to take a Type parameter, and pass typeof(MyForm).
You can't get the compiler to only accept the typeof() Form
descendants; you'll have to do runtime type checking like

public void FormFactory(Typ e FormType)
{
if (! typeof(Form).Is AssignableFrom( FormType))
throw new ArgumentExcepti on(
"FormType is not the typeof() a Form descendant");
// yadda yadda
}

--

www.midnightbeach.com
Nov 17 '05 #4
Jon Shemitz wrote:
You can't; C# is not Delphi.


Yes, this is an interesting Delphi feature.
Maybe in a future C# version...

Thanks.

--
Erick Sasse
Nov 17 '05 #5
"Erick Sasse" <es***********@ nospam.yahoo.co m.br> a écrit dans le message de
news: uM************* @TK2MSFTNGP09.p hx.gbl...
Yes, this is an interesting Delphi feature.
Maybe in a future C# version...


You can always create your own (meta)class that emulates the functionality
of Delphi's "class of" functionality.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #6
Erick Sasse wrote:
You can't; C# is not Delphi.


Yes, this is an interesting Delphi feature.
Maybe in a future C# version...


I haven't used Delphi for a while, so I'm not sure I remember all the
aspects of the "class of" operator, but I am quite sure it should be
easily reproducible with Reflection in .NET.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #7
Joanna Carter (TeamB) wrote:
You can always create your own (meta)class that emulates the
functionality of Delphi's "class of" functionality.


Hi Joanna, glad to see you here. :)

Do you have any code sample?

Thanks!

--
Erick Sasse
Nov 17 '05 #8
"Erick Sasse" <es***********@ nospam.yahoo.co m.br> a écrit dans le message de
news: ef************* *@TK2MSFTNGP14. phx.gbl...
Hi Joanna, glad to see you here. :)
Fortunately, my forté is OO not just Delphi, so C# is an ideal language and
I find that VS is just bearable; I'd rather use the Delphi IDE though :-)
Do you have any code sample?


I wouldn't suggest you keep the "Class" classname, but here is a quick
version :

////////////////////////////////
class Class
{
class TypeNotDerivedE xception : ApplicationExce ption
{
private Type derivedType;

private Type baseType;

public TypeNotDerivedE xception(Type derivedType, Type baseType)
{
this.derivedTyp e = derivedType;
this.baseType = baseType;
}

public override string Message
{
get
{
return String.Format(" {0} does not derive from {1}",
derivedType.Nam e, baseType.Name);
}
}
}

class InterfaceNotImp lementedExcepti on : ApplicationExce ption
{
private Type implementingTyp e;

private Type interfaceType;

public InterfaceNotImp lementedExcepti on(Type implementingTyp e, Type
interfaceType)
{
this.implementi ngType = implementingTyp e;
this.interfaceT ype = interfaceType;
}

public override string Message
{
get
{
return String.Format(" {0} does not implement {1}",
implementingTyp e.Name, interfaceType.N ame);
}
}
}

private class ConstructorNotF oundException : ApplicationExce ption
{
public ConstructorNotF oundException() :
base("No constructor found for these arguments") {}
}

private class ClassMethodNotF oundException : ApplicationExce ption
{
public ClassMethodNotF oundException() :
base("No class method found for these arguments") {}
}

private Type fClassType;

public Class(Type aType)
{
fClassType = aType;
}

public Type ClassType
{
get
{
return fClassType;
}
set
{
/////////////////////
if (fClassType.IsI nterface)
{
Type t = null;

Type[] a = value.GetInterf aces();
foreach (Type i in a)
{
if (i == fClassType)
{
t = value;
break;
}
}

if (t != null)
fClassType = value;
else
throw new InterfaceNotImp lementedExcepti on(value, fClassType);
}
else
{
//////////////////
if (!(value.Equals (fClassType) || value.IsSubclas sOf(fClassType) ))
throw new TypeNotDerivedE xception(value, fClassType);
fClassType = value;
}
}
}

public object Create(object[] args)
{
Type[] argTypes = new Type[args.Length];

for (int i = 0; i < args.Length; i++)
argTypes[i] = args[i].GetType();

ConstructorInfo lConstructor =
fClassType.GetC onstructor(Bind ingFlags.Instan ce |
BindingFlags.No nPublic|
BindingFlags.Pu blic,
null, argTypes, null);
if (lConstructor == null)
throw new ConstructorNotF oundException() ;

return lConstructor.In voke(args);
}

public object Func(string funcName, object[] args, Type returnType)
{
Type[] argTypes = new Type[args.Length];

for (int i = 0; i < args.Length; i++)
argTypes[i] = args[i].GetType();

{
MethodInfo methodInfo;
Type classType = fClassType;
do
{
methodInfo = classType.GetMe thod(funcName, BindingFlags.St atic |
BindingFlags.No nPublic
| BindingFlags.Pu blic,
null, argTypes, null);
classType = classType.BaseT ype;
}
while (methodInfo == null &&
classType != typeof(Object) &&
methodInfo.Retu rnType != returnType);

if (methodInfo == null)
throw new ClassMethodNotF oundException() ;
return methodInfo.Invo ke(null, args);
}
}

protected void Proc(string procName, object[] args)
{
Type[] lArgTypes = new Type[args.Length];

for (int i = 0; i < args.Length; i++)
lArgTypes[i] = args[i].GetType();
{
MethodInfo methodinfo;
Type classType = fClassType;
do
{
methodinfo = classType.GetMe thod(procName, BindingFlags.St atic //|
BindingFlags.No nPublic
| BindingFlags.Pu blic,
null, lArgTypes, null);
classType = classType.BaseT ype;
}
while (methodinfo == null && classType != typeof(Object)) ;

if (methodinfo == null)
throw new ClassMethodNotF oundException() ;
methodinfo.Invo ke(null, args);
}
}
}
}
////////////////////////

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
Nov 17 '05 #9
Joanna Carter (TeamB) wrote:
Do you have any code sample?


I wouldn't suggest you keep the "Class" classname, but here is a quick
version :


Thanks Joanna! :)

--
Erick Sasse
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2127
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data access module that creates a large disconnected dataset from a database. I want to pass that dataset back to the calling program. And then perhaps I...
2
2159
by: Jim Red | last post by:
hello first of all, i know, there are no classes in javascript. but i will use that word for better understanding of my question. here we go. i have three classes and need a reference to the parent class. could this be done by a reference, or just by constructing the class. function Class1() { this.class2 = new Class2();
4
2406
by: Peter Speybrouck | last post by:
I have a little problem with a webservice. I reproduced the problem in the following simplified example. I just create a new C# ASP.NET webservice and a c# console application. I added a new class test to the namespace of the webservice which I try to access from the console application. //Service1.asmx.cs
13
1548
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes. In the first I new an integer and assign it to i, in the second one I don't bother. In both cases, an integer is created and I can use it. If I...
12
3009
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but...
8
3481
by: Brett Romero | last post by:
I have this situation: myEXE <needs< DerivedClass <which needs< BaseClass Meaning, myEXE is using a type defined in DerivedClass, which inherits from BaseClass. I include a reference to DerivedClass in myEXE, which gives this error: The type 'BaseAssembly.SomeClass' is defined in an assembly that is not referenced. You must add a...
14
2614
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the...
16
4147
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and therefore being able to modify its properties?
20
4016
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. ...
5
2329
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; // convert to bool reference& operator= ( bool x ); // assign from bool reference& operator= ( const reference& x ); // assign from bit...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.