如何在VB语言项目中创建一个对象传递给C#语言项目 [英] How to create an object in VB language project to pass to C# language project

查看:58
本文介绍了如何在VB语言项目中创建一个对象传递给C#语言项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从VB到C#项目发送大约10个字段值。从VB解决方案,我需要传递要在C#解决方案中使用的值。我需要在哪个解决方案中创建对象,我是否应该在两个项目中创建相同的对象。



我尝试了什么:



目前,我将值作为逗号分隔参数传递,但我更愿意将它们作为单个对象而不是多个参数发送。



 VB项目代码与此类似:
objCSharpClass.MethodName(t1.Text,t2.Text,...,t10.Text)

C#项目代码与此类似:
void MethodName(string t1,..... string t10)
{
//逻辑此处使用传递的参数进行检查WindowForm打开
}

我想传递一个对象而不是十个参数。

解决方案

< blockquote>不,只需在VB或C#中创建一个类来编译项目,并在另一个中引用它。就.NET而言,定义类的位置并不重要,它可以在两者中使用它。


update ...假设:



0. VB解决方案有一个引用设置为已编译的C#项目(.dll或.exe)



1.一个VB .NET解决方案/项目名为'VBApp_2019



a。 'VBApp_2019设置为启动,并有一个名为'Form1的表单,启动表单



b。 'VBApp_2019有1o TextBoxes,用户可以编辑:'TextBox1 ... 10



c。 'VBApp_2019有一个Button,'Button1



2.在VB解决方案中:一个名为'WFrmApp_2019的C#项目,带有一个Form,'CSharpForm1,带有10个TextBox用户可以编辑:'TextBox1 ... 10



VB解决方案

 Public Class Form1 

Private Dim wfform

Private Sub Form1_Load(sender As Object,e As EventArgs)Handles MyBase.Load
wfform = New WFrmApp_2019.CSharpForm1'使用对C Project的引用
End Sub

Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click

Dim txtdata As Dictionary(Of String,String)= new Dictionary(Of string,string)

For Each c As Control in Controls
如果TypeOf c是TextBox
txtdata.Add(c.Name,c.Text)
End if
Next

wfform.ReceiveData(txtdata)

wfform.Show

End Sub

End Class

C#Project:

使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;

namespace WFrmApp_2019
{
public partial class CSharpForm1:Form
{
public CSharpForm1()
{
InitializeComponent() ;
}

public void ReceiveData(Dictionary< string,string> data)
{
//找到名称与VB控件相符的控件
foreach (数据中的var kvp)
{
控制c = this.Controls.Find(kvp.Key,false)[0];

c.Text = kvp.Value;
}

this.BringToFront();
}

private void Form1_Load(object sender,EventArgs e)
{

}
}
}

原帖...



您是在谈论加载 VB项目中定义的一组静态值到设计中-time C#project ...或者,你是在谈论在正在运行的VB应用程序和正在运行的C#应用​​程序之间交换数据吗?



这两项任务之间存在很大差异。



当你说:发送大约10个字段值,这表明,对我来说,你说的是运行程序。



所以,澄清:尽你所能。


I need to send around 10 field values from VB to C# project. From VB solution, I need to pass the values to be used in C# solution. In which solution do I need to create the object, should I create same object in both projects.

What I have tried:

Currently, I am passing values as comma separated parameters, but I would prefer to send them as a single object instead of multiple parameters.

VB project code is similar to this:
objCSharpClass.MethodName(t1.Text, t2.Text, .. . . , t10.Text)

C# project code is similar to this:
void MethodName(string t1, ..... string t10)
{
// logic here which checks using parameters passed for which WindowForm to open
}

I want to pass a single object instead of ten parameters.

解决方案

No, just create a class in either VB or C# compile the project, and reference it in the other. As far as .NET is concerned, it doesn't matter where you define the class, it can use it in both.


update ... assuming:

0. the VB solution has a reference set to the compiled C# Project (.dll or .exe)

1. a VB.NET solution/project named 'VBApp_2019

a. 'VBApp_2019 is set as the startup, and has a Form named 'Form1, the startup Form

b. 'VBApp_2019 has 1o TextBoxes the user can edit: 'TextBox1 ... 10

c. 'VBApp_2019 has a Button, 'Button1

2. within the VB solution: a C# Project named 'WFrmApp_2019, with a Form, 'CSharpForm1, with 10 TextBoxes the user can edit: 'TextBox1 ... 10

The VB Solution

Public Class Form1

    Private Dim wfform

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        wfform = New WFrmApp_2019.CSharpForm1 'use the reference to the C Project
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim txtdata As Dictionary(Of String,String) = new Dictionary(Of string, string)

        For Each c As Control In Controls
            If TypeOf c is TextBox
                txtdata.Add(c.Name, c.Text)
            End If
        Next

        wfform.ReceiveData(txtdata)

        wfform.Show

    End Sub

End Class

The C# Project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WFrmApp_2019
{
    public partial class CSharpForm1 : Form
    {
        public CSharpForm1()
        {
            InitializeComponent();
        }

        public void ReceiveData(Dictionary<string, string> data)
        {
            // find the controls whose names match the VB Controls
            foreach (var kvp in data)
            {
                Control c = this.Controls.Find(kvp.Key,false)[0];

                c.Text = kvp.Value;
            }

            this.BringToFront();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

original post ...

Are you talking about loading a set of static values defined in a VB project into a design-time C# project ... or, are you talking about exchanging data between a running VB app, and a running C# app ?

There are great differences between those two tasks.

When you say: "send around 10 field values," that suggests, to me, you are talking about a running program.

So, clarify: be as specific as you can.


这篇关于如何在VB语言项目中创建一个对象传递给C#语言项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