|
Roger Wright wrote: While C# recognizes a string type, it is internally converted to a
.Net String class. I'm not certain whether this happens with
int types, converting to Int classes,
Rather than using the term converted, I would use "is an alias for" here because conversion implies casting.
|
|
|
|
|
Good point, Pete; I stand corrected.
Will Rogers never met me.
|
|
|
|
|
Thanks Rogar,
Secondly i want to create our Own Datatype then What to do for it?
struct music
{
music mc;
}
If you can think then I Can.
|
|
|
|
|
It depends entirely on what you want your data type to look like. In C# you are essentially limited to classes and structs, but that gives you a lot of latitude. In some older languages you could define basic types, like a 12-bit integer, but there's really very little need to do silly things like that unless you're programming PLCs.
For example, in my work, I use electrical parts, and calculate values of signals passing through them. An important feature of electrical values is that they're not real numbers, but rather, complex types composed of a real and an imaginary part. No simple type exists for manipulating these values, so a custom data type is necessary. I prefer to use classes for these, out of habit and because I haven't used a struct since I quit using Pascal. So if I want to define a variable to hold an impedance value, I would use something like this:
namespace ClassLibElectrical
{
public class Z
{
private double re;
private double im;
public Z(double re, double im);
public double Re
{
get { return re; }
set { re = value; }
}
public double Im
{
get { return im; }
set { im = value; }
}
public double MagZ(double R, double I)
{
return Math.Sqrt(Math.Pow(R,2) + Math.Pow(I,2));
}
public double PhiZ(double R, double I)
{
return Math.Atan(I / R);
}
}
}
Now I have a new data type that I can use throughout my application whenever I need an impedance value. Note that Z implements two Properties, Re and Im. These are the real and imaginary parts of the complex value, Z. When I want to create a new impedance, I can use the constructor Z to explicitely pass the initial value to the new variable, or I can use the generic form to create an uninitialized variable of type Z, then use the properties to set the component values. In either case, I have to use the new operator to instantiate a new variable of the type.
Since I often need only the magnitude or phase angle of an impedance for my calculations, I also tossed in a couple of methods associated with the new type - MagZ and PhiZ. Since I did this just to answer the question, from memory, late at night, and somewhat drunk, I provided a bad example; it would be far smarter to pass the methods a value of type Z than to require the individual components as input parameters, and let the method sort it all out. The math is obvious and simple.
I can also make my class do other interesting things, like override standard arithmetic operators which won't work properly on complex numbers. I can also provide methods to convert the format from rectilinear coordinates to polar, and change the form of the impedance to trigonometric or exponential notation, as these forms make certain calculations more convenient. There's really no limit to what you can do with your classes.
Since you are clearly even more of a beginner than I am, keep in mind that a class is a fundamental working unit in the C# language, and that it's nothing more than a custom data type that you can use over and over again in your application. The great advantage of classes is that they allow you to create methods which apply only to variables of your class type. Inheritance is often touted as an important attribute of OOP languages, but I've rarely needed it. The best thing about OOP classes is the amazing flexibility you have to create your own world in the realm of data, and to manipulate your information in any way you like.
You tossed out an example of a type called music ; that isn't very informative, and we can't help you much until you tell us more. What do you want your music type to hold as data, and what do you want to be able to do with it? I can think of several components for a music type - pitch, attack_time, sustain_time, decay-time, tremolo_depth, tremolo_rate, fade_time - and that doesn't even touch on filtering characteristics.
Tell us what you're trying to accomplish, and how you plan to attack the problem, and we might be able to help you a bit. You'll still be expected to do all the hard work, since we're all volunteers and the pay here is lousy, but we do try to be helpful. The usual rule here is, show us what you've tried, describe what you're trying to accomplish, and we'll help you figure out what you did wrong. We won't do it for you, or waste too much time trying to figure out what you're trying to do from cryptic clues.
Will Rogers never met me.
|
|
|
|
|
Roger Wright wrote: While C# recognizes a string type, it is internally converted to a .Net String class. I'm not certain whether this happens with int types, converting to Int classes, but it wouldn't surprise me.
Actually, when and how this is done is understood when reading about boxing and unboxing of types (http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^]).
According to MSDN, a struct is a specific value type; specifically, an integer is a struct. A string is a reference type (class).
At any rate, all the C# names are just aliases for .NET types. Value types are stored in a 'raw' format until they are needed 'as objects' (such as when you call a method of Int32 on an integer variable): they are boxed into a proper class and then acted on.
|
|
|
|
|
I suspect you are asking why a string is a class rather than a structure, and there are many reasons for this. This[^] question gives a fairly detailed set of answers as to why a string is a class (and has to sit on the heap).
|
|
|
|
|
try this[^]
BTW, this is about one of the most basic things in programming. What's wrong with buying a good book and read?
V.
|
|
|
|
|
IMO that is the right answer.
|
|
|
|
|
|
Wikipedia is accurate. [citation needed]
|
|
|
|
|
Surprisingly, it is often very authoritative and accurate. Quite remarkable, actually, unless Dalak Dave is among the sources referenced.
Will Rogers never met me.
|
|
|
|
|
<pre>How you tell me how to change the C#.net desktop 2008 logic so it will compile clean and the logic works? The problem is I am trying to create an instance of an object in a static class
In this application I want to create a 'class with methods for accessing the active directory' from all parts of the solution file. As part of this logic change when a user clicks on the desktop shortcut and they are not assigned to any active directory groups, I want to throw an error message stating 'do not have access-contact network administrator and throw them out of the application.
The logic to check the user credentials comes from the main method that is a 'static' method in program.cs.
The code I want to add to the main methos is as follows:
ActiveDirectoryUser CurUser;
CurUser = new ActiveDirectoryUser();
However I get the following errors:
-
Error 55 'Desktop.CurUser': cannot declare instance members in a static class
Error 56 The type or namespace name 'ActiveDirectoryUser' could not be found (are you missing a using directive or an assembly reference?)
Error 57 The type or namespace name 'ActiveDirectoryUser' could not be found (are you missing a using directive or an assembly reference?)
The cLass i want to reference looks like the following:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Web;
using System.Windows.Forms;
using System.Security.Principal;
namespace EnrollTrac.Common
{
public class ActiveDirectoryUser
{
public ActiveDirectoryUser()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if ((!Thread.CurrentPrincipal.IsInRole("testit")))
{
MessageBox.Show("You do not have authortity.Please contact your network administrator"," Error", essageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
Application.Run(new Desk());
break;
}
</pre>
|
|
|
|
|
dcof wrote: Error 56 The type or namespace name 'ActiveDirectoryUser' could not be found (are you missing a using directive or an assembly reference?)
"Add a new assembly reference to System.DirectoryServices.dll. This provides access to the System.DirectoryServices namespace that contains managed types to help with Active Directory querying and manipulation."
dcof wrote: Error 55 'Desktop.CurUser': cannot declare instance members in a static class
Mark it with the "static" keyword, if it's appropriate a
Bastard Programmer from Hell
|
|
|
|
|
Hi,
I have a textbox control with 2 buttons for different actions to perform.
Now, there is a check on this textbox, which I am initiating by using OnTextChanged method.
While this is initiated, I want the buttons to be greyed, otherwise this check is not complete till the user clicks on the screen again or clicks to the next textbox.
That way they cannot change some value and access the submit buttons without this method being completed.
Thanks.
|
|
|
|
|
Your description is extremely hard to understand.
Why don't you just disable the buttons if the text field is empty, and enable them in the textChanged event handler if the proper conditions are met.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
I apologize for this.
But, I want an action to take place only if the text value has changed.
The problem is the onTextChanged method is not completed till the user clicks on the computer screen or tabs to the next textbox. If you are using a mouse, then you are not clicking anywhere on the screen and have the option to click on the button which should not happen.
|
|
|
|
|
vanikanc wrote: the onTextChanged method is not completed till the user clicks on the computer screen or tabs
That doesn't sound right. The TextChanged event fires on every change, right away, no special user action required. This code would prove that:
System.Windows.Forms.TextBox tb;
System.Windows.Forms.Label label;
public void Test() {
Form f=new Form();
tb=new TextBox();
tb.Bounds=new Rectangle(10,10,100,20);
f.Controls.Add(tb);
label=new System.Windows.Forms.Label();
label.Bounds=new Rectangle(10, 50, 100, 20);
f.Controls.Add(label);
tb.TextChanged+=new EventHandler(tb_TextChanged);
f.ShowDialog();
}
void tb_TextChanged(object sender, EventArgs e) {
label.Text=tb.Text;
}
|
|
|
|
|
The 'trouble' with the WinForms 'TextBox' 'TextChanged' Event is that it doesn't give you much information: its 'event argument' is just a dumb old 'EventArgs.
If you use the 'Leave event of the TextBox to judge the user is done, imho that's an 'ambiguous' choice that can lead to confusion, since all it means is the user shifted focus to some other control or object.
A TextBox with its 'MultiLine' property set to 'false, will ignore Enter/Return key-presses, and the focus will stay in the TextControl. The properties 'AcceptsTab, and 'AcceptsReturn only apply to MultiLine = 'true TextBoxes.
A Tab entry is going to move the focus to the next control in the tab-order; again, imho, an unsatisfactory way to handle the need to know the user has finished doing whatever they want to do with a TextBox.
The KeyPress, KeyDown, and KeyUp events give you access easily to which key is pressed.
So, what to do ? Since it is something of a convention that hitting the 'Enter key means you are 'done' with something, why not use that:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
}
} Another idea I would consider, if dealing with users who were not 'socialized' to 'enter key = done' ... would be to make a UserControl that would be a combination of a CheckBox and a TextBox.
good luck, Bill
"Is it a fact - or have I dreamt it - that, by means of electricity, the world of matter has become a great nerve, vibrating thousands of miles in a breathless point of time? Rather, the round globe is a vast head, a brain, instinct with intelligence!" - Nathanial Hawthorne, House of the Seven Gables
|
|
|
|
|
The Observer Patterns provides more flexibility to handle events when compared to the Proxy Patterns?
Thanks for the help and attention...
|
|
|
|
|
Of course it does as the Proxy Pattern has absolutely sod all to do with events. Did you even read the answer below?
|
|
|
|
|
What's the difference between Proxy Patterns and Observer Patterns? and the observer provides more flexibility to handle events when compared to the Proxy?
Thanks for the help and attention...
|
|
|
|
|
A Proxy pattern is essentially a class representing an interface to something else.
Observer pattern is where an object notifies all its listeners whenever it changes.
So both are very different patterns used for different tasks, and cannot really be compared.
Search more on the internet, you will get samples and lots of code for these patterns.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
Can you tell me what is wrong with this code?
In an exisitng C#.net 2008 desktop application, I want to setup a class .cs file so I can do security checks against the active directory. I want to add this code to a project file that contains lots of common routines all the other processing modules use. The code is the following:
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Security.Principal;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace En.Common
{
class ActiveDirectory
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
//Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
//if ((!Thread.CurrentPrincipal.IsInRole("testit")))
}
}
I want to start with 3 lines of code listed so far. However, the statement,
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
However I am getting the following two errors on this statement:
Error 94 Invalid token '(' in class, struct, or interface member declaration
Error 95 Invalid token ')' in class, struct, or interface member declaration
|
|
|
|
|
You need to put your statements into a method or constructor. At the moment it is like you are declaring then as class level variables.
Next time please put your code into Code Blocks. It makes it much easier to read!
Live for today. Plan for tomorrow. Party tonight!
|
|
|
|
|
I've created a custom control with a non-rectangluar border.
I'm trying to set the region properly and struggling to get a *filled* region.
I can make it the outline of my control, but then you can't drag the control from the middle.
How do I *fill* a region?
|
|
|
|
|