Korzystanie z obiektów w funkcjach

0

Witam,

dopiero zaczynam z C# i mam takowy problem - po utworzeniu obiektu:

SerialPort port = new SerialPort(portcom, speed, Parity.None, 8, StopBits.One);

mogę korzystać z np:

port.Open(); 
port.Close(); 

lecz gdy chciałbym buttonem wywołać kod:

 		void Button4Click(object sender, EventArgs e) {
			port.Write("Tekst");
		}

Otrzymuję komunikat:
Nazwa „port” nie istnieje w bieżącym kontekście. (CS0103)

Jak korzystać z port.* we wszyskich funkcjach?

0

A gdzie tworzysz ten port? Pewnie w jakiejś metodzie.
Daj takie coś jako pole klasy

SerialPort port;

i później to w metodzie

port = new SerialPort(portcom, speed, Parity.None, 8, StopBits.One);

A najlepiej przeczytaj jakiś tutorial.

0

Wstawiam pełny kod:

 using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
using System.Management;
using System.Diagnostics;


namespace COMPORT
{
	/// <summary>
	/// Description of MainForm.

	/// </summary>
	public partial class MainForm : Form
	{
		Int32 speed = 9600;
		string portcom = "COM6";
		
		public MainForm()
		{
			 InitializeComponent();
			 this.MaximumSize = this.Size; //zmiana rozmiarów okna
    		 this.MinimumSize = this.Size; 
			 label3.Text = "Nie wybrano szybkości trasmisji";
			 label4.Text = "Nie wybrano portu szeregowego";
			 label4.Text = Convert.ToString(cpuUsage);
		}
		
		void polacz() {
			SerialPort port = new SerialPort(portcom, speed, Parity.None, 8, StopBits.One);
			port.Open();
			port.Write("123");
		}
		

		void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
		{
			label3.Text ="Predkość " +  comboBox1.Text;
			speed = Convert.ToInt32(comboBox1.Text);  //predkość juz w int
		}
			
		void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
		{
			portcom = comboBox2.Text;
			label4.Text= "Port " + portcom;
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			Close();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			//port.Close();			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			polacz();
		}
		
		void Button4Click(object sender, EventArgs e)
		{
			//port.Write("Tekst");
		}

	}
}

Jak korzystać z metod np: port.Write("Tekst"); poza funkcją gdzie został stworzona klasa?

0

Tak jak napisałem w pierwszym poście.
SerialPort port; - Wrzucasz to obok speed i portcom
W połącz() ma być port = new SerialPort(portcom, speed, Parity.None, 8, StopBits.One);

0

Dziękuję, teraz zrozumiałem i działa, lecz pojawił się nowy problem - po kliknięciu w uruchomiane aplikacji na button z port.Close() otrzymuję poniższy komunikat:

 System.NullReferenceException: Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.
   w COMPORT.MainForm.Button2Click(Object sender, EventArgs e) w c:\Users\Patryk\Documents\SharpDevelop Projects\COMPORT\COMPORT\MainForm.cs:wiersz 69
   w System.Windows.Forms.Control.OnClick(EventArgs e)
   w System.Windows.Forms.Button.OnClick(EventArgs e)
   w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   w System.Windows.Forms.Control.WndProc(Message& m)
   w System.Windows.Forms.ButtonBase.WndProc(Message& m)
   w System.Windows.Forms.Button.WndProc(Message& m)
   w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   w System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
0

A czy wcześniej została wywołana metoda polacz()?

0

Tak, oczywiście.

1 użytkowników online, w tym zalogowanych: 0, gości: 1