How to set a property whose value is an enum

258 views Asked by At

I'm new to C# and the .NET Framework. I'm building a small console app to "block" URLs from Bing's index because they were accidentally indexed. I'm using the Bing Webmaster API to do this.

I do not understand how to set two properties of the BlockedUrl object (EntityType and RequestType). The BlockedUrl object is passed to AddBlockedUrl when sending the block request.

Setting the property values for Url, Date, and DaysToExpire makes sense - they're given string, DateTime, and DaysToExpire values respectively as their signatures indicate.

Going by the signature of EntityType:

public BlockedUrl.BlockedUrlEntityType EntityType { get; set; }

I don't understand BlockedUrl.BlockedUrlEntityType or how I would work with it. The RequestType property is similar.

My current code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "https://url/dir/path/";
            var api = new WebmasterApi.WebmasterApiClient();
            var blockedURLObj = new WebmasterApi.BlockedUrl();
            blockedURLObj.Url = url;
            blockedURLObj.Date = new DateTime(2018, 5, 8, 8, 00, 00);
            blockedURLObj.DaysToExpire = 90;
            blockedURLObj.EntityType = "Directory"; //error: "Cannot implicitly convert type 'string' to ConsoleApp1.WebmasterApi.BlockedUrl.BlockedUrlEntityType"
            blockedURLObj.RequestType = "FullRemoval"; //error: "Cannot implicitly convert type 'string' to ConsoleApp1.WebmasterApi.BlockedUrl.BlockedUrlRequestType"

        try
        {
            api.AddBlockedUrl(url, blockedURLObj);
            Console.WriteLine("Success!");
            Console.ReadLine(); 
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
            Console.ReadLine();
        }
    }
} 
3

There are 3 answers

0
Anil On BEST ANSWER

The EntityType property should be set using an enumeration value as dictated here: https://msdn.microsoft.com/en-us/library/hh969362.aspx

The RequestType proeprty should be set using an enumeration value as dictated here: https://msdn.microsoft.com/en-us/library/hh969383.aspx

For example:

blockedURLObj.EntityType = BlockedUrl.BlockedUrlEntityType.Directory;
blockedURLObj.RequestType = BlockedUrl.BlockedUrlRequestType.FullRemoval;
0
Curly Brace On

BlockedUrl.BlockedUrlEntityType is a enum

So you need something like

blockedURLObj.EntityType = BlockedUrl.BlockedUrlEntityType.Directory;
blockedURLObj.RequestType = BlockedUrl.BlockedUrlRequestType.FullRemoval;

More about enums here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum

0
jmesolomon On

So the EntityType and RequestType look like enums. you can set it via blockedURLObj.RequestType = WebmasterApi.BlockedUrl.BlockedUrlRequestType.FullRemoval; and blockedURLObj.EntityType = WebmasterApi.BlockedUrl.BlockedUrlEntityType.Directory;