Click here to Skip to main content
15,896,063 members

Making a class/function that works like 'class(type)' in a class library

Henry Hunt asked:

Open original thread
OK, this may be hard to explain so forgive me if you don't understand any of it. I am porting some of my functions that were previously in classes in my main application into a class library so I can use them in any projects. I have literally just started this so the code might not be very complete but I have this so far:
C#
namespace WeatherLink
{
    public class DataDownloader
    {
        public class MetOffice
        {
            public DataType DataType;
            public bool CacheData;
            public string CacheLocation;
            public string ApiKey;

            public string GetData()
            {
                string resource = "";

                if (DataType.Type == "VisibleObservation")
                {
                    string sourcePath = string.Format(Path.Combine(@"C:\Users\Henry\AppData\Local\WeatherLink\temp\runtime_cache\visibleObservation"));

                    if (!File.Exists(sourcePath))
                    {
                        string sourceImage = string.Format("http://datapoint.metoffice.gov.uk/public/data/layer/wxobs/SATELLITE_Visible_N_Section/png?TIME={0}Z&key={1}");
                        System.Drawing.Bitmap image = new Bitmap(Image.FromStream(System.Net.HttpWebRequest.Create(sourceImage).GetResponse().GetResponseStream()));
                        Bitmap bitmap = new Bitmap(640, 852);
                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.DrawImage(image, new Point[] { new Point(0, 0), new Point(650, 0), new Point(0, 852) });
                            bitmap.Save(sourcePath);
                            resource = sourcePath;
                        }
                    }
                    else { resource = sourcePath; }
                    return resource;
                }

                return resource;
            }
        }

        public class EuMetSat
        {

        }
    }

    public class DataType
    {
        public string Type { get; set; }
    }
}

...and I call it like this:
C#
DataDownloader.MetOffice MetOffice = new DataDownloader.MetOffice();
DataDownloader.EuMetSat EuMetSat = new DataDownloader.EuMetSat();

I want to know how I can change my code in the library so that I can call a DataDownloader like this:
C#
DataDownloader dd = new DataDownloader(MetOffice);
DataDownloader dd = new DataDownloader(EuMetSat);

...So instead of calling it using all the levels of the namespace I want to call the DataDownloader and specify its type (either MetOffice or EuMetSat). How do I do this? Would I have to use methods instead of classes for the 2 types? Like when you initiate most variables, as an example an XmlDocument has brackets after it (new XmlDocument()) where you could specify something for it. How would I do this in my code (new DataDownloader(MetOffice))? I hope you understand what I am trying to do. Thank You
Tags: C#

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900