所以我将方法class Program {
public static void Run() {
IWebDriver driver = new ChromeDriver;
}
public static void Do() {
driver.FindElement(By.Name("search"));
}
}
从一个类引用到另一个类
代码示例:
Do()
但在main()
驱动程序中当前上下文中不存在
我想在单独的类中对驱动程序执行命令,以便我可以在 Sub Unzip5()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
Dim I As Long
Dim num As Long
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=True)
If IsArray(Fname) = False Then
'Do nothing
Else
FileNameFolder = "D:\Template\test\"
Set oApp = CreateObject("Shell.Application")
For I = LBound(Fname) To UBound(Fname)
num = oApp.Namespace(FileNameFolder).Items.Count
For Each fileNameInZip In oApp.Namespace(Fname(I)).Items
If fileNameInZip Like "repo*" Then
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname(I)).Items.Item(CStr(fileNameInZip))
'this above line working fine but not copying file from zip
Exit For
End If
Next
'oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname(I)).Items
Next I
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
答案 0 :(得分:3)
这是您的代码中的范围问题。至少在您提出的代码中,您需要执行以下操作。
class Program {
public static IWebDriver driver;
public static void Run() {
driver = new ChromeDriver;
}
public static void Do() {
driver.FindElement(By.Name("search"));
}
}
虽然在你的问题的措辞中,听起来好像你把类与函数混淆了。
要共享功能,需要遵守Class或Namespace的范围。要与其他类共享值,您必须以可变格式或类似的方式传入该类的实例。
答案 1 :(得分:2)
首先,您的班级存在问题,由@jameswhyte和@alwayslearning解答。
首先应该知道变量在类中如何工作才能使其正常工作。这个时候,我会解决你的问题。
如何从其他类引用方法
我们将使用您的示例。
首先,您必须将子类声明为public,以便从主类中检测它 其次,您必须声明您的方法(静态或非静态是由您决定,但在您的情况下,您使用静态方法)
public class Program {
public static IWebDriver driver; //this become a global variable
public static void Run() {
driver = new ChromeDriver; //But this seems to initialize the driver
//rather than to run it
}
public static void Do() {
driver.FindElement(By.Name("search"));
}
}
我不知道你在这里的目标是什么,但看起来它更好用对象而不是静态访问。无论如何,这里是如何访问静态方法
public class MainClass
{
//You can do this way directly since program has a static method
Program.Run(); // this part initialized the static driver
Program.Do(); // this part used the driver to findElement;
}
这是使用实例化从另一个类访问方法的另一种方法 这是你的非静态方法和变量 公共课程{ public IWebDriver驱动程序; //这成为一个全局变量
public class Program
{
public IWebDriver driver;
public void Run() {
driver = new ChromeDriver; //But this seems to initialize the driver
//rather than to run it
}
public static void Do() {
driver.FindElement(By.Name("search"));
}
}
在MainClass中
public class MainClass
{
//instantiate the other class
Program myProgramClass = new Program(); // this is your reference to Program class
//then use it this way
myProgramClass.Run(); // this part initialized the driver
myProgramClass.Do(); // this part used the driver to findElement;
}
请注意,您的驱动程序未初始化,因此您需要先在Do之前调用Run方法,否则您将捕获有关未初始化变量的异常
您可以自己做一些实验。使用构造函数,实例化,静态方法和变量等