MoPraL Posted January 11, 2005 Posted January 11, 2005 Hello, I have a WinForm called frmMain and a class called IRCEngine, they are in the same namespace. In my class i want to add items in a listview which is in the frmMain. but it doesn't work : its doesn't add but if u put a messagebox IN the same method, the msg box is shown but the listview are not added... frmMain.cs (winform) public void AddUserInList(ListViewItem user) { MessageBox.Show("MainForm marche : " + user.Text ); this.oConsole.Text = user.Text; this.Console(user.Text); this.lstUsers.Items.AddRange( new ListViewItem[] { user } ); } // other method i use in my class IRCEngine. public void Console(string text) { this.oConsole.AppendText("\n" + text); } IRCEngine.cs (my class) frmMain MainForm = new frmMain(); public void Connect() { this.MainForm.Console("-> Connecting..."); try { ... ListViewItem item; // <========================== while (true) { while ((inputLine = reader.ReadLine()) != null) { if (...) { ... } else if (...) { ... foreach (string user in split) { if (user.Length <= 5) { ... } else { item = new ListViewItem(new string[] { user, user.Substring(user.IndexOf('|') + 4)}, -1); MainForm.AddUserInList(item); // <========================== } item = null; } Thread.Sleep(1000); } else { } } } } catch (Exception e) { MessageBox.Show(e.ToString()); } } ListView (lstUsers) & oConsole (richtextbox) are "Public" (modifiers). So why its doesn't append or add items in the form from the class? and why its doesn't add items BUT SHOWS MY MESSAGEBOX which is in AddUserInList() ? Thx a lot :) Quote
mocella Posted January 11, 2005 Posted January 11, 2005 Pass your form instance as a parameter to IRCEngine.Connect, and set that your global "MainForm" equal to this passed param - now you should have addressibility back to your actual form instance. I'm assuming you're calling into IRCEngine from the current instance of frmMain. Quote
MoPraL Posted January 12, 2005 Author Posted January 12, 2005 It doesn't work : private frmMain MainForm = new frmMain(); // i've tester many combinations but nothing.. public IRCEngine() { // something for me _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0"; } public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname) { // HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! this.MainForm = MainForm2; } But on build it says an error : Error : Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' K:\...\IRCEngine.cs It underlines this (only IRCEngine) : public IRCEngine(... It's frmMain (my form) and MaibnForm is the object i wanna use in my class representing my form frmMain. IRCEngine is my class. ALl are in the same namespace Quote
Administrators PlausiblyDamp Posted January 12, 2005 Administrators Posted January 12, 2005 How and where is frmMain declared? If possible could you attach the project (minus the .exes ) to see if that makes it easier for people to diagnose. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
forgottensoul Posted January 12, 2005 Posted January 12, 2005 (edited) It doesn't work : Error : Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' K:\...\IRCEngine.cs I have run into this error. Pass the the form as a generic form and then cast it as your form. I.E. private frmMain MainForm; public IRCEngine() { // something for me _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0"; } public IRCEngine(System.Windows.Forms.Form MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname) { // Now cast the passed var to your global one. // Might need the namespace in front of the frmMain if its not in // the current namespace. this.MainForm = (frmMain)MainForm2; } Hope that helps Edited January 12, 2005 by forgottensoul Quote
RobEmDee Posted January 12, 2005 Posted January 12, 2005 (edited) MoPraL: The error message you are receiving is telling you that the accessibility of the frmMain object itself is more strict than the constructor for IRCEngine. Your frmMain object class declaration must me internal or even private? Just make your frmMain class declaration public (jut to clarify: not your local MainForm variable, the frmMain class itself). Edited January 13, 2005 by RobEmDee Quote
MoPraL Posted January 13, 2005 Author Posted January 13, 2005 i've tested all your solutions but i have always the same error! frmMain is my WinForm.. so i cant say "public partial class" because if i do it, it shows me an other error : "Can't write the output file..." so i can't! habitually, it was easy to pass a form as parameter but in this case... ps: it's impossible to change modifiers of a WinForm! the winform's controls have "Modifiers" and i can put Public, Private, Internal, etc. But the form, we can't :confused: please, help! Quote
IngisKahn Posted January 13, 2005 Posted January 13, 2005 Since IRCEngine's constructor is public and contains frmMain then the definition of frmMain must be public. Quote "Who is John Galt?"
RobEmDee Posted January 14, 2005 Posted January 14, 2005 MoPral: I have many, many Forms that are internal or private. You can definitely control the accessibility of a Form just like any other class. Any time I have received the error message you have depicted, it was because the accessibility of a method parameter was more strict than the accessibility of the method accepting the parameter. Could you post your frmMain code and/or the code where the IRCEngine constructor is being called from? Quote
MoPraL Posted January 14, 2005 Author Posted January 14, 2005 Ok : frmMain.Designer.cs namespace SKYClient { partial class frmMain { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing && (components != null)) ............. (that continue) frmMain.cs #region Using directives using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Threading; using SKYClient; #endregion namespace SKYClient { partial class frmMain : Form { private IRCEngine IRC; private Thread tCnx; public frmMain() { InitializeComponent(); } private void btnConnect_Click(object sender, EventArgs e) { Console("Appuie sur le bouton CONNECT..."); txtHote.Text = txtPseudo.Text + "|" + cmbAge.Text + cmbDept.Text + cmbSexe.Text.ToLower().Substring(0, 1) + "0!" + txtUtilisateur.Text; Console("Host créé!"); IRC = new IRCEngine(this, cmbServer.Text, txtPort.Text, txtPseudo.Text, cmbDept.Text, cmbSexe.Text, cmbAge.Text, txtUtilisateur.Text, txtHote.Text, txtNomReel.Text); tCnx = new Thread(new ThreadStart(IRC.Connect)); tCnx.Start(); } IRCEngine.cs : #region Using directives using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Net; using System.Net.Sockets; using System.Threading; using System.IO; using System.Windows.Forms; #endregion namespace SKYClient { public class IRCEngine { public Form mainForm = new frmMain(); private frmMain MainForm; public IRCEngine() { // On créé le pseudo complet _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0"; } public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname) { //this.MainForm = formMain; ? this.MainForm = (frmMain)MainForm2; // On créé le pseudo complet _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0"; } The error is the same : public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname) ---> Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' I don't udnerstand? How to put my frmMain class (real class frmMain.cs) a public class? i put public before all partial class? thanks for help! Quote
pendragon Posted January 14, 2005 Posted January 14, 2005 The way I've done this in the passed is to change [CS] public Form mainForm = new frmMain(); [/CS] to [CS] public frmMain mainForm; [/CS] Then you should only need to do [CS] this.MainForm = MainForm2; [/CS] Quote
MoPraL Posted January 14, 2005 Author Posted January 14, 2005 I have always the same error AND a new error. (my new code): namespace SKYClient { public class IRCEngine { public frmMain mainForm; public IRCEngine() { // On créé le pseudo complet _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0"; } public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname) { // On instancie la frmMain //this.MainForm = formMain; this.mainForm = MainForm2; Errors : - Inconsistent accessibility: field type 'SKYClient.frmMain' is less accessible than field 'SKYClient.IRCEngine.mainForm ----> here : public frmMain mainForm; - Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' ----> here : public IRCEngine(frmMain MainForm2,...) Quote
Administrators PlausiblyDamp Posted January 15, 2005 Administrators Posted January 15, 2005 you will need to change the definition of the frmMain class to remove the error. public partial class frmMain : Form Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.