|
When in doubt, use a class instead of a struct.
Good structs are small and often short-lived (such as Point and Size).
Avoid big structs; my rule of thumb: any struct larger than 16 bytes is suspicious.
structs can do a few things classes can't; e.g. they are fine when representing value types with overloaded operators (e.g. a struct representing a complex number).
|
|
|
|
|
The difference between structs and classes is fairly simple (Doesn't make sense to differentiate between structs and "objects," since EVERYTHING is an object, including a struct).
Structure-type variables contain the actual structure. Class-type variables contain a POINTER to the actual class. That means multiple variables can point to the same class, but only one variable can point to a given structure. If you type a = b where 'b' is a structure, 'a' will gain a COPY of that structure. If 'b' is a class, 'a' will just point to the same class, so if you make changes to 'a', you'll see the same result in 'b'.
Structures are more efficient for small, immutable objects (Objects that cannot be changed once created). Classes are good for everything else. Of course there are exceptions to this, but that's a good starting point.
stephen.darling wrote: If it is better to use objects, is it valid to create such an object that has no public or private methods other than its constructor, and instead has a bunch of public variable declerations?
That's bad practice... In most cases, you don't want to expose variable declarations. Make properties or methods to return them, so you can control the flow of data.
|
|
|
|
|
Thanks for the responce everone!
Steve
|
|
|
|
|
Hello,
I have a simple multi-tier winform application (c# and SQL database) where I want to add some reporting functions. Business Logic, DataAcces layer and UI are the most important Layers in my project.
I am searching for a flexible way to add the reporting so I thought to do it by adding an extra class libary project to my sollution where i handle the reporting. I hope this makes it possible to deploy the dll to my clients when a report changement take place.
Is there someone who:
- have a sample solution
- show me how to access reports form the libary class
- know a better/easyer way to add a flexible reporting function to a winform application.
Thanks for advise.
|
|
|
|
|
The Best way is to Create a Webservice that you win app can consume and when you change the web service in one place you dont need to change it in other apps.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.com
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
With List those lines are valid:
List<string> strs = new List<string>()
{
"helo",
"yes"
};
How to perform the same with Dictionary ?
Dictionary<MyType, string> dict = new Dictionary<MyType, string>()
{
Yes, "yes",
No, "no"
};
Чесноков
|
|
|
|
|
|
that is great thanks
Чесноков
|
|
|
|
|
hey guys..i need some help...i want to connect a MySql database in the other pc...i will develop a program which will be used over the network in the office..i searched something over web and i wrote my codes as i sunderstand from the sites that i searched...but i have such problem when i run the program it gives error like "Unable to connect any of the specified MySql hosts"...i opened 3306 port from the modem..but still i have that problem..here is my codes
try
{
MySqlConnection connect = new MySqlConnection("Data Source=SErver_ip;database=dbname;user id=xxx;password=1");
connect.Open();
Console.WriteLine("Bağlantı Durumu : {0}", connect.State);
connect.Close();
}
catch(exception ex)
{
Console.WriteLine(ex.Message);
}
Where is my mistake??
or any other suggesstion ?
|
|
|
|
|
Too me your connection string doesn't look right compare it too this site and let us know if it works
Connection Strings[^]
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
yeah i compared my connection string with the site one and then i used the same string the one which was suggested by site but still same error...should i make any settings in the server machine?
or do i need mysql in my pc also or just connector is enough ?
|
|
|
|
|
if your using the database name etc as variables then make sure you have concatinated them properly into the string
i.e. "Server=" + ServerName + ";Database="
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
no man..i am not using them as variables...so do u have any other suggestion ?
|
|
|
|
|
erdinc27 wrote: MySqlConnection connect = new MySqlConnection("Data Source=SErver_ip;database=dbname;user id=xxx;password=1");
shoudn't be:
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
beside what qwertz321 told you, you need to check the firewall on a computer that has MySQL installed. For testing purposes you can disable firewall and you will know if firewall is the culprit.
|
|
|
|
|
thanks guys for help..when i installed MySql as a dedicated server my problem is solved...and i unchecked Ip binding...so it works now..
|
|
|
|
|
Is it possible that Dll assembly may have its won icon?
I added it as for ordinary project but it does not show it in windows explorer
Чесноков
|
|
|
|
|
I don't think so. The reason being that icons are usually so users can associate an image with an application. Since DLL's are not applications (they are used by applications), double clicking a DLL isn't going to launch that DLL as an application, so the user has no reason to associate an image with it. Open your System32 folder and you'll notice that not a single DLL has a special icon. You should be able to, however, embed images so that they can be used by other applications (like SHELL32.dll).
|
|
|
|
|
I ran into problem for my school assignment and it is to display whatever in a meal.csv file, here is a code we are starting with. The array string of itemDetails is where the data is store then passed into array ReadLines, then in Main, it is passed to mealContent, but when I tried Console.WriteLine(mealContent); it only prompted System.String[]. So how do I at least display it out in the command prompt. Thanks for the help guys~ Source code is also attached
Code/meal.csv
lunch,bento box b - sashimi,box combo,$9.59
dinner,vegetable sushi,6 rolls,$3.50
dinner,tuna roll,3 rolls,$4.50
dinner,roe, 2 rolls,$3.95
lunch,bento box a - chicken teriyaki,box combo,$8.59
lunch,bento box b - sashimi,box combo,$9.59 dinner,vegetable sushi,6 rolls,$3.50 dinner,tuna roll,3 rolls,$4.50 dinner,roe, 2 rolls,$3.95 lunch,bento box a - chicken teriyaki,box combo,$8.59
FileIOManager/FileLocation.cs
using System;
namespace FileIOManager
{
static class FileLocation
{
public const string INPUT_FILE = "../../Data/meals.csv";
}
}
using System; namespace FileIOManager { static class FileLocation { public const string INPUT_FILE = "../../Data/meals.csv"; } }
FileIOManager/FileReader.cs
using System;
using System.IO;
namespace FileIOManager
{
static public class FileReader
{
private static int GetLineCount()
{
StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);
int counter = 0;
while (!sr.EndOfStream)
{
counter++;
sr.ReadLine();
}
sr.Close();
return counter;
}
public static string[] ReadLines()
{
int totalItems = GetLineCount();
string[] itemDetails = new string[totalItems];
StreamReader sr = new StreamReader(FileLocation.INPUT_FILE);
string itemDetail;
int counter = 0;
while (!sr.EndOfStream){
itemDetail = sr.ReadLine();
if (itemDetail.Trim() != "")
itemDetails[counter++] = itemDetail;
}
sr.Close();
return itemDetails;
}
}
}
|
|
|
|
|
1) Use PRE tags with the lang attribute set to "C#" to format your code better so we have an easier time reading it and helping you.
2) There is no need for those two functions. Just use System.IO.File.ReadAllLines() , which returns an array of strings.
3) To output each item in the array of strings, use a for loop and either Console.WriteLine() or Console.Write() .
4) Instead of using the first function to scan the file to determine the number of lines, just use a List instead of an array. Lists dynamically resize based on how many elements you put in them. Of course, this is assuming that you don't want to use System.IO.File.ReadAllLines() for some reason.
|
|
|
|
|
I figure it out, just iterate over the array and write out each one individually. But im actually stuck on the next step lol i hope you can help out too.
static void Main(string[] args)
{
string[] meals = System.IO.File.ReadAllLines("meals.csv");
foreach (string meal in meals)
Console.WriteLine(meal);
Console.ReadKey();
}
the price actually needed to times by 1.8 so Price = Cost * 1.8
also i'll be dealing with bubble sort.. which I have never learn to knew how to coz we have ot make it look like this in the end
* Lunch Items *
$15.46 bento box a - chicken teriyaki, box combo
$17.26 bento box b – sashimi, box combo
* Dinner Items *
$7.11 roe, 2 rolls
$8.10 tuna roll, 3 rolls
$6.30 vegetable sushi, 6 rolls
|
|
|
|
|
d87c wrote: i hope you can help
I might have, but I will not because you didn't take my advice to use PRE tags. Ignoring my advice clearly shows you aren't considerate of others and would rather have them wade through your poorly formatted code than spend a little time learning how to properly format it. Why should I spend time trying to teach somebody who doesn't want to learn? Also, a bit of advice, use the real words you mean rather than the txt speak versions (e.g., "coz", "ot"). We here appreciate people who realize they have a full keyboard to type with and use that ability. Also, avoiding txt speak will help in the workplace, as using txt speak makes you appear childish and less professional.
|
|
|
|
|
I am new to C# and need to create a new class for NUnit that will enter and test items into an array. The first class is Item
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReneApp.Domain
{
public class Item
{
public string Status;
public string Title;
public string Series;
public string Author;
public string Type;
public string Description;
public bool Addition;
public string status
{
get { return Status; }
set { Status = value; }
}
public string title
{
get { return Title; }
set { Title = value; }
}
public string series
{
get { return Series; }
set { Series = value; }
}
public string author
{
get { return Author; }
set { Author = value; }
}
public string type
{
get { return Type; }
set { Type = value; }
}
public string description
{
get { return Description; }
set { Description = value; }
}
public bool addition
{
get { return Addition; }
set { Addition = value; }
}
}
public delegate void ProcessItemDelegate(Item item);
}
The second class is Transaction
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReneApp.Domain
{
public class Transaction
{
private IList<Item> list = new List<Item>();
public void AddItem(Item item)
{
list.Add(item);
}
public bool ValidateTransaction()
{
if (list == null)
return false;
return true;
}
public void ProcessItem(ProcessItemDelegate processItem)
{
foreach (Item b in list)
{
if (b.addition)
processItem(b);
}
}
}
}
The class that I am testing with is
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReneApp.Domain
{
using NUnit.Framework;
[TestFixture]
public class TransactionTest
{
[Test]
public void ValidateTransaction()
{
Transaction transaction1 = new Transaction("Stock", "Dark Rival", "Masters of Time", "Brenda Joyce", "Book", "Time Travel", true);
Assert.IsTrue(transaction1.ValidateTransaction());
Transaction transaction2 = new Transaction(" ", " ", " ", " ", " ", " ", " ");
Assert.IsTrue(transaction2.ValidateTransaction());
}
}
}
I am unsure how to get the items in the array into the test. Any help would be appreciated. With the current, I am getting an error on the two ValidateTransaction items saying "ReneApp.Domain.Transaction does not contain a constructor that takes 7 arguments"
|
|
|
|
|
mtn*rain wrote: Transaction does not contain a constructor that takes 7 arguments
I suggest you take compiler messages quite literally.
Your Item and Transaction classes don't have explicit constructors (well, you haven't shown any); so all they have is an implicit or default constructor that takes NO arguments.
Furthermore, your 7 parameters suggest they describe a new Item, not a new Transaction. Rethink your structure, add constructors, and check your code carefully.
If you're new to C# (and to object orientation?) you might want and consider something simpler to start with.
|
|
|
|
|
I am entering a new item to test with for NUnit. I am new to this and this is a project required for school, so I cannot do something simpler.
Do you know if I have already listed a constructor in another class that I might use or do I need to completely redo this entire thing?
|
|
|
|
|
How many constructors did you show so far? Have you any idea what a constructor is? Did you pay any attention in class, assuming you were present?
|
|
|
|
|