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

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

P: 4
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
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
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.