Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将字符串转换为类,水晶报表打印_Vb.net_Crystal Reports_Report - Fatal编程技术网

Vb.net 将字符串转换为类,水晶报表打印

Vb.net 将字符串转换为类,水晶报表打印,vb.net,crystal-reports,report,Vb.net,Crystal Reports,Report,我试图使我的代码更完美,我使用Crystal Report,对于一个项目,我使用的代码可以更快地打印Crystal Report,而无需选择打印机,问题是,我使用了很多Crystal Report,我想创建一个可以帮助我的函数,通常我使用这种方法: Select Case string_printer Case "cristale1" cristale1.PrintOptions.PaperSource = CrystalDecisions.Shared

我试图使我的代码更完美,我使用Crystal Report,对于一个项目,我使用的代码可以更快地打印Crystal Report,而无需选择打印机,问题是,我使用了很多Crystal Report,我想创建一个可以帮助我的函数,通常我使用这种方法:

Select Case string_printer

        Case "cristale1"
            cristale1.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
            cristale1.PrintToPrinter(NB_Copy.Value, True, 0, 2)
            cristale1.Close()

        Case "cristale2"
            cristale2.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
            cristale2.PrintToPrinter(NB_Copy.Value, True, 0, 2)
            cristale2.Close()

        Case "cristale3"
            cristale3.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
            cristale3.PrintToPrinter(NB_Copy.Value, True, 0, 2)
            cristale3.Close()

End Select
我尝试做的是这样的事情:

Sub Print_Report(string_printer as String , NB_Copy as Integer)
    Dim class_here =  // CHANGING THE STRING to the Class using the string_printer
    Dim data = Ctype(class_here,ReportClass)
    data .PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
    data .PrintToPrinter(NB_Copy, True, 0, 2)
    data .Close()
End Sub

有什么想法吗?

我的VB有点生锈了,你呢

Dim printer;

Select Case string_printer
    Case "cristale1"
        printer = cristale1;
    Case "cristale2"
        printer = cristale2;
    Case "cristale3"
        printer = cristale3;
End Select

printer.PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
printer.PrintToPrinter(NB_Copy.Value, True, 0, 2)
printer.Close()
埃塔:找到这个了吗

System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(string className)
所以这应该行得通

Sub Print_Report(string_printer as String , NB_Copy as Integer)
  Dim class_here = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(string_printer)
  Dim data = Ctype(class_here,ReportClass)
  data .PrintOptions.PaperSource = CrystalDecisions.Shared.PaperSource.Auto
  data .PrintToPrinter(NB_Copy, True, 0, 2)
data .Close()
End Sub
或者你可以用


你可以试试“对不起”,这是C:

public Printer GetByName(string name) {
    return this.GetType().GetFields().First(f => f.Name == name).GetValue(this) as Printer;
}
。这使用反射,所以如果您经常访问这段代码,可能效率低下。然而,我认为这不会是一个问题。不过,如果可以的话,我建议你坚持你目前的做法

请注意,反射的使用创建了cristale1、2、3字段的不明显用法。这可能应该记录在某个地方,以便您的代码读者确信这些是必要的,并且GetByName就是它们的使用位置


可以找到一个显示基础知识的工作示例。

问题是您使用了大量crystal report?这里的实际问题是什么?通常情况下,人们使用一个crystal report,但在我的情况下,我使用20个crystal report的单一形式和一个crystal report viewer,因此,我尝试创建一个函数来减少基本代码中的大量行谢谢,我昨天已经做过了,但每一份水晶报告的行数还是越来越多have@user3737184添加了一些可能对你有用的。我现在就试试,在1minit中给你反馈给我以下消息:引用对象未设置为对象的实例ligne:Dim class_here=System.Reflection.Assembly.getExecutionGassembly.CreateInstancestring_打印机返回Nothing别担心,我会将其转换为VB,然后我给你我的反馈,Thanks@user3737184可以让我们知道这对你是否有效,对不起,你有没有一个例子可以尝试?如果我介意的话,我必须创建Crystal Report的所有实例,这样我才能调用该方法,对吗?根据我对你的代码的了解,Crystal1,2,3是你类的字段。将上述函数放入类后,您应该能够使用这些字段的名称访问这些字段。
public Printer GetByName(string name) {
    return this.GetType().GetFields().First(f => f.Name == name).GetValue(this) as Printer;
}