|
|
You're welcome.
One way you might not have considered is to create a nested class: all instance methods have access to the class level fields (variables in Pascal), so instead of having your variables local to the pascal function and available to nested functions, you have them private to the class instance, and all methods in the nested class can access them, but the outside world can't:
public class MyClass
{
private class InnerClass
{
private int i = 0;
private int j = 1;
public InnerClass(int x)
{
j = x;
}
public int MethodOne(int x)
{
i = x;
return MethodTwo(j);
}
public int MethodTwo(int x)
{
return i * j + x * 2;
}
}
public void UseIt()
{
for (int i = 0; i < 10; i++)
{
InnerClass ic = new InnerClass(i);
Console.WriteLine(ic.MethodOne(i * 2));
}
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
In method of event ButtonClick I can create subclass and local method in it?
public class MyForm
{
private void button_Click(args)
{
private class InnerClass
{
private int i = 0;
private int j = 1;
public InnerClass(int x)
{
j = x;
}
public int MethodOne(int x)
{
i = x;
return MethodTwo(j);
}
public int MethodTwo(int x)
{
return i * j + x * 2;
}
}
InnerClass.MethodOne();
}
}
modified 18-Oct-14 5:49am.
|
|
|
|
|
No!
An event handler is just a method with a specific signature: you can't create classes local to methods either. Just create the new class as private to the class containing the Click handler, and then create an instance of the class in the button_Click method. You can then call the inner class methods on that instance.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok. Thank you.
I have to redeclare all local variables and send many arguments to other class.
Or declare local variables in global class as global variables.
Sorry for Pascal-thinking.
|
|
|
|
|
No, you declare the variables that the methods must share as private members of the nested class - then they are available to all methods within the class, but not outside.
Each instance of the nested class gets it's own "set" of the variables, just the same as each call to a function in Pascal creates it's own set of variables local to the function and available to all nested functions within it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ok. Ill do so.
Thank you again.
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Ignorissimus' vote of #1 leavened with the yeast of #5
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
Thank you Bill!
How are you this (locally grey, wet, and miserable but probably for you it's) fine day?
All finished setting up the new house?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Note that C# does not, like Pascal, have a separate syntax for procedures (do not return a value) and functions (return a value): in C# both are termed "methods."
There's no direct equivalent in C# to Pascal's nested functions where the inner functions have access to the variables in their "container" ... if that's what you are asking.
What exactly do you mean by "in program method:" do you mean in the Program.cs class that in the default Windows Forms application contains the 'Main method ?
It's easier to say where you can't create methods:
1. outside a NameSpace
2. inside a Method
However, one can, in a way, create methods inside methods by creating instances of delegates or anonymous methods/lambda expressions. The 'Action and 'Func delegates are useful to define methods inside a method.
And, you can have nested Classes in .NET, with each Class containing methods: by making an inner Class static one can easily execute the method given an instance of the outer Class.
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
modified 18-Oct-14 4:39am.
|
|
|
|
|
BillWoodruff wrote: It's easier to say where you can't create methods:
1. outside a NameSpace
2. inside a Method
That's clear.
Thanks.
ill check later rest wide information
|
|
|
|
|
My DeepCopy method:
public static T DeepCopy<T>(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("Entity cannot be null");
}
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, entity);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
The entity I'm trying to copy:
[Serializable]
[DataContract]
public class SafetyInfractionEntity : _BaseEntity
{
#region Properties
private int _SafetyInfractionId;
[DataMember]
public int SafetyInfractionId
{
get { return _SafetyInfractionId; }
set
{
if(_SafetyInfractionId != value)
{
_SafetyInfractionId = value;
IsDirty = true;
RaisePropertyChanged("SafetyInfractionId");
RaiseEntityChanged();
}
}
}
private int _EmployeeId;
[DataMember]
public int EmployeeId
{
get { return _EmployeeId; }
set
{
if(_EmployeeId != value)
{
_EmployeeId = value;
IsDirty = true;
RaisePropertyChanged("EmployeeId");
RaiseEntityChanged();
}
}
}
private int _InfractionTypeId;
[DataMember]
public int InfractionTypeId
{
get { return _InfractionTypeId; }
set
{
if(_InfractionTypeId != value)
{
_InfractionTypeId = value;
IsDirty = true;
RaisePropertyChanged("InfractionTypeId");
RaiseEntityChanged();
}
}
}
private LookupEntity _InfractionType;
[DataMember]
public LookupEntity InfractionType
{
get { return _InfractionType; }
set
{
if (_InfractionType != value)
{
_InfractionType = value;
RaisePropertyChanged("InfractionType");
RaiseEntityChanged();
}
}
}
private DateTime? _InfractionDate;
[DataMember]
public DateTime? InfractionDate
{
get { return _InfractionDate; }
set
{
if (_InfractionDate != value)
{
_InfractionDate = value;
IsDirty = true;
RaisePropertyChanged("InfractionDate");
RaiseEntityChanged();
}
}
}
private int _JobRevisionId;
[DataMember]
public int JobRevisionId
{
get { return _JobRevisionId; }
set
{
if (_JobRevisionId != value)
{
_JobRevisionId = value;
IsDirty = true;
RaisePropertyChanged("JobRevisionId");
RaiseEntityChanged();
}
}
}
private int _ReportedByEmpId;
[DataMember]
public int ReportedByEmpId
{
get { return _ReportedByEmpId; }
set
{
if(_ReportedByEmpId != value)
{
_ReportedByEmpId = value;
IsDirty = true;
RaisePropertyChanged("ReportedByEmpId");
RaiseEntityChanged();
}
}
}
private string _Comments;
[DataMember]
public string Comments
{
get { return _Comments; }
set
{
if(_Comments != value)
{
_Comments = value;
IsDirty = true;
RaisePropertyChanged("Comments");
RaiseEntityChanged();
}
}
}
private JobEntity _InfractionJobLocation;
[DataMember]
public JobEntity InfractionJobLocation
{
get { return _InfractionJobLocation; }
set
{
if (_InfractionJobLocation != value)
{
_InfractionJobLocation = value;
RaisePropertyChanged("InfractionJobLocation");
}
}
}
#endregion
}
Using it like this:
private SafetyInfractionEntity originalSelectedInfraction;
.
.
.
private void editInfraction()
{
originalSelectedInfraction= Utilities.DeepCopy<SafetyInfractionEntity>(SelectedInfraction);
InfractionEditMode = EditMode.Edit;
refreshView();
}
It's not copying. Changes to SelectedInfraction also appear in the originalSelectedInfractioncopy.
Anyone see what I'm doing wrong?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
What kind of debugging have you done, and what do you see ?
If you set a break-point in 'DeepCopy at: ms.Position = 0; and examine what's in 'ms: what do you find: what you expected to find ?
Have you experimented with using a non-Generic 'DeepCopy, or, a non-static version, to see if that might make a difference ? What's in originalSelectedInfraction after the DeepCopy: anything ?
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
From what I can see, there's no way that changes in your original are going to show up in the copy, they are two completely distinct objects.
I'd check where else you are doing this:
originalSelectedInfraction = Utilities.DeepCopy<SafetyInfractionEntity>(SelectedInfraction);
Regards,
Rob Philpott.
|
|
|
|
|
That can only happen due to side-effects of other code which you did not show here. What does happen in the PropertyChanged event handlers? Do you "refresh" some data somewhen from the database?
|
|
|
|
|
Hi everybody,
I am new to c# and have a question that is rather related to wireshark or how to capture messages in plain text sent over the network.
I created a socket server and a tcpclient in c#. I am sending a message like "Here I am" over tcp using the client. The Server answers with a simple string like "Hello". I do this on a local ip address and port 7.
I see the messages in the command line (console), but Wireshark does not show me the text. I am filtering ip.adress source and ip.address dest, but the capture window is empty.
I appreciate any help.
Cheers
Lillepige
|
|
|
|
|
This question has more to do with Wireshark than C#. I suggest you go to their website to see how to use it.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
What does this have to do with C#?
Chances are you didn't capture on the correct interface.
|
|
|
|
|
Install Fiddler.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
If the two apps are both on the same PC it probably is not routed via you NIC (going via 127.0.0.1 which wireshark cannot monitor)- so you cannot see it
Ta
|
|
|
|
|
Hi, I want to create a text file in C sharp at the specified time in a day for e.g., 00:30 am. Pl help.
|
|
|
|
|
What have you tried so far ? Show us some code.
Is your application guaranteed to be running at that time every day ?
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|
|
Hi, I have been following the below logic in C#
1- Text file is written at a source location continuously with fixed file name and destination.
2- Source needs to replicated at specified interval say 3 mins or more. Reason in case of loss of source file data is not lost as its written at destination path. i.e., replicated.
3- During EOD source file will be moved to backup folder and needs to be created at specified time. Such file backup needs to be retain as per number of files or a days.
4- File at destination should be with the naming convention such as file_YYYYMMDD.
5- Program should be running as service in client machine which needs to be auto start mode.
To start with i have been facing issue in below code for file creation. I have still not created main method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Security.Permissions;
using System.IO;
using System.Configuration;
using System.Security.AccessControl;
namespace SyncFile
{
class Create
{
public void Sync (string spath, string dpath, string sbpath, string dbpath)
{
String source = ConfigurationManager.AppSettings["spath"];
String target = ConfigurationManager.AppSettings["dpath"];
String bsource = ConfigurationManager.AppSettings["sbpath"];
String btarget = ConfigurationManager.AppSettings["dbpath"];
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Created += new FileSystemEventHandler(FileCreated);
fsw.EnableRaisingEvents = true;
}
public void FileCreated(Object source, FileSystemEventArgs e)
{
FileSecurity fs = new FileSecurity();
fs.AddAccessRule(new FileSystemAccessRule("spath", FileSystemRights.ReadData, AccessControlType.Allow));
if (!File.Exists("dpath"))
{
//File.SetCreationTime("dpath",(00,01,00));
DateTime fileTime = DateTime.Now;
//File.SetCreationTime("dpath", new DateTime(2014, 4, 3));
File.CreateText("dpath" + "File_"+ DateTime.Now.ToString("ddMMYYYY"));
}
else
{
// Take an action that will affect the write time.
File.SetLastWriteTime("dpath", new DateTime(1985,4,3));
Thread.Sleep (30000);
}
}
}
}
|
|
|
|
|
This is a significant change from your original specific question to many general questions.kanswal77 wrote: To start with i have been facing issue in below code for file creation What issue: where in the code does the issue occur. What error message do you see ?
« There is only one difference between a madman and me. The madman thinks he is sane. I know I am mad. » Salvador Dali
|
|
|
|