473,388 Members | 1,377 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,388 software developers and data experts.

making an object's or method's properties variable

Hi,

Is there a way to call an object or method, but use a variable to represent
the method or property to be used? eg:

I want to create a regular expression, but the regexp options I want to wait
until runtime to decide which options to use.

The following attempt throws an error:

string rxo = "MultiLine";
Regex rx = new Regex( sRegex, RegexOptions.rxo);

Thanks,
Lance

Nov 15 '05 #1
2 998
Hi Lance,

In general what you want is not easy to do, interpretate a string as code
is possible using reflection but is still a very difficult subject.

in particular you case is not very difficult, as the string you are trying
to be interpreted is part of a enumeration.

You have two options for this case:
1- Use a switch statement:
switch (rxo)
{
case"MultiLine" :
}

2- You can use Enum.Parse :

RegexOptions op = (RegexOptions) Enum.Parse( typeof(RegexOptions),
"MultiLine");
In your particular example I will go for the second one.

Cheers,

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

"Lance @hotmail.com>" <lancestuff27<nospam> wrote in message
news:10*************@corp.supernews.com...
Hi,

Is there a way to call an object or method, but use a variable to represent the method or property to be used? eg:

I want to create a regular expression, but the regexp options I want to wait until runtime to decide which options to use.

The following attempt throws an error:

string rxo = "MultiLine";
Regex rx = new Regex( sRegex, RegexOptions.rxo);

Thanks,
Lance

Nov 15 '05 #2
Thanks, the second one worked a treat!

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uU**************@TK2MSFTNGP10.phx.gbl...
Hi Lance,

In general what you want is not easy to do, interpretate a string as code
is possible using reflection but is still a very difficult subject.

in particular you case is not very difficult, as the string you are trying to be interpreted is part of a enumeration.

You have two options for this case:
1- Use a switch statement:
switch (rxo)
{
case"MultiLine" :
}

2- You can use Enum.Parse :

RegexOptions op = (RegexOptions) Enum.Parse( typeof(RegexOptions),
"MultiLine");
In your particular example I will go for the second one.

Cheers,

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

"Lance @hotmail.com>" <lancestuff27<nospam> wrote in message
news:10*************@corp.supernews.com...
Hi,

Is there a way to call an object or method, but use a variable to

represent
the method or property to be used? eg:

I want to create a regular expression, but the regexp options I want to

wait
until runtime to decide which options to use.

The following attempt throws an error:

string rxo = "MultiLine";
Regex rx = new Regex( sRegex, RegexOptions.rxo);

Thanks,
Lance


Nov 15 '05 #3

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

Similar topics

7
by: Christopher Jeris | last post by:
I am relatively new to JavaScript, though not to programming, and I'm having trouble finding the idiomatic JS solution to the following problem. I have a table with (say) fields f1, f2, f3. I...
7
by: Harag | last post by:
Hi all If I create an object with the following: var ob1 = new objMyDefinedObject(); then I assign it to a new variable. var ob2 = ob1
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
1
by: Hoss | last post by:
Hello- I have the following Javascript code function Obj() { obj.squares = new Array(); } Obj.prototype.Load= function(xdoc) { var goat = "head"; xdoc.ProcessNodes("squares/square",...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.