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

Create Method

Hello,

I have the following code:

* * MySection sect =
System.Configuration.ConfigurationManager.GetSecti on(Name) as
MySection;
* * foreach (Person p in sect.People) {
* * * * Interaction.MsgBox(p.Name);
* * }

Now I am trying to create a method that is a little bit more generic.

As result as GetSection I will get a class. This class can be of many
types (MySection, Google, ...).
All I know is that this class inherits ConfigurationElementCollection.

Then I want to loop through each item of this class and find the item
which Name is equal to a Key value I provide.
The item can be of type People (if the collection is of type
MySection), Service (if the collection if of type Google), ...

The one thing I know is that any of this classes has a property named
Name to which I will compare to Key.

I would like to create a method for this, for example Get, and then
use it:

People p = (People)Get("MySection")

or

Service s = (Service)Get("Google")

Is this possible to create this generalization?

Or should I even do this?

Thanks,
Miguel
Oct 3 '08 #1
1 1582
On Oct 3, 1:41*am, shapper <mdmo...@gmail.comwrote:
Hello,

I have the following code:

* * MySection sect =
System.Configuration.ConfigurationManager.GetSecti on(Name) as
MySection;
* * foreach (Person p in sect.People) {
* * * * Interaction.MsgBox(p.Name);
* * }

Now I am trying to create a method that is a little bit more generic.

As result as GetSection I will get a class. This class can be of many
types (MySection, Google, ...).
All I know is that this class inherits ConfigurationElementCollection.

Then I want to loop through each item of this class and find the item
which Name is equal to a Key value I provide.
The item can be of type People (if the collection is of type
MySection), Service (if the collection if of type Google), ...

The one thing I know is that any of this classes has a property named
Name to which I will compare to Key.

I would like to create a method for this, for example Get, and then
use it:

People p = (People)Get("MySection")

or

Service s = (Service)Get("Google")

Is this possible to create this generalization?

Or should I even do this?

Thanks,
Miguel
I am posting the entire code so it is better to explain:

Config

public class Config : ConfigurationSection {

[ConfigurationProperty("Google")]
public GoogleCollection Google {
get { return this["Google"] as GoogleCollection; }
}
[ConfigurationProperty("Mail")]
public MailCollection Mail {
get { return this["Mail"] as MailCollection; }
}

public static object Get(string section, ConfigType type, string
key) {
object parent = ConfigurationManager.GetSection(section);
// ?????
}

} // Config

GoogleCollection

public class GoogleCollection : ConfigurationElementCollection {

protected override ConfigurationElement CreateNewElement() {
return new Google();
}
protected override object GetElementKey(ConfigurationElement
element) {
return (element as Google).Key;
}

}

Google

public class Google : ConfigurationElement {

[ConfigurationProperty("Key", IsRequired = true)]
public string Access { get; set; }
public string Key { get; set; }
public string Type { get; set; }

}

These are configuration classes to create custom configuration section
in Web.Config.

As you can see in Config I can have many subsections, Google, Mail,
etc ...

The Web.Config looks as follows:

<MyApp>
<Google>
<add key ="AdSense" type ="Client" access = "" />
<add key ="Verify" type ="Id" access = "" />
</Google>
<Mail>
</Mail>
</MyApp>

So basically what I am trying to create is a method in Config that
given a section (Ex: MyApp) a type (Ex: ConfigType.Google) and a Key
(Ex: AdSense) I would get that part of the Web.Config.

In this case would be:

<add key ="AdSense" type ="Client" access = "" />

And this is an instance of Google.

I would like to have the get method inside Config ... this is my
problem.

Any idea?

Thanks,

Miguel
Oct 3 '08 #2

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

Similar topics

4
by: Frank Millman | last post by:
Hi all I need to generate potentially large reports from a database, and I want to offer the option of print preview before actually printing (using wxPython). I figure that the best way to...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
3
by: Tapas | last post by:
Hi, Generating a .cs file using CodeDom. It generates the class fine. But i have few queries about class generation. 1. How to create a protected member? By default it generates a private...
2
by: Just D. | last post by:
All, Do we have a simple way to Create an object on the fly knowing just an object type? The usual design-time way is to write a code something like this: CObjectType obj = new CObjectType();...
4
by: Sathyaish | last post by:
The WebRequest class implements IWebRequestCreate and hence, a method Create. This method has two other overloads, one of which is a private method. Here's how it looks: public static...
1
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
3
by: =?Utf-8?B?RGFiYmxlck5M?= | last post by:
I have the following Methods: public SecondResult DoSomething() { FirstResult firstResult=GetFirstResult(); SecondResult secondResult=GetSecondResult(firstResult); return secondResult; } ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
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,...

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.