By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

Simple code sample wanted: enum as method parameter

P: n/a

I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET
2005 Intellisense will pop up the members of an enum as a selection list if
you are using the enum as a method parameter. This sounds very handy.

I would really appreciate it if someone could give me a basic C# code sample
showing how this would be set up, both the enum and the method.

So let's say you're building a method, "ReturnPageType(...)", and you want
an enumerated list of possibilities to be displayed in Intellisense: "html"
"pdf" "mht".

What would the skeletal code be? I've tried a bunch of things, but it isn't
working, and for some reason I can't find a sample in literature on- or
offline.

Thank you,
-KF
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
<ke*****@u.washington.edu> wrote:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET
2005 Intellisense will pop up the members of an enum as a selection list if
you are using the enum as a method parameter. This sounds very handy.

I would really appreciate it if someone could give me a basic C# code sample
showing how this would be set up, both the enum and the method.

So let's say you're building a method, "ReturnPageType(...)", and you want
an enumerated list of possibilities to be displayed in Intellisense: "html"
"pdf" "mht".

What would the skeletal code be? I've tried a bunch of things, but it isn't
working, and for some reason I can't find a sample in literature on- or
offline.


Copy this into a console app:

using System;

enum Foo
{
FirstValue,
SecondValue,
ThirdValue
}

class Program
{
static void Main(string[] args)
{
DoSomething
}

static void DoSomething(Foo foo)
{
}
}

and put your cursor just after DoSomething. Hit '(' and you'll be
presented with intellisense which suggests "Foo". Hit '.' and it'll
fill in Foo for you, and present you with FirstValue, SecondValue,
ThirdValue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
<ke*****@u.washington.edu> wrote:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET
2005 Intellisense will pop up the members of an enum as a selection list if
you are using the enum as a method parameter. This sounds very handy.

I would really appreciate it if someone could give me a basic C# code sample
showing how this would be set up, both the enum and the method.

So let's say you're building a method, "ReturnPageType(...)", and you want
an enumerated list of possibilities to be displayed in Intellisense: "html"
"pdf" "mht".

What would the skeletal code be? I've tried a bunch of things, but it isn't
working, and for some reason I can't find a sample in literature on- or
offline.


Copy this into a console app:

using System;

enum Foo
{
FirstValue,
SecondValue,
ThirdValue
}

class Program
{
static void Main(string[] args)
{
DoSomething
}

static void DoSomething(Foo foo)
{
}
}

and put your cursor just after DoSomething. Hit '(' and you'll be
presented with intellisense which suggests "Foo". Hit '.' and it'll
fill in Foo for you, and present you with FirstValue, SecondValue,
ThirdValue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

P: n/a
Thanks for the help Jon. I'm going to expose an abundance of ignorance,
here, but this is how we learn...

Following your skeleton code below as a template, I was able to make the
following code work just fine when I declared a static method. My
understanding of static methods is that they can be invoked without
instantiating an object, and Intellisense's behavior on the code below
seemed to work great
:
enum PageReturnType
{
txt,
html,
pdf,
mht
}

public class Scraper
{

static void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
//whatever
}

public void MyTest()
{
Scraper.ArchiveRemotePageAsPDF(PageReturnType.
}

As I said, this worked fine. When I try to make my code so that it can work
against object instances, using public methods rather than static, I no
longer get a method list:

enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
myScraper = new Scraper;
Scraper.
Scraper.ArchiveRemotePageAsPDF(PageReturnType.mht) ;

}
}

I'm doing something dumb and missing something fundamental. What is it?

Thanks,
-KF
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<ke*****@u.washington.edu> wrote:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET
2005 Intellisense will pop up the members of an enum as a selection list
if
you are using the enum as a method parameter. This sounds very handy.

I would really appreciate it if someone could give me a basic C# code
sample
showing how this would be set up, both the enum and the method.

So let's say you're building a method, "ReturnPageType(...)", and you
want
an enumerated list of possibilities to be displayed in Intellisense:
"html"
"pdf" "mht".

