how to set default values in instantiating a generic List<>

  • Thread starter Thread starter Peted
  • Start date Start date
P

Peted

I have the following code


public enum pdfFlags
{
PFD_DRAW_TO_WINDOW,
PFD_DRAW_TO_BITMAP,
PFD_SUPPORT_GDI,
PFD_SUPPORT_OPENGL,
PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
PFD_NEED_PALETTE, PFD_NEED_SYSTEM_PALETTE,
PFD_DOUBLEBUFFER, PFD_STEREO,
PFD_SWAP_LAYER_BUFFERS
};



public List<pdfFlags> dwFlags = new List<pdfFlags>();



is there a way in this decleration line to set the list with default
values. I cant seem to find a way. I think i have a good reason for
wanting to do it here.

for example it want it to always default to a list of

pdfFlags. PFD_DRAW_TO_WINDOW
pdfFlags. PFD_DRAW_TO_BITMAP
pdfFlags. PFD_SUPPORT_OPENGL


thanks for any help

Peted
 
public List<pdfFlags> dwFlags = new List<pdfFlags>();

is there a way in this decleration line to set the list with default
values.
[...]
for example it want it to always default to a list of

pdfFlags.PFD_DRAW_TO_WINDOW
pdfFlags.PFD_DRAW_TO_BITMAP
pdfFlags.PFD_SUPPORT_OPENGL

In C# version 3.0 you can do:

public List<pdfFlags> dwFlags = new List<pdfFlags> {
pdfFlags.PFD_DRAW_TO_WINDOW, pdfFlags.PFD_DRAW_TO_BITMAP,
pdfFlags.PFD_SUPPORT_OPENGL };

but, alas, this is not available in earlier versions. If you are using C#
v 2.0, you are stuck with

public List<pdfFlags> dwFlags = new List<pdfFlags>();
dwFlags.Add(pdfFlags.PFD_DRAW_TO_WINDOW);
dwFlags.Add(pdfFlags.PFD_DRAW_TO_BITMAP);
dwFlags.Add(pdfFlags.PFD_SUPPORT_OPENGL);
 
From this, and your earlier post, I'm pretty sure you are making this
hard on yourself... why not just treat this as a [Flags] enum and "or"
them together? Given your previous comments about converting to an
int, it seems clear that this is actually bitwise data...

Maybe I'm missing something...

See previous post for a full example of a [Flags] enum, with that
property as the value. You could then just use:

PfdFlags dwFlags = PfdFlags.DrawToWindow | PfdFlags.SupportGdi;
(or whatever)

but with proper handling of the enum as a bitwise entity. And with a
custom editor (as previous) it can be a breeze to use.

I know that this doesn't answer your question with lists, but I really
don't think that a list is the right answer here... for instance, what
would it mean if somebody included support_opengl twice... three
times? If this is a set of flags that are either there or not, then an
enum is a good choice. If they overflow the int, then perhaps split
into 2 enums...

Marc
 
Hi,

Marc Gravell said:
From this, and your earlier post, I'm pretty sure you are making this hard
on yourself... why not just treat this as a [Flags] enum and "or" them
together? Given your previous comments about converting to an int, it
seems clear that this is actually bitwise data...

Maybe I'm missing something...

I think the same, it's better to use a Flag enum.
As a matter of fact it struck me as weird having a list of enum.
 
use this
public List<pdfFlags> dwFlags = new List<pdfFlags>();
dwFlags.Add(pdfFlags.PFD_DRAW_TO_WINDOW);
dwFlags.Add(pdfFlags.PFD_DRAW_TO_BITMAP);
dwFlags.Add(pdfFlags.PFD_SUPPORT_OPENGL);

otherwise make derived class from List<>
and initailize with ur default parameters.

Alberto Poblacion said:
public List<pdfFlags> dwFlags = new List<pdfFlags>();

is there a way in this decleration line to set the list with default
values.
[...]
for example it want it to always default to a list of

pdfFlags.PFD_DRAW_TO_WINDOW
pdfFlags.PFD_DRAW_TO_BITMAP
pdfFlags.PFD_SUPPORT_OPENGL

In C# version 3.0 you can do:

public List<pdfFlags> dwFlags = new List<pdfFlags> {
pdfFlags.PFD_DRAW_TO_WINDOW, pdfFlags.PFD_DRAW_TO_BITMAP,
pdfFlags.PFD_SUPPORT_OPENGL };

but, alas, this is not available in earlier versions. If you are using C#
v 2.0, you are stuck with

public List<pdfFlags> dwFlags = new List<pdfFlags>();
dwFlags.Add(pdfFlags.PFD_DRAW_TO_WINDOW);
dwFlags.Add(pdfFlags.PFD_DRAW_TO_BITMAP);
dwFlags.Add(pdfFlags.PFD_SUPPORT_OPENGL);
 
Hi use first u need to define ur own class deriverd from List<>
like this
public enum pdfFlags
{
PFD_DRAW_TO_WINDOW,
PFD_DRAW_TO_BITMAP,
PFD_SUPPORT_GDI,
PFD_SUPPORT_OPENGL,
PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
PFD_NEED_PALETTE, PFD_NEED_SYSTEM_PALETTE,
PFD_DOUBLEBUFFER, PFD_STEREO,
PFD_SWAP_LAYER_BUFFERS
}

class defaultpdf<T> : List<T>
{
public defaultpdf(T t1,T t2,T t3)
{
this.Add(t1);
this.Add(t2);
this.Add(t3);
}
}


now use ur class as
defaultpdf<pdfFlags> pdf = new
defaultpdf<pdfFlags>(pdfFlags.PFD_DRAW_TO_BITMAP,pdfFlags.PFD_DRAW_TO_WINDOW,pdfFlags.PFD_SUPPORT_OPENGL);


by default it will initialize ant varaile with 3 count and given by ur
values in constructer.
hope it wiill help u
 
Hi Marc,

you are correct, this is a better way especially with the checkedlist
box to make the selection.

Its not a solution that would have accured to me, i spent a lot of
time looking at any number of other other options that all had one
flaw or another.

Iunderstand most of what is happening in this code, but some o what is
done in creating the checked listbox control is obscure to me,
especially in the adding of events to the button click.

I was hoping you would have time to add comments to clear up exactly
what is happening in the code.

The control is the simpleopenglcontrol part of the opengl mono
bindings of the TAO framework.

thanks for any help

Peted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top