|
Big Daddy Farang wrote: class Device
{
private SerialPort Port;
private const int BufferSize = 256;
private char[] ReadBuffer;
private string LastResponse;
public Device(string CommPort)
When, and what things, do I want "private" and "public" ?
For that matter, just exactly what do those words mean ?
Am I headed in the right direction via reading stuff like this page ?...
http://msdn.microsoft.com/en-us/library/ms173121(v=VS.80).aspx
"...Access modifiers are keywords added to the class, struct, or member declaration to specify these restrictions. Those keywords are public, private, protected, and internal. For example:..."
|
|
|
|
|
If you mark something as private, it cannot be seen outside the class. If you mark something as public, you are telling the compiler that this item can be used from outside the class.
|
|
|
|
|
Pete O'Hanlon wrote: If you mark something as public, you are telling the compiler that this item can be used from outside the class.
That is where I am turbo-confused.
I just did exactly that; Marked some stuff (byte arrays, nothing but some bytes that don't mean anything to anyone except my little Barn Door Box) "public" in a class in another file, and the method in the first file does not know they exist. He gives me an error "...The name So_And_So does not exist in the current context..."
So now I need to learn/guess the definition of the word "context" in the mind of the developers of C#, and then learn how to make the name "exist" (whatever that has transmuted to mean in C#).
|
|
|
|
|
Let me start with the meanings. As Pete said, private variables or functions can only be used from within the class. Public variables or functions can be used from outside the class. For example, from another class such as your main form you could do the following.
Device device = new Device("COM1");
device.Connect();
because the constructor and Connect() are public. If you're interested in the device's response to connect command you'd want to see LastResponse. If you try
string connectResponse = device.LastRepsonse;
that's not allowed because LastRepsonse is private. You could however write
string connectResponse = device.GetLastResponse();
because that function is public.
What you want private and pubic is up to you, but basically data that should not be changed by outsiders should be private. Things that should be available to users of your class need to be public.
When deciding which ask, "Who wants to know?" Things are private by default, but if users should have access to them make them public. If we made Port public, users could close the serial port and the Device object wouldn't know about it, so that should be kept private.
As far as MSDN goes, yes that's a good reference. But it is designed to be a reference more than an instructional work. You might consider visiting your local bookstore (or online of course) and looking at books that teach C# programming. That depends on your learning style, some people can do fine with reference materials. Given your background you might do well with just reference materials.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
I tried to declare this public in one method...
public string ThePortName = "";
...but then no other method could see it.
When I moved it way up to the top along with the first ints just after the class was named, like this...
public partial class Form1 : Form
{
:
:
:
public string ThePortName;
:
public Form1()
{
...then everybody could see it.
There is, as I understand it, only one class in this practice app; so there are probably things that I need to increase in my understanding about this.
Again, NewBee NoClue here, flying high.
|
|
|
|
|
Just curious, If I declare a class as public, and within that class, I declare a...
-- Serial Port
-- Integer
-- Method
-- Whatever
...as also being public, then will other methods in other classes be able to use the Serial Port / Integer / Method / whatever ?
|
|
|
|
|
Yes, those will be visible to other classes that instantiate the object you are using.
I too was a long time embedded programmer who jumped into PC development. I also am using serial ports HEAVILY in .NET.
A suggestion for you:
Abstractions are a good thing. For example, I have a wireless device that uses 900MHz to communicate. This device has a companion (Aerocomm antenna) that attaches to a PC through a serial port. So, instead of accessing serial ports through out my software, I encapsulated the functionality into one class (object). So, the Aerocomm antenna class has all the stuff for the serial port contained within it. It exposes functions to other classes in order to communicate with the Aerocomm antenna - like configuring it, setting what device to communicate with, set it into broadcast mode, etc.
In your case, you are communicating with a device. I would create a class that represents the device. Then, create functions within the class that will get abstracted data from the device. So, for example, say you want to get the version number of the device. You would have a function in your class like:
string GetVersionNumber()
{
// Code to get the version number from the device, including the serial port commands, etc.
}
This function could also possibly be a property as well, but it would still have functional code in it.
Basic idea is to keep the accesses and functionality where it needs to be. This is a fundamental idea behind object-oriented programming.
Oh and one other thing - USB-Serial devices can cause exceptions when pulled out of a computer while the serial port is open. You will need to be aware of that and handle that as well.
Good luck!
|
|
|
|
|
David Knechtges wrote: "...instantiate the object you are using...."
Terribly sorry about my frozen brain cells, but just exactly what does "instantiate" mean ?
Also, the word "object" comes up again.
What I'm going to be doing is communicating with this external embedded system; normally with BlueTooth, and
-- drawing a lot of little wiggly lines on the user's screen
-- putting some blips on the screen (think of it as virtual LEDs)
-- watching for button clicks by the user
-- De-Confusing itself when noise appears on the serial port
|
|
|
|
|
What you the programmer write is a Class. Form1, Device in my earlier example, etc. are classes. When you "instantiate" an object, it means you are creating an instance of a class.
int count;
Device device;
device = = new Device("COM1");
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
Okay, so it's...
[1] Declare
[2] Instantiate
[3] Use it
Am I thinking properly ?
I'm still having trouble getting the linker to let my methods in one file see names that are declared and (I hope I'm getting the nomenclature correct) instantiated in another file.
e.g.,
My method for sending a group of bytes cannot see the name of those bytes unles they are in the same source file; which absolutely destroys any hope of having a manageable arrangement of source files.
|
|
|
|
|
David Knechtges wrote: Oh and one other thing - USB-Serial devices can cause exceptions when pulled out of a computer while the serial port is open. You will need to be aware of that and handle that as well.
Good thinking.
My mind immediately reverts to what has worked excellently for years; "Include" files.
Do they work in C# like they do with (many of, most of, almost all) embedded systems assembly language development systems ?
e.g., for ease of reading, and for code organization, the sub-sections are Included; I'm thinking like Macros and constant definitions, but I suppose that code can also be included.
I just searched for "C#, #Include" here on codeproject.com and barely understood the hits I got. Suggestions welcome.
Do I just give every source file the same Namespace, and make the all the classes within the namespace public ?
By the way, is the process the same here as elsewhere ? e.g., write then compile then link then execute ?
|
|
|
|
|
C# does not #include header files the way C and C++ do. Everything that's part of a class is (usually) in one file. You've already seen "partial" class that Visual Studio uses to separate forms into the file you edit, Form1.cs for example and the file it edits, Form1.designer.cs in this case.
For simpler programs, and to get started, I'd stick with using the same namespace throughout. Same with making classes public. There are reasons to use different namespaces and possibly classes that are not public, but let's not worry about that yet.
The build process is basically the same. Your various .cs file get compiled into .obj files and then all of those objects get linked into an executable (.exe) file. And if all goes well you execute it. Using Visual Studio the compliling and linking usually happens as a result of pressing F6 (see the Build menu.)
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
Thank you; you are helping me tremendously.
The Build/Build keystroke has become my friend; the best critic my code has.
Sortta'kinda like "Find the most egregious errors, please".
|
|
|
|
|
David Knechtges wrote: Then, create functions within the class that will get abstracted data from the device. Are the words "function" and "method" the same thing ?
|
|
|
|
|
In a C# 2008 desktop application, I need change the application since all the users are moving from one domain to new domain. The current application does not let the user access information under the new domain name.
To accomplish this goal, I would like to see what roles have been setup for those users starting with myself.
To accomplish this goal, I would like to know how to access those roles.
I do know that when I look at: Thread.CurrentPrincipal.Identity.Name, I can access the person's name. I also know that if I use System.Security.Principal.WindowsIdentity.GetCurrent(), I get the same information.
However can you tell me in code how to access the subcategories in the following:
1. Thread.CurrentPrincipal.Identity
2. System.Security.Principal.WindowsIdentity
Can you tell me in tell or show me in code how to accomplish this goal?
|
|
|
|
|
If you are using Active Directory, you can use the System.DirectoryServices.AccountManagement API to find details about a user and the groups he's associated with. You might want to try that (requires you to import System.DirectoryServices into your project).
|
|
|
|
|
Hi,
1. I wonder how to implement animations in WPF windows desktop app?
e.g. App startup form transitions in windows 8 metro start menu
2. & how to get the horizonatal scrollbar like metro start menu?
Regards,
NJ
============================================
The grass is always greener on the other side of the fence
|
|
|
|
|
Here[^]
Please reading the forums guide lines before posting.
|
|
|
|
|
|
"Attempted to read or write protected memory"
Above exception occurs when i add following lines to my code.
PosDamAx.IoXml ioxml;
ioxml = new PosDamAx.IoXml();
i have added reference to PosDamAx.dll
i have chosen the debugger as x86.
secondly i have uncheck suppress jit optimization box.
Also i want to share that the same code start running when i try it on another PC.
Both have installed VS 2010 and have same settings of debugger But the only difference is of architecture , code is running fine on 32bit while mine is 64bit.
|
|
|
|
|
You need to use your debugger to trap the actual failing piece of code. What you have shown above is unlikely to produce this error, unless the constructor for the PosDamAx class has a bug.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
That is likely because the PosDamAx object (which I am guessing is an ActiveX control) is 32 bit only. All of your projects in your solution must be set to compile as x86 and not as AnyCPU.
|
|
|
|
|
I have windows form , which contains treeview.
When i drag node and drop on windows explorer, i want event which will give me windows exploer path. Where I actually end node drag.
Does any body have solution for same.
|
|
|
|
|
|
I have Treeview on form, Tree view contain differnt nodes.
|
|
|
|