C#将变量传递给类,然后以形式返回类输出
本文关键字:返回 输出 然后 变量 | 更新日期: 2023-09-27 17:59:51
我很难思考如何在类之间来回传递数据。我参加了一些大学课程OOP入门、C++入门、C#入门,并完成了一些自定进度的在线教程。我相信我掌握了什么时候使用类,但没有掌握如何使用它们。所以这就是我要做的,我只需要一点关于如何实现这一点的指导。请不要告诉我去谷歌,因为我已经搜索到手指流血了(好吧,不是字面意思),现在只需要更多的内容,比如特定的术语。如果有人能用C#术语向我解释,那将是有帮助的,因为我将有一些特定的东西可以重新开始搜索。
这就是我现在的处境…
我写了一个应用程序,它把所有东西都放在同一个类(Form1)中并让它工作。我使用的是下拉列表中的值,这些值预生成数据库查询并分配一些变量,用于播放视频。
这是我在Form1 中的完整代码
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.SqlServerCe;
namespace Tests
{
public partial class Tests : Form
{
public Tests()
{
InitializeComponent();
using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT DISTINCT ColumnOne FROM main", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while (reader.Read())
columnOneComboBox.Items.Add(reader["ColumnOne"]);
}
}
private void columnOneComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
testsListBox.Items.Clear();
testVideoMediaPlayer.Visible = false;
var columnOne = columnOneComboBox.SelectedItem.ToString();
using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT * FROM main WHERE ColumnOne='" + columnOne + "' ORDER BY ColumnTwo", conn);
SqlCeDataReader reader = comm.ExecuteReader();
while (reader.Read())
testsListBox.Items.Add(reader["ColumnTwo"]);
}
}
private void TestListBox_SelectedIndexChanged(object sender, EventArgs e)
{
var columnTwo = testsListBox.SelectedItem.ToString();
using (var conn = new SqlCeConnection("Data Source=TestDB.sdf;Password=Tr@1n!ng;Persist Security Info=True"))
{
conn.Open();
var comm = new SqlCeCommand("SELECT * FROM main WHERE ColumnTwo='" + columnTwo + "'", conn);
SqlCeDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
bodyPositionRichTextBox.Text = reader["ColumnThree"].ToString();
techniqueRichTextBox.Text = reader["ColumnFour"].ToString();
string videoPath = reader["Video"].ToString();
if (videoPath != "")
{
testVideoMediaPlayer.URL = @"Videos/" + videoPath;
testVideoMediaPlayer.settings.playCount = 1000;
testVideoMediaPlayer.uiMode = "none";
testVideoMediaPlayer.Visible = true;
testVideoMediaPlayer.stretchToFit = true;
testVideoMediaPlayer.fullScreen = false;
}
else
{
testVideoMediaPlayer.Visible = false;
}
}
}
}
}
}
现在,我想做的是创建一个名为MediaPlayer的单独类,但我需要在指定了所有视频控件的MediaPlayer类中使用Form1中的一个变量,然后让From1调用该播放器。以下是我想转到MediaPlayer.cs:的想法
if (videoPath != "")
{
testVideoMediaPlayer.URL = @"Videos/" + videoPath;
testVideoMediaPlayer.settings.playCount = 1000;
testVideoMediaPlayer.uiMode = "none";
testVideoMediaPlayer.Visible = true;
testVideoMediaPlayer.stretchToFit = true;
testVideoMediaPlayer.fullScreen = false;
}
else
{
testVideoMediaPlayer.Visible = false;
}
我是不是搞错了?我不知道如何将字符串变量"Videos"从Test.cs传递到MediaPlayer.cs,也不知道如何从Test.cs调用MediaPlayer.cs。感谢您提供的任何指导。
您的类MediaPlayer如下(替换您的组件类型):
public class MediaPlayer
{
/* your video type */ TestVideoMediaPlayer { get; set; }
public MediaPlayer(/* your video type */ testVideoMediaPlayer)
{
TestVideoMediaPlayer = testVideoMediaPlayer;
}
public void SetMediaPlayer(string url, int playerCount, string uiMode, bool visible, bool stretch, bool fullScreen)
{
if (url != "")
{
TestVideoMediaPlayer.URL = @"Videos/" + url;
TestVideoMediaPlayer.settings.playCount = playerCount;
TestVideoMediaPlayer.uiMode = uiMode;
TestVideoMediaPlayer.Visible = visible;
TestVideoMediaPlayer.stretchToFit = stretch;
TestVideoMediaPlayer.fullScreen = fullScreen;
}
else
{
TestVideoMediaPlayer.Visible = false;
}
}
}
并将您的电话替换为:
if (reader.Read())
{
bodyPositionRichTextBox.Text = reader["ColumnThree"].ToString();
techniqueRichTextBox.Text = reader["ColumnFour"].ToString();
string videoPath = reader["Video"].ToString();
MediaPlayer mp = new MediaPlayer(testVideoMediaPlayer);
mp.SetMediaPlayer(videoPath, 1000, "none", true, true, false);
}