473,508 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple code sample wanted: enum as method parameter


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
6 1927
<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
<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
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
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
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11433
by: Greg B | last post by:
Well since getopt() doesn't seem to be compatible with Windows, and the free implementation of it for Windows that I found still had some annoying restrictions, I thought I'd whip up a simple...
2
5677
by: msnews.microsoft.com | last post by:
How do I get Intellisense to open a dropdown list box for a method's parameters when the parameter is an ENUM? public class MyClass { public enum IDkind { PersonID, EntityID, PlaceID
16
82736
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
31
3568
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
0
345
by: | last post by:
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....
0
1290
by: Pietje puk | last post by:
Hello, Since im quite new to ASP.NET i wanted to ask you folks what the best way is to create a WebForm for modifying 1 field from a record. The manipulation of this field can be done by using...
3
1073
by: James | last post by:
I'm making a very simple slideshow application. The issue I'm facing now is the application (by design) has no menus or max/min/close icons. It's basically just a rectangle as far as the end user...
232
13065
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
14
1591
by: =?Utf-8?B?R3JlZ2cgV2Fsa2Vy?= | last post by:
Hopefully someone can set me straight on this issue. I was testing some code where I have a method that is being overriden. The method overrides were working fine until I passed a literal 0 (int...
0
7233
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
7342
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
7410
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...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.