Passing Value Between Two Project - c#

I have two project and having trouble on passing value between two project. As usual I have passed the file reference between project.
My project Details Is:
Project1 Project2
All forms and object Only 1 MDI Forms Containing ManuStrip
I wants to read the data of MDI Forms on showing the project1 forms
The Example is as below:
//This is on Project2 MDI Forms
private void accountMasterToolStripMenuItem_Click(object sender, EventArgs e)
{
INVOICE1.Form24 f24 = new INVOICE1.Form24();
f24.PFrom.Text = label4.Text;
f24.PTo.Text = label5.Text;
f24.Namee.Text = textBox1.Text;
f24.ID.Text = label6.Text;
f24.ShowDialog();
}
I Have Created the Properties for the same on project1 forms
public Label PFrom
{
get { return label14; }
set { label14 = value; }
}
public Label PTo
{
get { return label16; }
set { label16 = value; }
}
public Label Namee
{
get { return label2; }
set { label2 = value; }
}
public Label ID
{
get { return label3; }
set { label3 = value; }
}
The value passed from MDI To Project1 is not showing on Form24 of Project1. There are no error. The Form24 Showing without value which are passed from MDI Form.
Why The value not showing on form24 of project1 ?. And What is the Solution?.

You may have forgotten to add a project reference to Project1 in Project2. In the Solution Explorer, right-click Project2 and select "Add Reference", then under "Projects" select Project1.
Also, if the two projects have different namespaces, you'll need to put
using Project1; // replace "Project1" with the namespace of your Project1
At the top of the Project2 source file.
If there are no compiler errors then the problem is not likely to be with project references. Perhaps you have some code in the Form24 constructor or Load event which is clearing those labels
As a side note, instead of exposing the Labels as properties, just expose their Text property:
public string PFrom
{
get { return label14.Text; }
set { label14.Text = value; }
}

I have faced with the same problem..and answer was simple: it is impossible. However you can crack this situation...using database or shared solution in which you will establish communication between 2 projects. Or use 3 project and create communication driver, which will be used in 1 and 2 project. It does't meter how you will do this.
situation: database
situation: shared solution with communication protocol
perhaps creating DLL will also help
My personal solution was this: (Tested on real website and separate background project)
To create shared project, use VS template (shared project). Then create your class and inside each project include in project reference section your shared project. So for example Project A, Project B , Project SharedPr -> contains communication protocol
Project A -> references-> Add Reference -> Shared Project.
Project B -> references-> Add Reference -> Shared Project.
That communication protocol it is something like a driver which you need to create.
Easily to do this by FILE. Create Hidden file in which project A writes and project B reads. Store in your file json string or json array then read all lines and deserialize everything for example with Newtonsoft.Json NuGet package.
I hope this will help.

Related

Get assembled from another project

I have a solution contains 2 projects
windows form application -> WFA
library of user controls -> LUC
Solution
|-Solution.Presentation -> WFA
|-Solution.Controls -> LUC
In LUC I have a control with a property that must show the host project assembly (here my problem is that I do not know how to do it).
public class customControl : TextBox
{
public string HostAssemblyName
{
get { /* HELP => should return Solution.Presentation (must be generated dynamically because the name of the project where it will be hosted is not known) */ }
}
}
The WFA project makes use of the LUC project:
Solutiono
|-Solution.Presentation
|-References
|-Solution.Controls
Any ideas?
Thanks

Sharing code between two c# assemblies

1) I have a project called SocketServer in which there is a Class Room, this project is a complied exe and can also be complied as a DLL.
2) In Another project called, lets say, MyGame, there is a class called MyAwesomeGame, in which I need to do something like:
public class MyAwesomeGame
{
public Init(Room room)
{
//I can get data from Room
}
}
Now MyGame Project is compiled into a MyGame.DLL, and is placed somewhere relative to the SocketServer.exe, which at runtime loads MyGame.DLL and Instantiates MyAwesomeGame class and calls the Method Init and passes room as its parameter. Code is SocketServer project is something like:
public class Room
{
private InstantiateGameRoom()
{
//Load External MyGame.DLL Assembly
Task<MyAwesomeGame> task = Task<MyAwesomeGame>.Factory.StartNew(() => (MyAwesomeGame)Activator.CreateInstance(classType, new object[] { this }));
MyAwesomeGame instance = task.result;
instance.init(this);
}
}
So my question is how can I get reference of the Class room in MyGame Project? Should I add reference of my SocketServer Project? and if I do, wont it get complied into my MyGame.dll?
p.s: I also intend to distribute the socketserver.dll as an API to thrid-party users.
You should add the SocketServer's generated dll to the references of the MyGame project. Right click on MyGame, click Add, click References..., in the dialog select Projects and select the SocketServer project.
I believe it is also wise to make sure that SocketServer is a depedency for the MyGame project to make sure SocketServer is built before MyGame.
--edit: please read the comment below that I made after Sushant's clarification. Or go straight to Sid's answer. :-)
I think the proper way to share Models among two or more projects is to extract all the models and relative logics to a separate project.
Then you can reference that project from every project that needs to use that data.
The scenario I am talking about is the following:
ModelsProject (Contains models and business logic)
ProjectA (has a dependency on ModelsProject)
ProjectB (has a dependency on ModelsProject)
Scheme

