473,378 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Getting type of ref parameter

hi there,

i try to do a little generic style of programing in c#. the situation
is like this:

class MyObj1
{
}

class MyObj2 : MyClass1
{
}

......

void EnsureObjectExists(ref MyObj1 myObj)
{
if (myObj == null)
{
Type myObjType = typeof(myObj);
myObj = myObjType.CreateInstance();
}
}

....

MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

Cheers,
Marc

Jul 5 '06 #1
8 1629
void EnsureObjectExists<T>(ref T obj) where T : new()
{
if (obj== null) obj= new T();
}

Note that the compiler can infer the <Tin this case, so:

SomeClass test = null;
EnsureObjectExists(ref test);

should work fine.

Marc
Jul 5 '06 #2
ma**@idev.ch wrote:
hi there,

i try to do a little generic style of programing in c#. the situation
is like this:

class MyObj1
{
}

class MyObj2 : MyClass1
{
}

.....

void EnsureObjectExists(ref MyObj1 myObj)
{
if (myObj == null)
{
Type myObjType = typeof(myObj);
myObj = myObjType.CreateInstance();
}
}

...

MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

Cheers,
Marc
Hi Marc,

With your current implementation, it's not possible to infer the type of
object within the EnsureObjectExists method, if you're just passing in
null, then the method has no way of knowing what type it is.

You could alter your method to use generics, if you are using .NET 2.0:

///
private void EnsureObjectExists<T( ref T myObj ) where T : MyObj1, new()
{
if ( myObj == null )
myObj = new T();
}
///

Then use it as follows:

///
MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists<MyObj1>( ref obj1 );
EnsureObjectExists<MyObj2>( ref obj2 );
///

--
Hope this helps,
Tom Spink
Jul 5 '06 #3
Hi,

This will not work:
Type myObjType = typeof(myObj);

It will always return MyObj1 cause typeof is evaluated at compile time.
What you need to do is
if (myObj == null)
{
Type myObjType = myObj.GetType();

This will be evaluated at runtime and will return the correct type.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<ma**@idev.chwrote in message
news:11**********************@a14g2000cwb.googlegr oups.com...
hi there,

i try to do a little generic style of programing in c#. the situation
is like this:

class MyObj1
{
}

class MyObj2 : MyClass1
{
}

.....

void EnsureObjectExists(ref MyObj1 myObj)
{
if (myObj == null)
{
Type myObjType = typeof(myObj);
myObj = myObjType.CreateInstance();
}
}

...

MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

Cheers,
Marc

Jul 5 '06 #4
"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>
wrote:
Hi,

This will not work:
Type myObjType = typeof(myObj);

It will always return MyObj1 cause typeof is evaluated at compile time.
What you need to do is
if (myObj == null)
{
Type myObjType = myObj.GetType();

This will be evaluated at runtime and will return the correct type.
Hi Ignacio,
Type myObjType = myObj.GetType();
You can't do that, because myObj is null. You'll get a
NullReferenceException.

--
Hope this helps,
Tom Spink
Jul 5 '06 #5
erm... no, it will throw an exception every time myObj == null

Marc
Jul 5 '06 #6
ma**@idev.ch wrote:
void EnsureObjectExists(ref MyObj1 myObj)
MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);
You know these need to be "EnsureObjectExists(ref obj1)", right?
Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)
With generics, yes:

---8<---
static void Create<T>(ref T value)
where T : class
{
// typeof(T) is the System.Type
if (value == null)
// choose your constructor and assign
}
--->8---

Alternatively, if you only need the default constructor (no parameters):

---8<---
static void Create<T>(ref T value)
where T : class, new()
{
if (value == null)
value = new T();
}
--->8---

-- Barry

--
http://barrkel.blogspot.com/
Jul 5 '06 #7
Hi,
>
>Type myObjType = myObj.GetType();

You can't do that, because myObj is null. You'll get a
NullReferenceException.

Ooops, my mistake :)

I need coffee I did not see the == , I assume it was != (which would have
no sense at all in the first place )

Going to prepare myself a hot strong cuban coffee :)

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 5 '06 #8
hey thanks all of you.
the one with the generic seems to be what i am looking. my luck i am
using 2.0 :-)))

cheers, marc

Jul 5 '06 #9

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

Similar topics

0
by: Willoughby Bridge | last post by:
Hi - Having trouble getting a multipart series of forms to move to the next form. The problem line is: <form method="post" enctype="multipart/form-data" action="Data_Form1.php"> If the...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
10
by: Peter Afonin | last post by:
Hello, I have a simple client-side form that is checking the domain availability on the domain registrar's server: <FORM action="https://www.webnames.ru/scripts/RegTimeSRS.pl" method="post">...
5
by: asianmuscle | last post by:
I am trying to learn RAII and some template techniques by writer a smarter pointer class to manage the resource management. Since I find that a lot of the resource management is kinda the same, I...
0
by: Steve | last post by:
Hi All, I have a Python script that uses SOAPpy and I'm outputting all of the methods and info about the parameters... I'm having trouble getting information out of the __init__ parameter. ...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
22
by: Sri | last post by:
All Recenetly our shop migrated to DB2 V8 from V7. We are in IBM System Level: z/OS 1.6.1 @ RSU 0702. Processor : IBM 2064-1C7 (z/900) # 1B89 Mode: 64-bit One of my application is facing...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
4
by: jehugaleahsa | last post by:
Hello: Say I were to query the schema tables from a database management system. Say I got a DataType field that held the string 'VARCHAR2'. Now, I know that VARCHAR2 associates to System.String....
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.