|
This is sort of a repost, but I realized after the original post that I was really asking a c# question, not a .NET framework question.
I have the need to change 'this' from one object to another.
Back ground is that I am deserializing some objects that I may already have in memory. If I identify an object as already existing, I want to deserialize directly onto that object, as opposed to deserializing onto a new object and then transfering the values.
1) I can not just use the new object because other objects that were not serialized may have links to the original object, and I don't know them all to change them.
2) The code is already unsafe , so I can't hurt it any more.
Richard
Silver member by constant and unflinching longevity.
|
|
|
|
|
Changing "this" isn't really what you're asking for. If I'm reading this correctly, what you really want to do is overwrite one object's memory with another, so references don't have to be changed.
I would actually advise against this... It would be much easier to do with a simple wrapper class.
public class MyDataObject
{
public SerializableObject Data { get; set; }
public class SerializableObject
{
}
}
So all of your references would be to the instance of MyDataObject, and those would never change... When you deserialize, you set the Data property to the new object, and it works as well as a direct replacement.
If you want to make it more transparent, you can hide the inner object entirely, and make methods and properties in the wrapper that pass through to the inner object.
|
|
|
|
|
I know I can make a variable global to a class. But What I want to do is set a variable under program.cs where "static void Main(string[] args)" Exists. In this class I want to run a initialization process to activate some variables with values I can call from any point in the application. Such as say a System Code. Is there a means of making a variable global to allow for all classes to see the variable.
My current understanding is this is not possible. Only with in classes unless you pass the variable to the other class. Is this correct?
|
|
|
|
|
Terrible idea in general, but you could create a public static variable in a class.
Slightly better: make it a static private variable and expose it as a property.
Cheers,
Vikram. (Cracked not one CCC, but two!)
|
|
|
|
|
You can write a static class to hold such things.
|
|
|
|
|
As Vikram and Piebald suggested, what you're looking for is a static class, which would contain static properties or fields.
If these are program-wide settings, as your post seems to suggest, you can simplify things further (And for that matter, keep it cleaner), by setting up static properties INSIDE the Program class.
public static string SystemSettingThingy { get; private set; }
public static void Main(string[] args)
{
SystemSettingThingy = "MySetting";
}
if (Program.SystemSettingThingy == ...)
One of the main points here is that if it's inside the Program class, you can keep the setter private, so you can ensure that they are never modified. One of the reasons many developers consider globals to be a "bad idea," is that in complex programs, particularly multi-threaded ones, you can have different parts of the app writing to them at different times, and can never really rely on their accuracy.
|
|
|
|
|
Hi,
There is a button on a win form which populates a gridview control with data.
to prevent the UI being locked I am using the Backgroundworker.
The application does not get locked when the data is being retrieved, but it does get locked when inside the event "backgroundWorker1_RunWorkerCompleted" on line:
dataGridView.DataSource = e.Result;
Any thoughts on this please?
Thanks
|
|
|
|
|
Sorry, nothing pops up without seeing some of the code.
I trust the GridView got created before you started the BGW?
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Hi,
your code has vanished again. This was going to be my reply:
(*) I'll put it in when your code is back.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
RunWorkerCompleted is invoked back to the GUI thread, so any code in there will suspend your UI until it finishes.
Some guesses:
1) If you have a lot of data, the grid may be taking a long time to display it. I haven't worked with the WinForms DataGrid in a long time, but if it has any sort of virtualization, turn that on, so it will only try to display the data that's actually visible, not the part that's scrolled off the screen.
2) If it's not a size issue, there may be a property of the result object that's taking a long time to read. When you set the datasource, the grid will read through the results and try to display it, so it will read all the properties it needs... If some of those are complex, it could take a while. If this is the case, try precalculating and caching those properties during the background thread.
|
|
|
|
|
I want to Iterate thru a listBox and write the strings to a file.
I have the following code that worked in VS 2005 but does not compile in VS 2008
StreamWriter f = new StreamWriter(fn);
IEnumerator eEnum = this.mylistBox.Items.GetEnumerator();
if (eEnum != null)
while (eEnum.MoveNext())
f.WriteLine((String) eEnum.Current);
f.Close();
In VS 2008 I get a compile error saying
Using the generic type 'System.Collections.Generic.IEnumerator<t>' requires '1' type arguments
Can someone explain ?
|
|
|
|
|
Hi,
Since .NET 2.0 some interfaces such as IEnumerator exist in non-generic and generic form;
if you have a "using System.Collections.Generic" statement and no "using System.Collections", then the compiker will insist you use IEnumerator<someType>.
If you want to know more, I suggest you read up on "generics".
BTW: you could avoid all this by using a foreach statement, e.g. foreach(object item in myListBox.Items) and then probably work with item.ToString().
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
So I am having a problem. I generated about 100 ISO's files of my movies so I can stream them to my TV. Well now I find out I dont need them in ISO, I need AVI. I am trying to open the ISO and the tool that was used created 2 versions of the same file in the same folder. 1 with 0 bytes and 1 with full bytes. Now here is my question from a C# point of view. I am mounting the ISO as a Drive in Windows and copying all but the 0 byte file out. Works fine in Windows. I want to automate this by having c# Mount the Image and copy the files out. I use the System.IO.Directory.GetFiles and build a array of files. When I go through the array to copy the files, I reference by filename which always get the 0 byte file and not the correct file. I cant delete the 0 byte file as it gets mounted as a CD which is read only. Is there any way to reference a file by some internal ID rather than filename in c#?
private void button1_Click(object sender, EventArgs e)
{
List<string> sFilestoProcess;
List<string> sCopyFiles;
sFilestoProcess = GetISOtoProcess();
foreach (string sFile in sFilestoProcess)
{
Mount(sFile);
sCopyFiles = GetValidFiles();
}
}
private List<string> GetISOtoProcess()
{
List<string> Files = new List<string>();
foreach (string sDirectory in System.IO.Directory.GetFiles(@"D:\"))
{
Files.Add(sDirectory);
}
return Files;
}
|
|
|
|
|
I don't have a direct response for getting the files, but is that pseudo-code or something you are working with? I was wondering if you have code for the Mount() method.
|
|
|
|
|
Dear Sir,
I know its very common problem which has been asked by many people but i did not get any of the persons way of immplementing Remoting as i did so please go through with my steps of my project and provide me solution...
The steps which i used in my program ....
Server Side Code...
my app.config code
<?
xml version="1.0" encoding="utf-8" ?> <
configuration>
<
connectionStrings>
<
add name="VitalDataBaseCon" connectionString ="initial Catalog=pubs ;uid=sa;pwd=success; server=VITAL-17\\VITAL17;integrated security=true"></add>
</
connectionStrings>
<
appSettings>
<
add key ="serverIp" value="192.168.1.17"/>
<
add key ="ServerPort" value="8080"/>
</
appSettings>
<
system.runtime.remoting>
<
customError Mode="on"/>
<
application>
<
service>
<
wellknown mode="SingleCall" type="Vital.DotNet.Server.LogIn,Server" objectUri="LogIn" />
</
service>
<
channels>
<
channel ref="tcp" secure="true" port="8080" impersonate="true" protectionLevel="EncryptAndSign"/>
</
channels>
</
application>
</
system.runtime.remoting>
</
configuration>
Server.cs
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Runtime.Remoting.Lifetime;
using
System.Runtime.Remoting.Channels;
using
System.Runtime.Remoting.Channels.Tcp;
namespace
Vital.DotNet.Server
{
class LogIn:MarshalByRefObject,Vital.DotNet.Server.IvitalServerInterfaces
{
SqlDataAdapter adap;
SqlCommand cmd;
SqlConnection con;
public DataSet retriveData(string authorFirstName,string authorLastName,out bool isSuccess)
{
con =
new SqlConnection("initial Catalog=pubs ;uid=sa;pwd=success; server=VITAL-17\\VITAL17;integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("Demoinput", con);
cmd.CommandType=
CommandType.StoredProcedure;
cmd.Parameters.Add(
new SqlParameter("@au_fname",authorFirstName));
cmd.Parameters.Add(
new SqlParameter("@au_lname",authorLastName));
DataSet ds = new DataSet();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
isSuccess =
false;
}
else
{
isSuccess =
true;
}
return ds;
}
static void Main(string[] args)
{
Console.ReadLine();
}
}
}
My procedure in pubs data base
create procedure [dbo].[Demoinput]( @au_lname varchar(50),@au_fname varchar(50))
as
begin
select * from authors
where au_lname=@au_lname
end
Interface on server side which has been added on client side as a DLL.
namespace
Vital.DotNet.Server
{
public interface IvitalServerInterfaces
{
DataSet retriveData(string authorFirstName, string authorLastName, out bool isSuccess);
}
}
CLIENT Side Code
client side app.config file
<?
xml version="1.0" encoding="utf-8" ?>
<
configuration>
<
appSettings>
<
add key ="serverIp" value="192.168.1.7"/>
<
add key ="ServerPort" value="9002"/>
</
appSettings>
<
system.runtime.remoting>
<
application>
<
channels>
<
channel ref="tcp" secure="true" tokenImpersonationLevel="Identification" username="" password=""/>
</
channels>
</
application>
</
system.runtime.remoting>
</
configuration>
client.cs
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
Vital.DotNet.Server;
using
System.Configuration;
using
System.Runtime.Remoting.Lifetime;
using
System.Runtime.Remoting.Channels;
using
System.Runtime.Remoting.Channels.Tcp;
namespace
Vital.DotNet.client
{
public partial class Form1 : Form
{
Vital.DotNet.Server.
IvitalServerInterfaces ObjServerSide = null;
bool isSuccess = false;
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(object sender, EventArgs e)
{
try
{
ObjServerSide = (
IvitalServerInterfaces)Activator.GetObject(typeof(IvitalServerInterfaces), @"tcp://192.168.1.17" + ":8080" + "/pop");
ObjServerSide.retriveData(txtAuthorFirstName.Text, txtAuthorLastName.Text,
out isSuccess);
}
catch (System.Runtime.Remoting.RemotingException ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
please help me out from this problem ,i checked this code on remote computer(On LAN) as well as same computer but it doesnot work....
Thanks and regards
AMRITA VISHNOI
hi this is amrita.
Amrita
|
|
|
|
|
No one is going to audit all that unformatted code, (no one will probably do it even if you used the pre tags like you're supposed to)
Post specific issues, not a 'here is my project, fix it' post.
|
|
|
|
|
That is a real work of art, that one can only enjoy from a large distance.
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Did you actually READ your post after you hit "Post Message"?? Do you really think anyone is going to scroll through all this empty space? Would you mind cleaning this up so it's readable?
|
|
|
|
|
My eyes! Please, format your code and put it in a <pre> block.
|
|
|
|
|
Especially on weekends i can't read this type of complex code.
Try 2 give code snippets which is well formatted(even having some format) or about the actual problem.
|
|
|
|
|
Hello,
I have one server and 5 clients. I want to write a program in c# on server machine to find out at what time user logged in , logged out; what was the total data uploaded/downloaded by user and what are the software installed or uninstalled by user for that session.
for 5 clients , IP will be provided and the program should return 5 log files (one per client machine) giving details for the above mentioned parameters.
Any help in this regard is highly appreciated.
Thanking you in anticipation.
|
|
|
|
|
Ajay Kewale wrote: I want to write a program in c# on server machine to find out at what time user logged in , logged out
That information can be obtained by browsing the EventLog[^]. Provided that you have the appropriate rights within your network to do so.
Ajay Kewale wrote: what was the total data uploaded/downloaded by user
That's going to be trickier. Most applications don't tell Windows how much they download, so you'd have to dive a level deeper. Perhaps you can use the Firewall API[^] to do so.
Ajay Kewale wrote: what are the software installed or uninstalled by user
The easiest way is to write a small daemon that makes a list of the installed applications[^] when the session starts, make a second list when the session ends, and compare them. Any modification, even individual service packs, will be listed. There should be entries like;
"Update for Microsoft Visual Studio Web Authoring Component (KB945140)"
The hard way is to write a Windows-service, looking for various installer-processes. Either way, you won't find any PortableApps[^] with these methods.
Ajay Kewale wrote: for 5 clients , IP will be provided and the program should return 5 log files (one per client machine) giving details for the above mentioned parameters.
Aw, so without installing any code on the clients at all? You can read the Registry remotely to obtain the list of applications, again, if you have sufficient rights on your domain. I doubt however that the amount of transmitted data can be remotely retrieved.
I are Troll
|
|
|
|
|
Hi,
I have a desktop application which takes photos and upload to a web server. I am using HttpWebRequest and HttpWebResponse classes to make request and get response. I am showing progressbar on the UI to show upload process. Everything is working fine. But since I am getting the status, when one file gets uploaded completely, my progressbar looks frozen, till the complete file upload.
var responseFromServer = ServiceRequest.GetResponsePhoto("AddPhotos", "ServiceInstances", photoToUpload, serviceInstanceID.ToString());
var response = responseFromServer.Deserialize<Dictionary<string, object>>();
bool success = (bool)response["success"];
ServiceRequest class takes care of making a request and getting response from server.
HttpWebResponse.GetResponseStream method gives response for the complete upload of one file. I want to know if there is any way I could able to know intermediate upload steps which I can show on the progressbar which makes UI more interactive.
Any help or any sample will be greatly appreciated.
Veena
|
|
|
|
|
This[^] is for download but you may be able to use for upload also.
only two letters away from being an asset
|
|
|
|
|
Nope. Not using what you have now. Since all of the code blocks until the transaction is complete and none of the methods reports at status information, you'd have to scrap all of your upload code and rewrite it to do this yourself. Even the Async methods don't report status, but they will allow you to keep your form responsive while the upload is going on.
|
|
|
|
|