String from getter is empty when called in another Project C#

I am a total beginner in C# programming language. I am trying to use Getter and Setter in order to set the string in ProjectA and the retrieve it in Project B.
Project B uses Windows Forms, and I wasnt to set the value of TextBox
with the retrieved string.
Project A is a Console Project and it just reads out some stuff from
file and stores it in string, which I want to retrieve.
However, this is my call in Project B:
string cardOwner = Transmit.Program.CardOwner;
Debug.WriteLine("Card owner = " + cardOwner);
tb_cardholder.Text = cardOwner;
And this is my Getter / Setter in Project A:
private static string _cardOwner;
public static string CardOwner
{
get
{
return _cardOwner;
}
set
{
_cardOwner = value;
}
}
_cardOwner = System.Text.Encoding.ASCII.GetString(bCardOwner);
But in Project B I get "" empty string.
I have included Project A in Project B (added Reference and wrote "using ProjectA").
Any ideas what's going wrong?
Thanks.
Just because you include a project and use its classes in your project B, it doesn't mean that you also use the instances of these classes.
Take the following class:
public class Test
{
public string Message { get; set; }
}
You can put this class into a DLL project (Tools) and reference it from other projects, like a WinForms project ProjectA and a console project ProjectB.
In both projects, you can write something like:
Test t = new Test() { Message = "Hello" };
That creates a new instance of the Test class, but the two running applications ProjectA and ProjectB do not exchange the data! They are completely separated.
The same is true for class properties.
You can't share information between two different applications so easily. Static properties only share data within the same Application Domain, that is in most simple constellations within the same Windows process.
If you want to transfer data between two different processes, you need to use an explicit mechanism for interprocess communication.
When is this line executed?
_cardOwner = System.Text.Encoding.ASCII.GetString(bCardOwner);
You'll need to put that in a method and call that method (and knowing when the call happens will help you understand why _cardOwner is not set:
public static void Init()
{
_cardOwner = System.Text.Encoding.ASCII.GetString(bCardOwner);
}
Then call this method somewhere that you know will be executed before you need _cardOwner:
Transmit.Program.Init();
string cardOwner = Transmit.Program.CardOwner;
tb_cardholder.Text = cardOwner;

Accessing class members defined in another *.cs file

