This question already has answers here:
C#: Inheritance Problem with List<T>
(4 answers)
Closed 8 years ago.
i have a code:
public class _clientSockets<Socket> : List<Socket>
{
public event EventHandler OnAdd;
public void Add(Socket s)
{
if (OnAdd != null)
OnAdd(this, null);
base.Add(s);
}
}
that every client that will try to connect to the server will be stored on the list. And i tried to call it using this code:
_clientSockets<Socket> s = new _clientSockets<Socket>();
s.OnAdd += new EventHandler(clientAlerted);
s.Add(socket);
But the problem is got an error something like:
" MainWindow._clientSockets.Add(Socket)' hides inherited member 'System.Collections.generic.List.Add(Socket)'. Use the new keyword if hiding was intended. "
How can i solve this stuff? :)
How can i solve this?
Either don't call your method Add or don't inherit from List. You may find that composition works better than inheritance here, since clients could just treat your class as a plain List<T> and bypass your "new" Add method.
Related
This question already has answers here:
Passing a List into a method, modify the list within the method without affecting 'original'
(5 answers)
Closed 2 years ago.
Maybe someone can help me, I'm really stumped.
I have a class called TextFunc which contains 2 functions.
The first function read a filepath and return a List<string>.
The second function takes that List<string> as a argument and returns an float[,]. Within this function I remove the first item of the list since I'm not interested in the header.
My problem is that somehow my second function modifies the original List.
So If I display the the first item of the list before the second function it shows what I expect.
After the second function the first item is gone.
I can't figure out why since I'm not using a reference when passing the argument into the second function. I don't even call it the same name or anything within the second function.
My class containing the 2 functions look like this (only keeping the relevant parts):
class TextFunc
{
public static List<string> ParseText(string filePath)
{
List<string> lines = File.ReadAllLines(filePath).ToList();
//do some stuff
return lines;
}
public static float[,] txt2Array(List<string> txtList)
{
txtList.RemoveAt(0);
// do some stuff
return floatArray;
}
}
I call the functions like this from an buttonclick event inside the Form1.cs
public partial class BRkData : Form
{
public BRkData()
{
InitializeComponent();
}
private void BRkData_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
List<string> txtFile = TextFunc.ParseText(#"C:\org_data.res");
MessageBox.Show(txtFile[0]); // here it displays what I expect
float[,] floatArr = TextFunc.txt2Array(txtFile);
MessageBox.Show(txtFile[0]); // here the first item of the list is gone?
}
}
I even tried making a copy of the list inside the txt2Array function but it does not matter. Somehow it's like I send a reference to the list to this function without knowing it.
Think of it like this. There is a man called List (reference type object) who knows a list of good pubs. You have his phone number on a piece of paper.
Calling a method is like giving the phone number to someone else. They have it written on a different bit of paper, but the same man answers the phone.
Passing using the ref keyword would mean that you give the other person the same bit of paper. They could cross out the original phone number and put a new one in, and give it back to you.
I didn't know I had to use the ToList command when I tried to copy it inside the function.
List<string> txtList = txtList_org.ToList();
Thank you for clarifying and pointing me in the right direction!
This question already has answers here:
Make all Controls on a Form read-only at once
(7 answers)
Closed 4 years ago.
I'm making a Windows Form Application.
Well, C# programming is new to me and maybe this is a silly question, but how I can apply ReadOnly property in multiple textbox elements? I tried this code:
public void DoReadOnly(Control control){
foreach (Control c in control.Controls){
if (c.Controls != null && c.Controls.Count > 0){
DoReadOnly(c);
}
else if (c is TextBox){
(c as TextBox).ReadOnly = true;
}
}
}
public void getData(){
DoReadOnly(this.Form);
}
The trouble is that I don't know which parameter I should put when I'm call doReadOnly's function. Visual Studio doesn't recognize this.Form like a valid argument.
To call use this.
DoReadOnly(this)
If the method is on Form class
Pass only 'this', that object is your current form
public void getData(){
DoReadOnly(this);
}
Use DoReadOnly(this); instead of DoReadOnly(this.Form);
another thing, why getDate if you are going to put or change a propity, use setData
This question already has answers here:
How do I programmatically wire up ToolStripButton events in C#?
(2 answers)
Closed 7 years ago.
Me again, and it's another problem with my plugin parser for my c# browser, I'm trying to add a eventhandler to make it to where when you hit the plugin button it does something. The reason i am having to do this in code is because it is loading the plugins from files, and they are not hard-coded.
Here's my code, It will look pretty familar to the last one if you saw it
toolStrip1.Items.Add( pluginButton );
pluginButton.Image = Simple_Browse.Properties.Resources.plugin;
pluginButton.Alignment = ToolStripItemAlignment.Right;
pluginButton.ToolTipText = TitlePlugin;
pluginButton.Click += StartPlugin();
private EventHandler StartPlugin()
{
PluginPage plgp = new PluginPage();
plgp.Show();
plgp.Text = PlgTitle2;
}
So the code is pretty basic, but im getting an error at private EventHandler StartPlugin() The error is not all code paths return a value Please help!
You probably meant to do this instead:
pluginButton.Click += StartPlugin; // not StartPlugin()
private void StartPlugin(object sender, EventArgs e)
{
PluginPage plgp = new PluginPage();
plgp.Show();
plgp.Text = PlgTitle2;
}
It looks like you may need to read a bit more on how delegates and event handlers work.
You're requesting an EventHandler which means you have to return an EventHandler. In your handler there is no return, so this error is thrown.
You could use private void StartPlugin(). void doesn't request anything to return.
So your code will look like this:
pluginButton.Click += StartPlugin;
private void StartPlugin(object sender, EventArgs e)
{
PluginPage plgp = new PluginPage();
plgp.Show();
plgp.Text = PlgTitle2;
}
private EventHandler StartPlugin()
This line, specifically "EventHandler", means you're returning an EventHandler. Your code does not do this. To link the EventHandler with the button click, you dont just use a line, you also need a return, (e.x return button1.Click;) because if i used return null; it would just do the action once you binded it.
This question already has answers here:
How to create a custom MessageBox?
(4 answers)
Closed 9 years ago.
I'm trying to make a custom message box for my application. The problem is, I want to code it in a way so that I can use it as regular message box.
MyCustomBox("My Message");
intead of doing
FormMessage frm = new FormMessage();
frm.message = "My Message";
frm.show();
How can I accomplish this? Thanks!
You can add a static method to FormMessage class
public static void ShowBox(string message)
{
using (FormMessage frm = new FormMessage())
{
frm.Message = message;
frm.ShowDialog();
}
}
And then
FormMessage.ShowBox("My Message");
Create the form with the appropriate controls, etc. Then add a static method to the class that handles all the messy bits - creating an instance (if necessary), setting properties, etc.
I wish I could write more on this, but it's pretty simple stuff. Just call MyCustomBox.ShowMessage() or whatever you call the static method.
This question already exists:
Multiple Instances New Class() C#
Closed 9 years ago.
I am using the code sample below -
Program.cs has a list of clients as:
public static List<Client> clients = new List<Client>();
with an event handler for click on button1
private void button1_Click(object sender, EventArgs e)
{
Client client = new Client(combobox1.selecteditem);
Program.clients.Add(client);
}
Client.cs
All variables are non-static and public. There is an eventhandler where on packet receive, a class is called, this class is then filtered and processed
it is called in the following code:
public void recieved(short op, string str, Client c)
{
switch (op)
{
case (short)OpCodes.matches:
{
c.something(c, str);
break;
}
}
}
Handler.cs
public void something(Client c, string movement)
{
if (movement == null)
c.coords = movement;
c.freeSpot = true;
}
And in the above ^ the variables would overlap and freespot would be made true throughout all the instances.
It will work fine with one instance. But I'm trying to compile with multiple instances.
So creating a button_onclick would create a new instance using the above.
As the program is running, it runs flawlessly on one instance, but with 2+ instances, the variables in MyClass start to overlap. Is there a way to prevent this?
I can't say for certain without more context, but this may be a concurrency problem. List is not thread safe. Try using ConcurrentBag<T> instead of List<T>.