473,548 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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.WriteLi ne("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.WriteLi ne("\nFail with error reports: ");
for(int i=0; i<(ms.l*2); i++)
{
ms = i;
if(ms.error) // *Note: Works well
Console.WriteLi ne("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.WriteLi ne("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.thi s[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.WriteLi ne("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.WriteLi ne("\nFail with error reports: ");
for(int i=0; i<(ms.l*2); i++)
{
mi = i;
if(ms.error) // *Note: Doesn't work
Console.WriteLi ne("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.WriteLi ne("mi[ " + i + " ] out-of-bounds");
}
}
}


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

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

Similar topics

4
8176
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by making my contnent dlls implement an interface created in vb6. The viewer application is bound to this interface. This way, I am able to add Content...
3
3586
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would be "public interface Value{}" in Java! class IntValue : public Value{ private: int _value; public:
12
2768
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that you are implementing in the class. Other than to resolve the problem that arises when a class implements two interfaces with the same method...
2
2504
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific...
11
1482
by: sotto | last post by:
If i have this Interface: Public Interface MyInterface Function test() As Boolean Function test(ByVal MyVar As String) As Boolean End Interface And then i make a Public Class MyOwnClass
8
5800
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they would like to support. During the code generation phase, I add an "Implements Ixxx" for each interface they select, but I've not yet figured out how...
4
7528
by: Brian | last post by:
I really want to use the IDataErrorInfo interface. I'm using it now in combination with an ErrorProvider and this seems really good. But the downside: IDataErrorInfo wants to use a string indexer on my class. No big deal, but now I have a class where I want to use my own string indexer. This is frustrating. Do I have to do something...
52
20830
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535:...
10
6920
by: Sebastian | last post by:
Hi, I'm confronted with a problem that seems not to be solvable. In general: How can I override an interface member of my base class and call the overridden method from my derived class? This is my class: class RemoteXmlDataSource : System.Web.UI.WebControls.XmlDataSource And I want to override
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7805
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
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 we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.