469,626 Members | 1,062 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

C#, interface indexer implementation by struct (public vs private and explicit)

Hola!

I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain.

Please help!

Code Snippet 1:



// indexers in interface, public implementation by struct

using System;

interface MyInterface
{
int this[int index]
{
get;
set;
}
}

struct MyStruct : MyInterface
{
int[] array;

public int l;

public bool error;

public MyStruct(int size) : this()
{
array = new int[size];
l = size;
}

public int this[int index]
{
get
{
if(ok(index))
{
error = false;
return array[index];
}
else
{
error = true;
return 0;
}
}

set
{
if(ok(index))
{
array[index] = value;
error = false;
}
else
error = true;
}
}

bool ok(int index)
{
if((index>=0) && (index<l))
return true;
else
return false;
}
}

struct MainStruct
{
static void Main()
{
MyStruct ms = new MyStruct(5);

int x;

Console.WriteLine("Fail quietly: ");
for(int i=0; i<(ms.l*2); i++)
ms = i;

for(int i=0; i<(ms.l*2); i++)
{
x = ms;
if(x!=-1)
Console.Write(x + " ");
}


Console.WriteLine("\nFail with error reports: ");
for(int i=0; i<(ms.l*2); i++)
{
ms = i;
if(ms.error) // *Note: Works well
Console.WriteLine("ms[ " + i + " ] out-of-bounds");
}

for(int i=0; i<(ms.l*2); i++)
{
x = ms;
if(!ms.error) // *Note: Works well
Console.Write(x + " ");
else
Console.WriteLine("ms[ " + i + " ] out-of-bounds");
}
}
}



Code Snippet 2:



// indexers in interface, private and explicit implementation by struct

using System;

interface MyInterface
{
int this[int index]
{
get;
set;
}
}

struct MyStruct : MyInterface
{
int[] array;

public int l;

public bool error;

public MyStruct(int size) : this()
{
array = new int[size];
l = size;
}

int MyInterface.this[int index]
{
get
{
if(ok(index))
{
error = false;
return array[index];
}
else
{
error = true;
return 0;
}
}

set
{
if(ok(index))
{
array[index] = value;
error = false;
}
else
error = true;
}
}

bool ok(int index)
{
if((index>=0) && (index<l))
return true;
else
return false;
}
}

struct MainStruct
{
static void Main()
{
MyStruct ms = new MyStruct(5);

int x;

MyInterface mi = (MyInterface)ms;

Console.WriteLine("Fail quietly: ");
for(int i=0; i<(ms.l*2); i++)
mi = i;

for(int i=0; i<(ms.l*2); i++)
{
x = mi;
if(x!=-1)
Console.Write(x + " ");
}


Console.WriteLine("\nFail with error reports: ");
for(int i=0; i<(ms.l*2); i++)
{
mi = i;
if(ms.error) // *Note: Doesn't work
Console.WriteLine("mi[ " + i + " ] out-of-bounds");
}

for(int i=0; i<(ms.l*2); i++)
{
x = mi;
if(!ms.error) // *Note: Doesn't work
Console.Write(x + " ");
else
Console.WriteLine("mi[ " + i + " ] out-of-bounds");
}
}
}


Thanks
Sep 7 '07 #1
1 2535
Plater
7,872 Expert 4TB
I don't believe you can have dynamic allocation in a struct?
Do it with a Class
Sep 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Roy Pereira | last post: by
3 posts views Thread by Markus Dehmann | last post: by
2 posts views Thread by COLIN JACK | last post: by
4 posts views Thread by Brian | last post: by
52 posts views Thread by Ben Voigt [C++ MVP] | last post: by
10 posts views Thread by Sebastian | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.