What would the skeletal code be? I've tried a bunch of things, but it
isn't
working, and for some reason I can't find a sample in literature on- or
offline.


Copy this into a console app:

using System;

enum Foo
{
FirstValue,
SecondValue,
ThirdValue
}

class Program
{
static void Main(string[] args)
{
DoSomething
}

static void DoSomething(Foo foo)
{
}
}

and put your cursor just after DoSomething. Hit '(' and you'll be
presented with intellisense which suggests "Foo". Hit '.' and it'll
fill in Foo for you, and present you with FirstValue, SecondValue,
ThirdValue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4

P: n/a
Hi Kefine,
the difference between a static and non static method is that the static
method does not belong to a single instance of a class it belongs at the
class level, meaning if you have a class called MyClass then you could call a
static method by saying:

MyClass.MyStaticMethod();

if the method is not static then you have to call the method on a specific
instance of the type like:

MyClass objX = new MyClass();
objX.MyNonStaticMethod();
So in your code you will have to change:

enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
myScraper = new Scraper;
Scraper.
Scraper.ArchiveRemotePageAsPDF(PageReturnType.mht) ;

}
}

into:


enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
//create an object instance of type Scraper
Scraper myScraper = new Scraper();

//on the specific instance you can now call the method because it
//is no longer static you can not reference it at a class level
myScraper.ArchiveRemotePageAsPDF(PageReturnType.mh t);
}
}
"ke*****@u.washington.edu" wrote:
Thanks for the help Jon. I'm going to expose an abundance of ignorance,
here, but this is how we learn...

Following your skeleton code below as a template, I was able to make the
following code work just fine when I declared a static method. My
understanding of static methods is that they can be invoked without
instantiating an object, and Intellisense's behavior on the code below
seemed to work great
:
enum PageReturnType
{
txt,
html,
pdf,
mht
}

public class Scraper
{

static void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
//whatever
}

public void MyTest()
{
Scraper.ArchiveRemotePageAsPDF(PageReturnType.
}

As I said, this worked fine. When I try to make my code so that it can work
against object instances, using public methods rather than static, I no
longer get a method list:

enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
myScraper = new Scraper;
Scraper.
Scraper.ArchiveRemotePageAsPDF(PageReturnType.mht) ;

}
}

I'm doing something dumb and missing something fundamental. What is it?

Thanks,
-KF
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<ke*****@u.washington.edu> wrote:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET
2005 Intellisense will pop up the members of an enum as a selection list
if
you are using the enum as a method parameter. This sounds very handy.

I would really appreciate it if someone could give me a basic C# code
sample
showing how this would be set up, both the enum and the method.

So let's say you're building a method, "ReturnPageType(...)", and you
want
an enumerated list of possibilities to be displayed in Intellisense:
"html"
"pdf" "mht".

What would the skeletal code be? I've tried a bunch of things, but it
isn't
working, and for some reason I can't find a sample in literature on- or
offline.


Copy this into a console app:

using System;

enum Foo
{
FirstValue,
SecondValue,
ThirdValue
}

class Program
{
static void Main(string[] args)
{
DoSomething
}

static void DoSomething(Foo foo)
{
}
}

and put your cursor just after DoSomething. Hit '(' and you'll be
presented with intellisense which suggests "Foo". Hit '.' and it'll
fill in Foo for you, and present you with FirstValue, SecondValue,
ThirdValue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 17 '05 #5

P: n/a
Super, thanks, uber Bene. Many thanks to you Mark and to Jon as well. Much
appreciated.

-KF

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:CD**********************************@microsof t.com...
Hi Kefine,
the difference between a static and non static method is that the static
method does not belong to a single instance of a class it belongs at the
class level, meaning if you have a class called MyClass then you could
call a
static method by saying:

MyClass.MyStaticMethod();

if the method is not static then you have to call the method on a specific
instance of the type like:

MyClass objX = new MyClass();
objX.MyNonStaticMethod();
So in your code you will have to change:

enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
myScraper = new Scraper;
Scraper.
Scraper.ArchiveRemotePageAsPDF(PageReturnType.mht) ;

}
}

into:


enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
//create an object instance of type Scraper
Scraper myScraper = new Scraper();

//on the specific instance you can now call the method because it
//is no longer static you can not reference it at a class level
myScraper.ArchiveRemotePageAsPDF(PageReturnType.mh t);
}
}
"ke*****@u.washington.edu" wrote:
Thanks for the help Jon. I'm going to expose an abundance of ignorance,
here, but this is how we learn...

Following your skeleton code below as a template, I was able to make the
following code work just fine when I declared a static method. My
understanding of static methods is that they can be invoked without
instantiating an object, and Intellisense's behavior on the code below
seemed to work great
:
enum PageReturnType
{
txt,
html,
pdf,
mht
}

public class Scraper
{

static void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
//whatever
}

public void MyTest()
{
Scraper.ArchiveRemotePageAsPDF(PageReturnType.
}

As I said, this worked fine. When I try to make my code so that it can
work
against object instances, using public methods rather than static, I no
longer get a method list:

enum PageReturnType
{
txt,
html,
pdf,
mht
}
public class Scraper
{
public void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
}
public void MyTest()
{
myScraper = new Scraper;
Scraper.
Scraper.ArchiveRemotePageAsPDF(PageReturnType.mht) ;

}
}

I'm doing something dumb and missing something fundamental. What is it?

Thanks,
-KF
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
> <ke*****@u.washington.edu> wrote:
>> I'm new to VS.NET, C#, and the enumerated datatype. I'm told that
>> VS.NET
>> 2005 Intellisense will pop up the members of an enum as a selection
>> list
>> if
>> you are using the enum as a method parameter. This sounds very handy.
>>
>> I would really appreciate it if someone could give me a basic C# code
>> sample
>> showing how this would be set up, both the enum and the method.
>>
>> So let's say you're building a method, "ReturnPageType(...)", and you
>> want
>> an enumerated list of possibilities to be displayed in Intellisense:
>> "html"
>> "pdf" "mht".
>>
>> What would the skeletal code be? I've tried a bunch of things, but it
>> isn't
>> working, and for some reason I can't find a sample in literature on-
>> or
>> offline.
>
> Copy this into a console app:
>
> using System;
>
> enum Foo
> {
> FirstValue,
> SecondValue,
> ThirdValue
> }
>
> class Program
> {
> static void Main(string[] args)
> {
> DoSomething
> }
>
> static void DoSomething(Foo foo)
> {
> }
> }
>
> and put your cursor just after DoSomething. Hit '(' and you'll be
> presented with intellisense which suggests "Foo". Hit '.' and it'll
> fill in Foo for you, and present you with FirstValue, SecondValue,
> ThirdValue.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


Nov 17 '05 #6

P: n/a
<ke*****@u.washington.edu> wrote:
Thanks for the help Jon. I'm going to expose an abundance of ignorance,
here, but this is how we learn...

Following your skeleton code below as a template, I was able to make the
following code work just fine when I declared a static method. My
understanding of static methods is that they can be invoked without
instantiating an object, and Intellisense's behavior on the code below
seemed to work great
:
enum PageReturnType
{
txt,
html,
pdf,
mht
}

public class Scraper
{

static void ArchiveRemotePageAsPDF(PageReturnType pagereturntype)
{
//whatever
}

public void MyTest()
{
Scraper.ArchiveRemotePageAsPDF(PageReturnType.
}

As I said, this worked fine. When I try to make my code so that it can work
against object instances, using public methods rather than static, I no
longer get a method list:


<snip>

Well, you're trying to call an instance method (ArchiveRemotePageAsPDF)
as if it were a static method. Try

myScraper. (or this.)

instead of

Scraper.

and you should get a list.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.