|
I am writing a program that requires array types and dimensions be decided at runtime. I can use System.Array.CreateInstance() to do this. What is the most efficient way to access the data in this array? I know I can use the getValue and setValue methods. I can also use the enumerable interface when allowed by the situation. Both of these are however inconvenient when compared to indexing the values using [].
Does anyone have any suggestions. Efficiency of the code is a major consideration.
Thanks,
Karl
|
|
|
|
|
If you expect the modify the array while enumerating you cannot use IEnumerable . It will throw an exception if the underlying enumerable changes.
Why not just use an ArrayList ? It has the same read performance as an array because it uses an array internally. It's only when you add elements past the current capacity that there is overhead since a new array is created (doubling in size) and previous elements are copied.
Dynamic code is often an inconvenience, but there's not much you can do short of wrapping such behavior (which, once again, ArrayList already does for you - just examine the IL for yourself or use a decompiler like .NET Reflector). Ever written native code to access automation (IDispatch implementations) servers? It's a pain compared to automation clients like VB6 and script, but that's the price to pay for late-binding code.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for your response.
As I understand the ArrayList and all other collections provide lists of objects. My arrays will contain standard types such as byte, int, float...
As a result if I use a collection there will be a large performance hit (boxing,unboxing,longer value retreval times).
Thanks
Karl Baum
|
|
|
|
|
An array is an IList implementation, which inherits from ICollection which inherits from IEnumerable . As I said - and as you can see in the IL module where ArrayList is defined - the ArrayList (and, hence, any collections that use an internal ArrayList ) - do use arrays that are doubled in capacity before that capacity is exceeded.
There will only be a performance hit if you collect value types (like primatives, not "standard" types). Boxing and unboxing only describes the process that the CLR performs when treating a value type as an object and back again, respectively.
In .NET 2.0, of course, you could simply use List<> to declare a generic list for whatever type, reference or value types.
If performance is a problem now and you can't or don't want to use .NET 2.0 (granted, it's still in beta 1 but beta 2 is due out shortly) than you'll need to use what you're doing now to create arrays. Again, though, I urge that you wrap such functionality in a class. Later you can derive that class from List<> and remove your implementation. Now you won't have to change your calling code. Just make sure to use the same signatures as List<> , which you can find at http://msdn2.microsoft.com/library/6sh2ey19.aspx[^] (the new namespace extension to MSDN2 doesn't appear to be functioning just yet for generics, so I can't give you the simple change-tolerant link).
Basically, you'd just implement your own ArrayList -like class. It's really not hard. If you know IL, use ildasm.exe that ships with the SDK (that's installed by default with VS.NET) or use a decompiler like .NET Reflector to see how ArrayList operates if you can't figure it out based on the documentation. You're already part way there with what you're doing.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you for your response,
I understand how all of this works, and was just wondering if c# contained anything I wasn't aware of. It appears I will have to continue doing it as I have been.
ArrayLists and the other collections are simply not appropriate for what I am trying to do. The functionality they provide come with too much of a performance hit. The implementation of ArrayList is significantly different from what I am trying to do. ArrayList's power and weakness come from it being a list of references, and hence you can stick anything in there, at the expense of following the references. What I am looking for is a standard array (one continuous chunk of memory, with only one reference to the array itself).
Thanks for your help and suggestions. Particularly your suggestion for easier transition over to the new capabilities that will be provided by 2.0.
Karl Baum
|
|
|
|
|
Hi All
I am not using the toolbox to place a textbox
Instead i want to generate code for placing the text box on page_load
i used the following code:
Label lblMessage=new Label();
lblMessage.Text="ID";
lblMessage.Controls.Add(System.Web.UI.Control Label);
is there ne other way coz this does not work.
Sukanya
|
|
|
|
|
First of all you have to use TextBox class insted of Label Class and you should set a location for that control in addetion to its dimenssions
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
You do exactly what you see in the source code when use the designer. There's no magic like with VB6 using hidden code (though the designer region of code is collapsed, but you just expand it). Look at the source code and don't depend so much on what the designer does.
As the first reply stated, if you want a ,TextBox than use a TextBox , not a Label . But you must add the instance that you instantiated (in your example, that would be lblMessage ) to the contorl collection for the parent control:
TextBox tbMessage = new TextBox();
this.Controls.Add(tbMessage);
tbMessage.Location = new Point(8, 8); Just look at the source code that the designer used. You can do all that manually, too. Most application I write I don't use the designer at all, and it results in the same code.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
"enumerate all the ways of making change for X cents."
For example, if given 10 cents, the possibilities include:
10 pennies, 5 pennies and 1 nickel, 2 nickels, and 1 dime.
You can assume the allowable denominations are pennies,
nickels, dimes and quarters.
cents : number of cents that you should enumerate the
change possibilities for (for example, 82)
Above C# Code used should be non-recursive.
|
|
|
|
|
Wow - there's a piece of real world code if ever I saw one.
I'm happy to do your homework. For $5. When the next assignment comes, and you can't do it because it assumes you know how to do this one, I'll charge you $10. And so on.
OR, if your daddy isn't that rich and/or you actually want to be a software developer one day, think about the problem in terms of the tools you've been given ( i.e. what they taught you in class ), and if you get stuck, post your code and ask for help. Then I'll help for free.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
As what a great mutant once said, "Sometimes the mind needs to discover things for itself."
I certainly agree with Christian and will certainly encourage you to stretch your God given nuts!
|
|
|
|
|
i had problem in returning a DataReader object from a ServicedComponent class. The dataset object was return fine, but when return the DataReader, the datareader object was a transparent_proxy object. I dont understand why only the return DataReader object is a transparent_proxy?
And if i use any method of the returned DataReader, the following error will be occur:
This remoting proxy has no channel sink which means either the server has no registered server channels that are listening, or this application has no suitable client channel to talk to the server.
my codes is simplied as follow:
//First layer is DataAccess Layer
public sealed class DataAccess : ServicedComponent
{
//others methods here...
public OleDbDataReader ExecuteReader (OleDbConnection objConn, string
sql, CommandType commandType, OleDbParameters paramList)
{
//SomeOthers codes here...
return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
}
}
//Second Layer is Data Access Logic
public class EmployeeDAL : ServicedComponent
{
public IDataReader GetEmployeesFirstName()
{
//someothers codes here...
DataAccess objDa=new DataAccess();
OleDbConnection objConn=new OleDbConnection();
return objDa.ExecuteReader(objConn,sql);
}
}
Thanks in advance for your help.
gkb
|
|
|
|
|
my project is to reuse current C++ application modules and create a whole new web-based GUI.
My question is, when i convert my C++ application into DLL. Can i use C# to import that DLL and then use ASP.net to create front-end for that application? as i know that ASP.net can use C# as code-behind.
if all above can work. can anybody provide a simple example to let me test it out. I don't know how to create C++ DLL in Visual Studio.Net 2003. Can anybody give me a useful link. So that i can try to create a simplest hello world example.
Thanks for viewing
regards;
webdevelopex
|
|
|
|
|
Why do you need to create the dll, isn't it preexisting ? Is it a COM dll, or a normal one ? Either way, there's no reason i can see why you can't use C# to call it from ASP.NET, just as you would in C# in any other application.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I meant not the preexisting C++ DLL, it is something like COM dll or my own created DDL. But i am not sure that the DDL created in visual studio 6.0 can be use in C#? If yes, that's mean i can use ASP.net to call the function in my own DDL which was created in visual studio 6.0.? and the C# will act as a middel man between ASP.net and C++ DLL(created in visual studio 6.0).
anywhere, thanks for repied. Cheers!!!
|
|
|
|
|
webdevelopex wrote:
it is something like COM dll
Yes, you can call a COM-based .dll from C#, in the ASP.NET environment. Please read the documentation on the Runtime Callable Wrapper[^] in the .NET Framework Developer's Guide.
|
|
|
|
|
That can be dangerous, though. The COM server must run in a multi-threaded apartment or you'll notice significant performance loss and perhaps even worse. ASP.NET runs in an MTA, so the COM server should, too.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Is there any way of preventing graphics files from being overwritten by an end user even when the application is not running? In Windows 2000 and above when you right-click on a graphics file, there is a folder tab which contains the potential to add information about the graphics file. While there is not any PASSWORD field available, could this information perhaps be used to prevent the files from being overwritten by the end user. Say, for example that these files were important enough to need protection. Would some sort of run-time windows interface file be needed to prevent the files from being overwritten? Any suggestions for an approach to protect the graphics files that could be turned ON and turned OFF again when needed by the application?
|
|
|
|
|
You could change the attributes on the file to make it a system and hidden file when you are not using it. Users shouldn't even be able to see the file then. Then, when you need to change the file, just remove the attributes(if that is necessary, you may just be able to edit the file without removing the attributes) and put them back after your done.
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
if ((file.Attributes & System.IO.FileAttributes.Hidden) != 1) {
file.Attributes &= System.IO.FileAttributes.Hidden;
}
if ((file.Attributes & System.IO.FileAttributes.System) != 1) {
file.Attributes &= System.IO.FileAttributes.System;
}
//Modify File
file.Attributes |= System.IO.FileAttributes.Hidden;
file.Attributes |= System.IO.FileAttributes.System;
|
|
|
|
|
smbecker:
Attempted to utilize your code, but it appears that much of the code is written in C++. For example, the code:
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
if ((file.Attributes & System.IO.FileAttributes.Hidden)!=1)
{
file.Attributes &= System.IO.FileAttributes.Hidden;
}
does not work, because the C# compiler does not understand != 1. More specifically, the error message I get is: "operator '!=' cannot be applied to operands of type 'System.IO.FileAttributes' and 'int'." I would also like to request a couple other clarifications.
First, I suspect that the aforementioned code would act in the following manner. If a file is hidden, then unhide it so that the changes could be made. Make the changes to the file, and then hide it again. The same would be true for the other FileAttributes including System, and ReadOnly.
I really like your thought processes in writing the code, but I do admit that I am at a loss to understand it. I did not come from the C++ environment, and I do not clearly understand the use of the &= , the |= or the !=1 construction in C# nor do I understand the logical AND statment before the !=1.
Therefore, I would really appreciate it if you could write it another way. Also, I am not certain how the code in the if statements work to convert the files to their normal format (ie unhidden, nonsystem, and not readonly) so that changes could be made. Would really appreciate some further elaboration.
Thanks
New_Phoenix
|
|
|
|
|
Embed them as a resource in your application.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
Not certain what embedding as a resource means exactly. However, I would need to be able to overwrite them when more current files are available. If I embedded them as a binary file somehow, if that is what you mean, could that be overwritten utilizing a binary writer in System.IO.BinaryWriter, or something?
I need to be able to have the user overwrite the files when properly interacting with the application, but I do not want the user to be able to overwrite the graphics files while the program is not running. Otherwise, the user could set the graphics to something that is an unintended usage for the program. Meaning, I do not want undesirable graphic files overwriting the intended graphic files for the application.
Could you kindly enlighten me about what you mean by embedding it as a resource. I would appreciate it. Thanks
|
|
|
|
|
It means that you make the file part of the project, and then select 'embed as resource' from the properties for the file. This means the file is part of the exe, and loaded from the exe, but it therefore means you cannot edit it.
Now that I understand the problem, I'd recommend some sort of digital signature on your images, so that you can reject images that don't fit it. There are articles on embedding a string in an image on this site, that's probably how I'd go.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
(1) The application does not change the graphics.
Try Christian Graus' solution.
(2) The application changes the graphics.
Change the filenames, so that the user does not recognise them as graphics files.
If that's not possible, save a backup of the files everytime the application is being closed (in /data/important.dat, or something like that). On next start, check last modification time and, if the backups are older than the original, restore the backups.
(3) The application won't crash with different images, you just don't like them to get changed.
Embed a watermark in the graphics, and try to extract it whenever the application starts. Refuse to run, if the watermark is not okay.
_________________________________
Vote '1' if you're too lazy for a discussion
|
|
|
|
|
Not certain exactly how to embed a watermark in a .gif file. Isn't a .gif file the equivalent of a raster graphics file, while a watermark would require something like a vector-based photoshop file so that the watermark could be somehow extracted and compared to some text value? Also, I would need to be able to overwrite them when more current files are available.
I like the idea of changing the properties of the files to readonly, system and hidden that smbecker had suggested, but then I would need to also be able to hide the folder in which the files are stored. If I embedded them as a binary file somehow, if that is what you mean, could that be overwritten utilizing a binary writer in System.IO.BinaryWriter, or something?
I need to be able to have the user overwrite the files when properly interacting with the application, but I do not want the user to be able to overwrite the graphics files while the program is not running. Otherwise, the user could set the graphics to something that is an unintended usage for the program.
Could you kindly enlighten me about how to store the graphic files in a .dat file that could be overwritten by the application when the user interacts with the program properly? I also need to be able to prevent the user from deleting the folder or the files entirely so some interaction with the O/S is necessary via the System.IO.FileAttributes.System interface which restricts what the user can do with files.
Also, with above Windows 2000, graphic files when right-clicked have permissions provided, could C# interface with these properties and change those when the application interacts with them properly when running? To see what I mean, simple right-click on a graphics file when using above Windows 2000. Could these permissions be interfaced with as well as utilizing the .Hidden and .System and .ReadOnly attributes of FileAttributes?
I seriously doubt that an end user would know that they would be required to not only find the file folder, then the files, then change the attributes of the files, and then change the permission to the files provided by right clicking on them. Would these steps be enough to protect the graphic files?
I would appreciate more assistance. Thanks
|
|
|
|
|