PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Objekt deklarieren in VB


DraconiX
2009-12-13, 20:55:55
Sooo... ich bin echt zu doof!

in C deklariere ich das Objekt folgendermaßen:

private Eq3.LCP100.LCP100 Lcdd

das funktioniert ganz wunderbar und funzt eigentlich so immer so.
Nun muß ich aber diese Bibliothek in Visual Basic einfügen und wollte dies eigentlich so machen:

Dim lcdd As Eq3.LCP100.LCP100

nun nörgelt der debuger rum das ich es als new deklarieren soll... gesagt getan:

Dim lcdd As New Eq3.LCP100.LCP100

Kann mir wer helfen? :D Komisch, für C reichen meine Kenntnisse aber bei VB bin ich komischerweise zu doof :freak: Sollte dort doch einfacher sein. Denke ich.

samm
2009-12-13, 23:39:45
Mit New erzeugst du eine neue Instanz der angegebenen Klasse. In deinem Fall wird der default-Konstruktor aufgerufen.

Wie du in C überhaupt ein Objekt erzeugen kannst, ist mir ohnehin etwas schleierhaft?

Coda
2009-12-13, 23:43:55
Das ist auch garantiert kein C :ugly:

samm
2009-12-13, 23:46:22
Jo, die ganzen Punkte und der fehlende Strichpunkt verwirren mich jetzt irgendwie... Und private vorne dran? Das würde ja auch in C++ ziemlich anders aussehen.

DraconiX
2009-12-14, 01:53:02
Gut gut, es ist C# :wink: erklärt mir aber immernoch nicht mein Problem.

Hier der C# Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace Eq3.LCP100
{
public partial class MainForm : Form
{
private const int INDEX_COUNT = 64;
private const int BAUD_RATE = 38400;
private const int DATA_BITS = 8;
private const Parity PARITY = Parity.Even;
private const StopBits STOP_BITS = StopBits.One;
private const int READ_TIMEOUT = 10000;
private const int PAGE_SIZE = 512;
private const int PAGES_PER_IMAGE = 64;

private Eq3.LCP100.LCP100 lcp100;

private bool isConnected;
private SerialPort serialPort;

public MainForm()
{
InitializeComponent();

isConnected = false;

String[] portNames = SerialPort.GetPortNames();
PortsComboBox.Items.Clear();
if (0 < portNames.Length)
{
PortsComboBox.Items.AddRange(portNames);
PortsComboBox.SelectedItem = PortsComboBox.Items[0];
}
else
{
ConnectButton.Enabled = false;
Disable();
}

IndexComboBox.Items.Clear();
for (byte b = 0; b < INDEX_COUNT; b++)
{
IndexComboBox.Items.Add(b);
}
IndexComboBox.SelectedItem = IndexComboBox.Items[0];

Disable();
}

private void SavePicture(byte index, byte[] image)
{
try
{
int offset = index * PAGES_PER_IMAGE;
for (int i = 0; i < PAGES_PER_IMAGE; i++)
{
byte[] page = new byte[PAGE_SIZE];
for (int j = 0; j < PAGE_SIZE; j++) { page[j] = image[j + (i * PAGE_SIZE)]; }
if (!lcp100.SavePage(offset + i, page)) { throw new Exception(); }
lcp100.ShowPage(offset + i, PAGE_SIZE);
}
}
catch
{
MessageBox.Show("Das Bild konnte nicht gespeichert werden.", "Bild speichern");
}
}

private void Disable()
{
ConnectionLabel.Text = "- nicht verbunden -";
VersionLabel.Text = "n.a.";
IndexComboBox.Enabled = false;
LoadPictureButton.Enabled = false;
SaveButton.Enabled = false;
ShowTestImageButton.Enabled = false;
SwitchBackLightOnButton.Enabled = false;
SwitchBacklightOffButton.Enabled = false;
}

private void Enable()
{
IndexComboBox.Enabled = true;
LoadPictureButton.Enabled = true;
SaveButton.Enabled = true;
ShowTestImageButton.Enabled = true;
SwitchBackLightOnButton.Enabled = true;
SwitchBacklightOffButton.Enabled = true;
}

private void UpdateVersion(Eq3.LCP100.LCP100.Version version)
{
VersionLabel.Text = version.Major.ToString() + "." + Version.Minor.ToString();
}

private void Connect()
{
try
{
String portName = PortsComboBox.Text;
serialPort = new SerialPort(portName, BAUD_RATE,
PARITY, DATA_BITS, STOP_BITS);
serialPort.Open();
Stream stream = serialPort.BaseStream;
stream.ReadTimeout = READ_TIMEOUT;
lcp100 = new Eq3.LCP100.LCP100(stream);
UpdateVersion(lcp100.GetVersion());
ConnectionLabel.Text = "verbunden mit " + portName;
ConnectButton.Text = "Trennen";
Enable();
isConnected = true;
IndexComboBox.SelectedItem = IndexComboBox.Items[0];
}
catch (Exception)
{
MessageBox.Show("Es konnte keine Verbindung aufgebaut werden.",
"Verbinden");
if(serialPort.IsOpen) serialPort.Close();
}
}

private void Disconnect()
{
serialPort.Close();
ConnectionLabel.Text = "- nicht verbunden -";
ConnectButton.Text = "Verbinden";
Disable();
isConnected = false;
}

private void ShowImage(int index)
{
lcp100.SendCommand(0x2C);

for(int i = 0; i < PAGES_PER_IMAGE; i++)
{
int pageNumber = (index * PAGES_PER_IMAGE) + i;
lcp100.ShowPage(pageNumber, PAGE_SIZE);
}
}

private void ConnectButton_Click(object sender, EventArgs e)
{
if (!isConnected) { Connect(); }
else { Disconnect(); }
}

private void IndexComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (isConnected)
{
ShowImage((byte)IndexComboBox.SelectedItem);
}
}

private void LoadPictureButton_Click(object sender, EventArgs e)
{
if (DialogResult.OK == OpenFileDialog.ShowDialog())
{
try
{
PictureBox.Image = new Bitmap(OpenFileDialog.FileName);
}
catch (Exception)
{
MessageBox.Show("Fehler beim Laden des Bildes", "Bilder laden...");
}
}
}

private void SaveButton_Click(object sender, EventArgs e)
{
byte index = (byte) IndexComboBox.SelectedItem;
Bitmap bitmap = new Bitmap(PictureBox.Image, 128, 128);
byte[] buffer = new byte[2 * 128 * 128];
int i = 0;
for(int y = 0; y < 128; y++)
{
for(int x = 0; x < 128; x++)
{
Color pixel = bitmap.GetPixel(x, y);

byte r = (byte)(((31 * pixel.R) / 255) & 0x1F);
byte g = (byte)(((63 * pixel.G) / 255) & 0x3F);
byte b = (byte)(((31 * pixel.B) / 255) & 0x1F);

buffer[i++] = (byte) ((r << 3) | (g >> 3));
buffer[i++] = (byte) ((g << 5) | b );

}
}
SavePicture(index, buffer);
}

private void ShowTestImageButton_Click(object sender, EventArgs e)
{
lcp100.ShowTestImage();
}

private void SwitchBackLightOnButton_Click(object sender, EventArgs e)
{
lcp100.SwitchBacklightOn();
}

private void SwitchBacklightOffButton_Click(object sender, EventArgs e)
{
lcp100.SwitchBacklightOff();
}

}
}