I'm writing an update checker for my program and I'm using xml from a remote server. The request is working fine and it does what I want. The problem is that I can't call the function from another file. (See code below)
The files
home.cs - The place i want to call the RequestVersion()
version.cs - Where the RequestVersion() is located
The code
version.cs
namespace MyName
{
class version
{
public string[] RequestVersion()
{
XmlDocument doc = new XmlDocument();
try
{
string[] version_data = new string[3];
doc.Load("link_here");
foreach (XmlNode node in doc.DocumentElement)
{
string version = node.Attributes[0].Value;
string date = node["date"].InnerText;
string changelog = node["changelog"].InnerText;
version_data[0] = version;
version_data[1] = date;
version_data[2] = changelog;
return version_data;
}
}
catch (Exception xml_ex)
{
}
return null;
}
}
}
(returns an array)
home.cs
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
//This is the place from where i want to access the array!
}
And my XML structure:
<?xml version="1.0" encoding="UTF-8"?>
<SerialMate>
<release version="1.0.0">
<date>12-10-2014</date>
<changelog>Test</changelog>
</release>
</SerialMate>
(I'm not adding any new <release> tags on the xml so it always has 1)
The question
So, my question is: How do it access the array elements from the RequestVersion() within home.cs?
I don't really understand your problem, but:
version v = new version();
string[] s = v.RequestVersion();
Referencing code within other files and projects
Within the same project it makes absolutely no difference whether the code is in the same or in different files. The only things which matter are the access modifiers (public, protected, internal, and private).
If the two code pieces are in different projects, then the compiled code will be compiled into two different assemblies (*.exe or *.dll). Therefore one project will have to reference the other one. Typically the start up project (*.exe) will reference a class library project (*.dll).
If the two projects are within the same solution, you can add a so called project reference. Right click on the class library project in the solution explorer and click “Copy As Project Reference”. In the startup project, right click on “References” and click “Paste Reference”.
If the two projects are within different solutions you will have to add a reference to the class library DLL (usually the one in bin/Release) from within the startup project. Right click “References” and click “Add Reference…”. In the references dialog choose “Browse” and select the DLL.
Also make sure not to create circular dependencies (project A references project B and project B references project A). If you have such a dependency, you can usually resolve it by placing the code that has to be accessed by the two projects into a third project C. Then change the references to: A references C and B references C.
Calling the method of another class
Types (a class in your case) and their members (properties, methods, events …) must be declared as public in order to be accessible from other projects. Within the same project they can also be declared as internal. (They can also be declared as protected if you want to derive new classes.)
If a member is declared as static or if it is a constant, you can access it by specifying the type name dot the member name:
var result = MyClass.MyMethod();
If the member is an instance member you must call it from an instance of the type (an object):
MyClass obj = new MyClass();
var result = Obj.MyMethod();

Converting a C# Console App to a DLL

I am rewriting the Betfair API to JSON from SOAP and I have started off the way I did it before as a console APP which is then called from a task scheduler or win service.
However now I have been asked to do various different jobs with the code and I don't want to write a console app for each job (different sites want prices, bets placed etc)
The new codebase is much larger than the old one and I would have been able to copy the 4 files from the old system into a DLL app and then create various console apps/services to implement the DLL - however because it's 40+ files I don't want a copy n paste job if possible.
Is there a way I can EASILY convert an existing console project into a class / DLL project with some tool or command in VS?
I want to be able to just then create simple apps that just go
BetfairBOT myBOT = new BetfairBOT()
myBOT.RunGetPrices();
or
BetfairBOT myBOT = new BetfairBOT()
myBOT.RunPlaceBets();
e.g 2/3 lines of code to implement my DLL that is registered to my app.
So without copy and paste can I do this.
I am using VS 2012, .NET 4.5 (or 4.0 if I need to depending on server), Windows 8.1
Any help would be much appreciated.
This answer is from here. while it used winforms instead of console application, I think you will be able to use it.
Steps for creating DLL
Step 1:- File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory click OK
After Clicking on button ‘OK’, solution explorer adds one C# class ‘Class1.cs’. In this class we can write our code.
When we double click on Class1.cs, we see a namespace CreatingDLL. We will be use this namespace in our project to access this class library.
Step 2:- Within Class1.cs we create a method named ‘sum’ that takes two integers value and return sum to witch method passed numbers.
using System;
namespace CreatingDLL
{
public class Class1
{
/// <summary>
/// sum is method that take two integer value and return that sum
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int sum(int x, int y)
{
return x + y;
}
}
}
Step 3:- Now build the Application and see bin\debug directory of our project. ‘CreatingDLL.dll’ is created.
Now we create another application and take this DLL (CreatingDLL.dll) reference for accessing DLL’s method.
Steps for accessing created DLL
Step 4:- File->New->Project->Visual C# Projects->Windows Form Application.
Step 5:- Designed windows form as bellow figure.
Step 6:- Add reference of DLL (CreatingDLL) which we created before few minutes.
After adding reference of DLL, following windows will appear.
Step 7:- Write code on button click of Windows Form Application. Before creating object and making method of Add DLL, add namespace CreatedDLL in project as bellow code.
using System;
using System.Windows.Forms;
using CreatingDLL;
namespace AccessingDLL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
Class1 c1 = new Class1();
try
{
txtResult.Text = Convert.ToString(c1.sum(Convert.ToInt32(txtNumber1.Text), Convert.ToInt32(txtNumber2.Text)));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Step 8:- Now build the application and execute project and see output.
Edit: To change an application into a library do these steps
First, double click on Properties inside Solution Explorer window.
Then, On the openned page, change the Output Type from Console Application to Class Library

Categories