|
Question 1:
Make the variable "tmparray" private to the class like this
class MyClass
{
public int MyInt1;
public int MyInt2;
public int MyInt3;
private int[] tmparray;
public int[] MyIntArray
{
get
{
tmparray = new int[3];
tmparray[0] = MyInt1;
tmparray[1] = MyInt2;
tmparray[2] = MyInt3;
return tmparray;
}
set
{
MyInt1 = value[0];
MyInt2 = value[1];
MyInt3 = value[2];
}
}
public MyClass()
{
MyIntArray = new int[3];
}
}
Question 2:
declare the property in the base class and then use the "new" keyword in the sub class on the same property.
class MyBaseClass
{
public int[] MyIntArray
{
get
{
}
set
{
}
}
public MyBaseClass()
{
MyIntArray = new int[3];
}
}
class MyClass : MyBaseClass
{
public int MyInt1;
public int MyInt2;
public int MyInt3;
private int[] tmparray;
public new int[] MyIntArray
{
get
{
tmparray = new int[3];
tmparray[0] = MyInt1;
tmparray[1] = MyInt2;
tmparray[2] = MyInt3;
return tmparray;
}
set
{
MyInt1 = value[0];
MyInt2 = value[1];
MyInt3 = value[2];
}
}
}
|
|
|
|
|
andre_swnpl wrote: Make the variable "tmparray" private to the class like this
That will have the same effect as before. It will not solve his problem as you never use tmparray so the value will never be properly reflected back into the object from which it came.
andre_swnpl wrote: declare the property in the base class and then use the "new" keyword in the sub class on the same property.
Don't do that either. Do it right by declaring the base as virtual or abstract then override it in the derived class.
|
|
|
|
|
Dewald wrote: The problem comes when I'm trying to set only certain elements in the array. I would expect that, at this point, the three int member variables would hold the values 10, 40 and 30 but the set method is never called when I allocate a value to an individual element in the array as done here.
That is correct. You only get the array. The accessing of the individual elements of the array is on the array that you have retrieved (an independent object that has no knowledge of how it was constructed or where it came from).
Also, because your array is being generated by the getter it is brand new and never stored anywhere. If you were to pass back an array that was stored as a field of the class then it would have the bahaviour you want. However, this is considered bad design as it breaks the encapsulation of the class (i.e. you can modify the contents of the class without the class knowing about it)
Dewald wrote: Does anyone know how to write the get and set methods so that the array can be accessed both as an entire array or as individual elements?
One possibility to get what you want AND keep good with OO is to write your own array class that understands where it came from and updates the source with its changes.
Dewald wrote: Can get and set metods be virtual (or something similar)?
I don't see why not.
Dewald wrote: This doesn't work. It doesn't even compile
You have provided no implementation in the base class. If you don't want to provide an implementation at that point you want to make it abstract .
Try something like this:
public abstract class A
{
public abstract int Property { get; set; }
}
class B : A
{
public override int Property
{
get
{
}
set
{
}
}
}
|
|
|
|
|
The problem with accessing the items as an array, is that you are working with an array that is a copy of the values. Changing the copy will not change the original values that the copy was created from.
You can either keep the data as an array and make properties to access the items separately, or keep the data separately and make an indexer to access them as an array.
public class ArrayStorageClass {
private int[] _data;
public ArrayStorageClass() {
_data = new int[3];
}
public int[] MyIntArray {
get { return _data; }
set {
if (value.Length != 3) throw new ApplicationException("An array with the size of three has to be used.");
_data = value;
}
}
public int MyInt1 { get { return _data[0]; } set { _data[0] = value; } }
public int MyInt2 { get { return _data[1]; } set { _data[1] = value; } }
public int MyInt3 { get { return _data[2]; } set { _data[2] = value; } }
}
ArrayStorageClass storage = new ArrayStorageClass();
storage.MyIntArray[0] = 42;
storage.MyInt2 = 1;
public class ArrayEmulatorClass {
private int _data1, _data2, _data3;
public ArrayEmulatorClass() {}
public int this[int index] {
get {
switch (index) {
case 0: return _data1;
case 1: return _data2;
case 2: return _data3;
default: throw new IndexOutOfRangeException();
}
}
set {
switch (index) {
case 0: _data1 = value;
case 1: _data2 = value;
case 2: _data3 = value;
default: throw new IndexOutOfRangeException();
}
}
}
public int MyInt1 { get { return _data1; } set { _data1 = value; } }
public int MyInt2 { get { return _data2; } set { _data2 = value; } }
public int MyInt3 { get { return _data3; } set { _data3 = value; } }
}
ArrayEmulatorClass emulator = new ArrayEmulatorClass();
emulator[0] = 42;
emulator.MyInt2 = 1;
If you want to declare the property in a base class without supplying the implementation, you can make it abstract. You also have to make the base class itself abstract, which means that you can't create instances of it. If you want to declare data in the base class to be used in the derived classes, make it protected.
public abstract class PropertyBaseClass {
protected int _data;
public abstract int MyProperty { get; set; }
}
public class SpecificPropertyClass : PropertyBaseClass {
public override int MyProperty { get { return _data; } set { _data = value; } }
}
---
single minded; short sighted; long gone;
|
|
|
|
|
Fantastic! Thanks guys, you saved me a late night!
|
|
|
|
|
int s are value types. The int that is in tmparray[0] is different from the int that is MyInt1. Read up on value types and reference types.
Colin has answered Q2 very well, so I'll just skip that.
Cheers,
Vikram.
"But nowadays, it means nothing. Features are never frozen, development keeps happening, bugs never get fixed, and documentation is something you might find on wikipedia."
- Marc Clifton on betas. Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
Hi,
we are using vs 2005, for reporting purpose we are using .rdlc file. Here i want to set a parameter field, in design mode. pls help me in this regard
Selva..
|
|
|
|
|
Hi,
Can you help me how to "synchronize" .NET DateTime.Tick and C++ time (CTime, LPSYSTEMTIME, t_time doesn't matter which one) . I need to write certain time to file (using .NET C# -> write Ticks value) and than read it in C++ == create time structure (constructor with !same! ticks or something...) . I'm using old VS 6 C++ !!!
thanx
|
|
|
|
|
I would imaging that the TickCount of DateTime does in fact syncronise to one of the time concepts in c++, but I'm not sure.
If you need to communicate between programs, why not use WM_COPYDATA, or C++/CLI ? Are you using the file to send the time, or does the file have it's own purpose ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
File has it's own purpose, it contains a lot of license data, and some of them is time structure. File is created on server(in C#) and sent to client(and red in C++)...
now I'm looking at DateTime.ToFileTime() method, which maybe resembles _FILETIME structure in C++, but those constructors... omg
|
|
|
|
|
Hello.
So this is my problems:
I have to find a way to save a class of mine to a file.
Solution:
I found serialization and I thought it might work.
But... When I tried BinarySerialization, some chars would just disapear, and I'd get an exception for "not a correct format", so BinarySerialization is OUT of the question (and I tried it in a few ways).
XmlSerialization is throwing non-stopable exceptions, I just went to the one after, so that's out of the question also.
I tried SOAP serialization, but now I came across another problem.
I need to serialize a generic collection which I built. This gives me an exception, saying that it cannot serialize a generic class. (that's after successfuly serializing List<t>).
Another thing that I have with SOAP serialization is that i'm not sure that it'll work in Linux (I was unable to install mono), since I need to import the SOAP class from Windows folder.
Can anyone recommend me on what to do?
Waiting for your help.
NaNg.
|
|
|
|
|
I use C#.net to create datagrid,and I don't know how to add sub columns in grid.
Example:
NANE PRODUCT
PROA PROB
I want to add columns PROA PROB under columns PRODUCT,any body please help me.I have code like this:
Store = new DataTable();
DataRowCollection rows = Store.Rows;
Store.Columns.Add(new DataColumn("Name", typeof(string)));
Store.Columns.Add(new DataColumn("PRODUCT", typeof(string)));
DataRow newRow = Store.NewRow();
newRow[0] = strDocname;
newRow[1] = strProductA;
newRow[2] = strProductB;
Store.Rows.Add(newRow);
myDatagrid.DataSource = StoreView;
StoreView = new DataView(Store);
thank you
|
|
|
|
|
Hi all,
More trouble with unmanaged code calls from C#: I am trying to call a function in C++ that looks something like this:
int DoSomething(const char* string1,const char* string2,const char* string3,<br />
const struct1* someStruct, const bool yesno, int* pos, struct2* anotherStruct)
The corresponding wrapper function in C#:
public static extern bool Do_Something(<br />
[MarshalAs(UnmanagedType.LPStr)] string str1,<br />
[MarshalAs(UnmanagedType.LPStr)] string str2,<br />
[MarshalAs(UnmanagedType.LPStr)] string str3,<br />
StructOne someStruct,<br />
bool YesNo, <br />
ref int pos,<br />
out StructTwo anotherStruct);
The two structs in the unmanaged code (struct1 and struct2) have similar corresponding definitions in the C# code (in this example, called StructOne and StructTwo). These struct defintions are preceded by the following line:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 8)]
Admittedly, I am not quite sure what it does (I'm adding onto someone else's code and am new to C#) but I suppose it has something to do with mapping the C# version of the struct to its C++ counterpart.
The problem: I'm having trouble passing the parameters from the C# wrapper to the C++ routine and am getting AccessViolationExceptions. On stepping through the C++ code, I have found that all the parameters from struct1 onwards are null (the YesNo bool is always false regardless of how it is set in the C# wrapper). All the strings are passed correctly. My feeling is that I have failed to set something up correctly (thus resulting in the program trying to use 'protected' memory), but having checked and rechecked the code (ensuring for instance that there are equivalent members for each of the two C# structs and its corresponding mate in C++), I cannot locate the offending part of the program.
Can anyone help?
thanks
|
|
|
|
|
Hi,
in C# a struct is a value type, but your native function expects a struct pointer,
hence a reference. Adding ref to your wrapper function Something may be sufficient
(depends on the kinds of members in your struct).
AccessViolation typically means some pointer is bad, in this case struct data is used
as a pointer...
In general, to get interop debugged, my advice is add logging code to both sides,
and reduce each problem to its simplest instance, then experiment if you dont
understand what is going on...
|
|
|
|
|
That was exactly what was missing. Works fantastically now. Thanks Luc
On your comment on logging, sounds like a great idea but what information could I garner from this method which I am not able to by debugging it conventionally? (This is not sarcasm, I'm still rather new to the whole software engineering thing and am still feeling my way around concepts like source control and logging)
cheers!
|
|
|
|
|
Hi,
logging provides the information automatically and systematically;
you can get the same info by debugging interactively, but one typically fails
to do that systematically, so I prefer to get log files and read them carefully
when something fails.
|
|
|
|
|
Thanks for the clarification.
cheers!
Joseph
|
|
|
|
|
How do I write to the Visual Studio Output window? Can I read user input from there like I can in Eclipse?
_____________________________
...and justice for all
APe
|
|
|
|
|
Console.WriteLine will appear there. I don't think you can read it, no.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Ok, thanks!
_____________________________
...and justice for all
APe
|
|
|
|
|
Hi frnz....this is an urgent requirement...plz help as soon as possible...
I am creating a tool which runs a batch process for color replacement....
I select a folder which contains all the bitmaps.
I give the input RGB color and the output RGB values.
In all the bitmaps the the color having input RGB should be replaced with a color having output RGB value...i dont know how to replace the colors using C#...plz help me out..
P.S. if anyone can tell me where i can get the color dropper code...that will be of gr8 help...
|
|
|
|
|
You need to read my image processing articles, they show how to access the bits of an image. It will need to be 24 or 32 bit, so the full range of colors is available, then color mapping is trivial
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
thanx Chris...can u gimme the link for ur articles...
|
|
|
|
|
|