Coda
2009-12-14, 02:27:54
Gut gut, es ist C#
Klar, was muss man sich auch mit Details abgeben :rolleyes:

Monger
2009-12-14, 09:11:22
So wie ich das sehe, hat das schlicht was mit den Compilereinstellungen zu tun. Dass die Variable nicht in allen Fällen initialisiert wird, und du folgerichtig eine NullReferenceException kriegen könntest... das müsste er dir eigentlich auch unter C# anmeckern.

Wenn du dir sicher sein kannst dass die Variable auf allen gangbaren Wegen auch null sein darf, dann initialisiere sie einfach explizit mit null vor - dann ist die Warnung weg.


Dim lcdd As Eq3.LCP100.LCP100 = Nothing

Gast
2009-12-17, 00:13:30
1.) Das kann maximal eine Warnung sein, dass die Variable nicht verwendet wird und deshalb sinnlos ist, aber natürlich kannst du auch Variablen nur deklarieren, ohne gleich eine Instanz zu erzeugen. Das ist sogar der Normalfall. Zu VB6 Zeiten hat es das afaik noch nicht einmal gegeben, dass man gleich eine Zuweisung bei der Deklaration macht.

2.) Ich bevorzuge folgende Konvention:
Dim VarName as VarType

Das ist nur eine Deklaration. Was drin steht, weiß ich noch nicht und der erste Zugriff muss ein Schreibzugriff sein, sonst ist etwas falsch gelaufen. Ich mache keine Annahmen, was in der Variable drin steht.

Dim VarName as VarType=Nothing

Das ist eine Deklaration und ich weiße explizit Nothing zu. Im Code später gehe ich davon aus, dass dort Nothing drin steht, wenn nichts verändert wurde.