I defined a class in the "Datasource.cs" file. In my another asp.net file "FineUIGrid.aspx.cs" I need to quote the class's method I defined in the Datasource.cs. But when I quote the method there is an error "the name 'class1' does not exist in the current context". How can solve this problem?
Datasource.cs code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace FineUI.Examples
{
public class Class1
{
public Class1()
{
}
public string write()
{
string AD = "This is a FineUI DataGrid Example";
return AD;
}
}
}
FineUIGrid.aspx.cs code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace FineUIProject
{
protected void page_load(object sender, EventArgs e)
{
string information = Class1.write();
}
}
You need to add a reference to FineUI inside FineUIProject then include the using statement below. After that you need to instantiate class1 and call it on the instanciated object
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using FineUI.Examples;
namespace FineUIProject
{
protected void page_load(object sender, EventArgs e)
{
Class1 classOne = new Class1();
string information = classOne.write();
}
}
Either instantiate the class or make the method static.
Instantiated:
var class = new Class1();
var information = class.write();
Static:
//Class1
public static string write()
{
string AD = "This is a FineUI DataGrid Example";
return AD;
}
// no change
string information = Class1.write();
Since there is no member variables being used by Class1 used to return the result, a static method would most likely be the best course of action. You may want to read C# static vs instance methods.
Related
I can't figure what is the wrong with my code below. Compiling on Linux/raspberry using Mono,mcs.
When I try to compile using (mcs) I get the message:
FTDI_Mgt.cs(15,38): error CS0246: The type or namespace name
`FTDI_Mgt' could not be found. Are you missing an assembly reference?
This is part of my code in a file named FTDI_Mgt.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.IO;
using FTD2XX_NET;
using myGenericTool;
namespace ST_NBIOT_TOOL
{
public partial class FTDI_Mgt1: FTDI_Mgt ///// line 15
{
//..
public FTDI_Mgt() ////// line 51
{
Init_timerGetData();
}
private void Init_timerGetData()
{
timerGetData = new Timer();
timerGetData.Interval = 5;
timerGetData.Tick += new EventHandler(ReadData_Tick);
}
My Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ST_NBIOT_TOOL
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FTDI_Mgt1());
}
}
}
I tried by changing Application.Run(new FTDI_Mgt1()) to Application.Run(new FTDI_Mgt()) AND ligne 15 in FTDI_Mgt.cs to public class FTDI_Mgt
I got this error:
FTDI_Mgt.cs(51,16): error CS1520: Class, struct, or interface method
must have a return type
Any idea please? Any help? Did I miss something? I am not an expert in C#!
Thank you in advance
I am trying to access to a public static variable in a class but I don't know how to do it. I try different solutions like using and namespace but doesn't work.
In the form called "CategoriasCaracteristicas.cs" (blue), I am trying to access to empresaGlobal.cs (red) as you can see in the next image:
But I can't do it using namespace, or others. The code of the file "CategoriasCaracteristicas.cs" that is where I am trying to access the other class is the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using Utils;
namespace imlnet
{
public partial class CategoriasCaracteristicas : Form
{
private string codEmp;
private string cadenaConexion;
public CategoriasCaracteristicas()
{
And this is the code of empresaGlobal.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ImeApps
{
public static class empresaGlobal
{
public static string empresaID = "3";
public static String EmpresaID
{
get { return empresaID; }
set { empresaID = value; }
}
}
}
Thanks in advance!
Put your empresaGlobal.cs file in a project and add that project as a reference to the GestorCategoriasCaracteristicasIngenieria project.
Make sure any classes, properties and methods you need to access are available using the public keyword.
I have this C# class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Handler.FTPClient
{
public class FTP
{
private static string _message = "This is a message";
public static String message
{
get { return _message; }
set { _message = value; }
}
public FTP()
{
}
public static String GetMessage()
{
return message;
}
}
}
And am trying to call it with this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Handler.FTPClient;
namespace swift_ftp
{
public partial class FTP : Form
{
FTP ftp;
public FTP()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void FTP_Load(object sender, EventArgs e)
{
ftp = new FTP();
MessageBox.Show(FTP.message);
}
}
}
But for some reason, I am unable to call the property, is there something I have not done? I have also done this without static and got the same result -_-
Sorry for the stupid and basic question, it's just been such a while since I have done C#!
(I have added the ref to the second project which is where the ftp class library is held)
You have two classes with the same name. Try
ftp = new Handler.FTPClient.FTP()
to make sure you instantiate the right class.
Don't forget the variable in the class itself:
Handler.FTPClient.FTP ftp;
You should avoid the same name for different classes for exactly this reason. Since your FTP class inherits from a Form I assume it is a window ... so refactor it to "FTPWindow" and you have less problems and the code is easier to read.
Partial classes are only possible in the same assembly and the same namespace. Your (partial) classes reside in different namespaces.
See https://msdn.microsoft.com/en-us/library/wa80x488.aspx
How to use MessageBox in class library?
Here is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
MessageBox.Show("test");
}
}
i can load MessageBox but can't have show property, MessageBox.Show("test"); <-- fail
You should NOT use a Windows forms MessageBox inside a class library. What if you use this library in an ASP.NET application. The MessageBox will be shown in Webserver. And your webserver will be waiting (hung) untill someone responds to that MessageBox in webserver.
An ideal design would be that you either return the message as string and deal with that string in caller specific way or throw an exception if thats what you want.
If you still want then here is your code corrected
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
}
}
You have the call to messagebox outside any method.
This code cannot be compiled at all.
You should write
namespace MessageBoxes
{
class ShowInfo
{
public void ShowUserMessage(string messageText)
{
MessageBox.Show(messageText);
}
}
}
and then call it after instancing an object of type ShowInfo
ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");
Additional Answer to this Question:
After the Class Library Project has been created.
Right Click your Project Add > New Item > Windows form
it's done by adding reference System.Windows.Forms.dll
Make sure you actually use the the class in the main form.
class ShowInfo
{
public static void show()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
...
public Form1()
{
InitializeComponent();
ShowInfo.show();
}
Basically, I'm trying to move the contents of WriteLine to a Label box in a WinForm, as an intro to object oriented programming. I believe I have some syntax error, and I know the method I have the writeline in is void. So, any help with getting this to work is appreciated. This is just one of the attempts I've made.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld.Program;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string words = new ConsoleHelloWorld.Program.Main(words);
label1.Text = words;
}
}
}
This is the code I'm referencing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static void Main(string[] args)
{
string words = "hello world";
Console.WriteLine(words);
Console.ReadLine();
}
}
}
Your words variable is a local one, i.e. its scope is the method where it is declared, that is Main. Outside that method you cannot reference it.
For the variable to be accessible, you need to make it a public field (or a property of the class). If you need to access it without having an instance of that class type, than this field should be declared static. Then your code would look like this:
public static class Program
{
public readonly static string Words = "hello world";
//...
}
Assuming the windows application project has a reference to the console application project in the windows application form you may write:
string words = ConsoleHelloWorld.Program.Words;
Also you don't need to launch the console application, besides you won't manage to do it like this (consider the answer by Cuong Le, if you meant to launch both Console and Windows applications).
Got it to work. Turns out I was rooting down incorrectly. (I had thought rooting worked a bit more like the android file structure.) Basically, I created a separate class for holding the Hello World variable, and just called that for whether I wanted to print the strings to a console or WinForm app.
The initial block of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static class Hello
{
public static string words = "Hello World";
}
public static void Main(string[] args)
{
string word = ConsoleHelloWorld.Program.Hello.words;
Console.WriteLine(word);
Console.ReadLine();
}
}
}
The WinForm calling the class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld; //Program.Hello;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string word = ConsoleHelloWorld.Program.Hello.words;
label1.Text = word;
}
}
}
In future projects I'll just keep the code being run by various environments in it's own project.