|
Why does it have to be a struct? Unless this is for Interop I don't see the value in storing a reference type within a value type - why not just declare it as a class?
It definitely isn't definatley
|
|
|
|
|
moon_stick wrote: storing a reference type within a value type
What reference type?
only two letters away from being an asset
|
|
|
|
|
An array is a reference type, no? Of type System.Array??
It definitely isn't definatley
|
|
|
|
|
I guess I looked at the data type and ignored the array
only two letters away from being an asset
|
|
|
|
|
Personally, I'd make your struct a class, and use a generic list instead of an array.
At that point you could do something like this:
public class ChangeRec
{
private List<int> m_modes = new List<int>();
public DateTime LastTimeStamp { get; set; }
public int LastValue { get; set; }
public List<int> Modes { get { return m_modes; } }
public ChangeRec() {}
}
There is now no need to initialize the list beyond what's done in the class.
One other thing... I the variable "Last" refers to the last value added to the list. If that's the case, you can do this (uif you're using the code I posted above):
ChangeRec changeRec = new ChangeRec();
changeRec.Modes.Add(1);
changeRec.Modes.Add(2);
changeRec.Modes.Add(3);
changeRec.Modes.Add(4);
int lastValue = (int)(changeRec.Modes.Last());
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Monday, January 5, 2009 11:42 AM
|
|
|
|
|
i think the fixed keyword might help you
unsafe public struct ChangeRec
{
public DateTime LastTimeStamp;
public int last ;
public fixed int mode[10] ; // I want an integer array of values
}
}
The big problem here is that you'll have to allow unsafe code within your project so it's probably not the best plan unless you're planning to use interop.
i'd be tempted to implement the thing as a class and create getMode(int i) and setMode(int i) methods.
Russ
|
|
|
|
|
The reason is that C# does not allow us to have instance field initializer in struct.
there is a workaround if you do not want to use the unsafe code, such as:
struct MyStruct
{
public int last;
public int[] mode;
public int[] Mode
{
get
{
if (this.mode == null)
{
this.mode = new int[10];
}
return this.mode;
}
}
}
The code above can be compiled successfully.
Tan Li
I Love KongFu~
|
|
|
|
|
Like everyone else, I would question why this is not a class. That doesn't answer your question, however. If you do indeed need a struct...which indicates you need it to be allocated on the stack, I would do something like the following (this might not be exact...havn't used stackalloc in a long time):
public struct ChangeRec
{
public ChangeRec(byte modeSize)
{
mode = stackalloc int[modeSize];
}
public DateTime LastTimeStamp;
public int Last;
public int[] mode;
}
|
|
|
|
|
Thanks for all your inputs, I've got it working as a class. I still don't know how to make this an array using this structure. What I need is an array of 200 records with each record having the structure as originally shown and guidance on how to actually use it.
|
|
|
|
|
I want to know if I can inherit a class and then make it so one of the members that is inherited is not visible. My reason is that on top of memory issues, I don't want the empty member because i am going to use a list of members of the inherited class as a datasource of a gridview and if i have the extra member, it will appear on the gridview. I am up for other suggestions on how to deal with this problem.
|
|
|
|
|
bfis108137 wrote: My reason is that on top of memory issues
You can't fix this, and if the object is empty, the memory usage is negligible anyhow.
bfis108137 wrote: and if i have the extra member, it will appear on the gridview
Only if you make the gridview auto generate column s
bfis108137 wrote: I am up for other suggestions on how to deal with this problem.
Create an interface that only exposes the items you want to see, and then bind to a collection of that.
You can't make a property on the base class disappear.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hello everyone,
Suppose I have the following XML file, I want to use XPath query based technologies to iterate all attributes for all rules nodes and all attributes for each sub rule nodes (under each rules node).
Could anyone provide some pseudo code please? I know how to query rules and related attribute but do not know how to loop inside embed node rule under each rules node.
<Abc>
<rules name="USA">
<rule foo="123" goo="345"/>
</rules>
<rules name="CAN">
<rule foo="321" goo="543"/>
<rule foo="321" goo="234"/>
</rules>
</Abc>
thanks in advance,
George
|
|
|
|
|
Suppose you have the following to query the rules nodes:
XmlNodeList rulesNodes = myDoc.SelectNodes("//rules");
Then, in order to loop the rule nodes:
foreach (XmlNode rulesNode in rulesNodes)
{
XmlNodeList ruleNodes = rulesNode.SelectNodes("rule");
foreach (XmlNode ruleNode in ruleNodes)
{
....
}
}
Hope this helps.
|
|
|
|
|
Thanks Le Centriste,
For this statement -- "myDoc.SelectNodes("//rules");", XMLDocument class does not contain a method called SelectNodes. Any comments?
regards,
George
|
|
|
|
|
George_George wrote: XMLDocument class does not contain a method called SelectNodes. Any comments?
Yes it does[^]
|
|
|
|
|
Sorry, Le Centriste!
My typo.
regards,
George
|
|
|
|
|
George_George wrote: For this statement -- "myDoc.SelectNodes("//rules");"
How did you created the XMLDocuments?
XmlDocument xmlDoc = new XmlDocument();
Now Check xmlDoc.SelectedNodes() is there or not ?
cheers,
Abhijit
|
|
|
|
|
Thanks Abhijit!
My typo in my code before.
regards,
George
|
|
|
|
|
George_George wrote: XMLDocument class
What XMLDocument class George? You have provided ZERO information about your development environment.
I see this year is going to be like last year with you eh?
led mike
|
|
|
|
|
Thanks led mike,
Found it out.
regards,
George
|
|
|
|
|
Try This one
XPathDocument doc = new XPathDocument(XmlFilePath);
XPathNavigator XPathnav = doc.CreateNavigator();
XPathExpression expr;
expr = XPathnav.Compile("/Abc/rules");
XPathNodeIterator XpathIterator = XPathnav.Select(expr);
try
{
while (XpathIterator .MoveNext())
{
XPathNavigator IteXPathnav= XpathIterator .Current.Clone();
Console.WriteLine("name: " + IteXPathnav.Value);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
This is For Further Study[^]
cheers,
Abhijit
|
|
|
|
|
Thanks Abhijit,
Your code works good. But it only has one loop, but what I need is a loop inside another loop for the XML structure.
regards,
George
|
|
|
|
|
Please Read These
Ref1[^]
And
Ref2[^]
cheers,
Abhijit
CodeProject.Com MVP
|
|
|
|
|
I created a DLL (class library) on C#.NET (2005) and want
to use it as an ActiveX control on a web page (html).
I have checked the box for Make "assembly COM visible" in the
project properties before I build the DLL. Code does do much,
here is my C# code for ctrl
namespace MyActiveXctrlLib
{
public partial class MyUserCtrl : UserControl
{
public string setName
{
set
{
txtName.Text = value;
}
}
public MyUserCtrl()
{
InitializeComponent();
}
}
}
here is my HTML
<title>Untitled Page
<object id="myControl1" name="myControl1" classid="MyActiveXctrlLib.dll#MyActiveXctrlLib.MyUserCtrl" width="288" height="50">
function doScript()
{
myControl1.setName = frm.txt.value;
}
on button click doScript(); is executed .
it sets the setName property of myControl1 i.e. WinForm control.
this code working successfully on IE 7.0 machine having .NET installed on them
but is not working on Machine those dnt have .NET installed on them.
not checked on IE 6.0.
Thanks in advance .
DaTtA
way2datta@gmail.com
|
|
|
|
|
Dattatraya Kale wrote: Subject: Run .NET ActiveX control on IE 7.0 machine that doesnt have .NET installed.
This is crosspost. You have already put the same question in asp.net forum.
cheers,
Abhijit
|
|
|
|