|
Is the object unmanaged? If not, it will be difficult. The GC is agnostic of pointers, so pointers are not allowed to reference types or structs that may appear (including structs with references) on the managed heap. If it were allowed, you could potentially wind up with heap resources referenced only by pointers that could be reclaimed while still in use.
From the language spec:
Unlike references (values of reference types), pointers are not tracked by the garbage collector - the garbage collector has no knowledge of pointers and the data to which they point. For this reason a pointer is not permitted to point to a reference or to a struct that contains references, and the referent type of a pointer must be an unmanaged-type.
An unmanaged-type is any type that isn't a reference-type and doesn't contain reference-type fields at any level of nesting. In other words, an unmanaged-type is one of the following:
sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Suppose that I have an interface named IMyInterface and two classes both of which implement this interface, say ClassA and ClassB. Now I need a collection of these classes. So I have to have an array of IMyInterface(I don't want to use any thing like ArrayList or something). Now my problem is how to initiate this kind of array so that it would be able to hold both ClassA and ClassB? For example think I need an array of size 10:
private IMyInterface[] _interfaces;
_interfaces = new (what?)[10]
ps: What is the best way of having such a dynamic array? should I have a temp array and use .CopyTo method and re-new the old array?
Don't forget, that's Persian Gulf not Arabian gulf!
Murphy:Click Here![^]
Events and Delegates simplified:Click Here![^]
I'm thirsty like sun, more landless than wind...
|
|
|
|
|
_interfaces = new IMyInterface[10];<br />
|
|
|
|
|
You are right about interfaces = new IMyInterface[10]; , but if I recall it correctly, the array is set to a size of 10, but contains NO values, not even null .
- Daniël Pelsmaeker
An egoist is someone who doesn't think about me.
|
|
|
|
|
Interfaces are reference types, hence the array is filled with null . Try to call methods on any of those array elements (before initialization) and you'll get a NullReferenceException or ArgumentNullException depending on how you use the array elements. Step through in a debugger and you'll find that they're null as well. Structs and other value types - on the other hand, do not require instantiation in an array and their members will be the default values for whatever Types they are (reference types will still be null since you can't provide a default constructor with structs, but an Int32 will be 0, etc.).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Each element of your array will be set to null. There is no such thing as NO value. Every reference type is initialized to null.
If you want dynamic array, you can use ArrayList() and just cast when you get elements:
ArrayList list = new ArrayList();
list.add(elem);
IMyInterface iface = (IMyInterface)list[i];
|
|
|
|
|
hi,
i have a form(frmQueue) where i've put a datagrid control with only one column on it. when the user clicks on the "Update" button it gets whatever info from the datasource and displays it in the datagrid. the user can then select a particular column and click a "Get Details" button, when the button is clicked, i want the details to be displayed in textboxes in another form(frmTransaction). my question is how do i code the Get Details button. im new to this language...so an example would really get me working.
thx in advance
When a man's wife starts to understand him, she usually stops listening to him.
|
|
|
|
|
Change the constructor of your second form and add a srting parameter to it. And when you create it in your button event handler, pass he column name there and proccess it in second form.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Hi Mazy,
When u said to pass the column name, did u mean the column name from the datasource or the datagrid?. i just wanna confirm.
|
|
|
|
|
DataSource
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
OK. I'm workin on this program and theres this one part of it that loads all of the names of the files in a certain folder, and then adds them to a context menu. Now here is where i run into my problem. I want to make it so that you can click on the menu item in the context menu (which was loaded when the program opened) and have it open its corresponding file in the folder it was loaded from.
Here is the code i used to load the files:
string[] files = Directory.GetFiles(@"docs\");<br />
int fnumber = files.Length;<br />
<br />
for(int cNumber = 0; cNumber != fnumber; cNumber++)<br />
{<br />
string fileName = Path.GetFileNameWithoutExtension(files[cNumber]);<br />
MenuItem menuItem = new MenuItem();<br />
menuItem.Text = fileName;<br />
contextMenu.MenuItems.Add(0, menuItem);<br />
}
Now all i need is an event handler for each menu item that can open the right file. If you know how to do this, then please tell me. Thanks.
By BigBlob202
check out my site - Dinco Inc. - I just started it so theres not much on it.
|
|
|
|
|
You can simply attach an event handler to your MenuItem object.
MenuItem mi = new MenuItem();
mi.Click += new System.EventHandler(this.MenuClick);
...
private void MenuClick(object sender, System.EventArgs e)
{
MenuItem mi = sender as MenuItem;
if(mi != null)
{
}
}
- Nick Parker My Blog
|
|
|
|
|
OK ive put that in the code but now i have no idea how to tell the event handler which menu item was clicked. Thanks so much!!
By BigBlob202
check out my site - Dinco Inc. - I just started it so theres not much on it.
|
|
|
|
|
It appeared in your case you were assigning the Text property of the MenuItem to be a filename. Once you cast the sender as a MenuItem and confirm the cast didn't fail (by checking for null), you can then check the MenuItem (i.e.; mi in my example) for it's Text property. Try throwing a MessageBox.Show(mi.Text); after your check for null to see what I am talking about. This will tell you exactly what was clicked.
- Nick Parker My Blog
|
|
|
|
|
Thank you so much. It worked perfectly!! If you wanna see the program i was making then go to my site - Dinco Inc. - The program is a miniware program called Desktop Saver. It should be up at least by the middle of next week. Thanks Again!
By BigBlob202
|
|
|
|
|
Hello,
My program needs to access a network share and either copy files from the share or else just execute a program. First I have the client check if it can see the share:
if(Directory.Exists("\\\\server\\share") {
// do whatever
}
else {
// couldn't see share
// provided the share is online
// I assume the client does not have proper permission
// basically I want to "net use" here.
}
Is there a function that does this? Or do i need to start a "net use" process? What is the preferred method of gaining access to a remote share through C#?
Thanks,
Ian
|
|
|
|
|
just use it like a normal file resource
|
|
|
|
|
As the first post mentioned - just access the file using a UNC path. If you get an UnauthorizedException , this means you don't have permissions. The correct way is to prompt the user for credentials and impersonate them using those credentials. For an example of impersonation using .NET, see the documentation for the WindowsIdentity.Impersonate method in the .NET Framework SDK.
If you want to go to the trouble of forming a net use statement (using /user and prompting for the password), you could do that but you'll have to make sure you change all UNC references so that they use the new mapped drive letter.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
This might be a stupid question but I can't figure it out:
I am writing a simple c# console app that calls another executable (usually a batch file but sometimes an exe). I want the other executable called to run in the same window that I ran the c# app.
For instance, one of the executables I can call is an exe and it prints the build# to the screen. If I call it from the command prompt manually it runs in the same window:
c:\>\\server\share\getversion
build number: 3000
c:\>
However in my C# app it always opens getversion.exe in a new window. How do I make it run in the current command window? I tried using ProcessStartInfo.CreateNoWindow = false but that doesn't work.
if(Directory.Exists("\\\\server\\share\\"))
{
Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "\\\\server\\share\\getversion.exe";
myProcess.StartInfo = startInfo;
myProcess.Start();
}
When I run ConsoleApplication1.exe from a command prompt, it opens another command window, executes getversion.exe (which then closes immediately and you can't even see what it said).
|
|
|
|
|
Arg I figured it out. I used UseShellExecute = false and took out the CreateNoWindow = true.
|
|
|
|
|
Hi everyone,
I guess that this is just somethink trivial, but I can't seem to find how to do it. I have a single DataGrid in my Windows Form which I am using to display a DataSet containing two tables constrained by a parent-child relationship. I'm using the DataGrid.SetDataBinding method to databind the datagrid to the first (parent) table. The DataGrid automatically displays a small "+" buttons in each rowheader which allows the user to navigate to related records in the child table. The child table contains a header showing information about the parent table (determined by the ParentRowDisplayStyle property of the DataGrid, I think). This is all nice, but is there any way to navigate back to the parent table? Or do I have to add another control to the form (button) which will call some DataGrid's method to achieve this?
Thanks a lot in advance!
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
Set both AllowNavigation and CaptionVisible to true . The caption will show some navigational buttons.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Oh, thanks a lot, I didn't use the caption at all, I will try it!
Thanks again
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
I have just tried it and it works, thanks! I've got one (well, two) more questions, though:
1. Regardless of the value of ParentRowLabelStyle property, the ParentRow always displays the table name as well as all columns. What am I doing wrong?
2. When users click the "+" button in the parent table, the name of the relation appears and once they click on it, they are navigated to the chlid table. Is it possible to display something else in place of the relation name (something more user friendly) or not?
Thanks again!
rado
Radoslav Bielik
http://www.neomyz.com/poll [^] - Get your own web poll
|
|
|
|
|
Radoslav Bielik wrote:
1. Regardless of the value of ParentRowLabelStyle property, the ParentRow always displays the table name as well as all columns. What am I doing wrong?
Try setting DataGrid.ParentRowsVisible to false .
Radoslav Bielik wrote:
2. When users click the "+" button in the parent table, the name of the relation appears and once they click on it, they are navigated to the chlid table. Is it possible to display something else in place of the relation name (something more user friendly) or not?
Why don't you give the relationship a better name? That'd be the easiest way. Otherwise, changing this will be either impossible (without messy reflection) or difficult, since those types of things are wrapped up into private methods - perhaps even nested classes - of the DataGrid control. Learn to read Intermediate Language (IL) - the language to which all .NET languages compile - and use ildasm.exe that comes with the .NET Framework SDK to see what's going on inside it if you want to go down this route. The first suggestion would be far easier, though.
Microsoft MVP, Visual C#
My Articles
|
|
|
|