Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
T

TMattC

@TMattC
About
Posts
36
Topics
16
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Inheritage with multiple abstract classes
    T TMattC

    Ahem, just found the answer by myself. I leave the method out altogether in the Mammal class.

    C# question

  • Inheritage with multiple abstract classes
    T TMattC

    I´ve got a question regarding inheritage with multible abstract classes. Say I´ve got these classes:

    public abstract class Animal
    {
    public abstract void MyMethod;
    }

    public abstract class Mammal : Animal
    {
    public abstract void MyMethod;
    }

    public class Dog : Mammal
    {
    public override void MyMethod()
    {...}
    }

    Animal and Mammal are both abstract. Now I dont want the method MyMethod() implemented in Mammal, but I want it implemented in Dog. How can I do? I know the code above doesnt compile.

    C# question

  • Dynamicly created ErrorProvider and garbage collection
    T TMattC

    Hi! I am creating ErrorProvider objects with new in my code (and calling SetError(...)) while validating a form. It is also added to a List Later on I call Clear() and remove the reference in the List. Will the object be garbage collected after that or am I piling up a lot of never-used ErrorProvider objects?

    ErrorProvider errProvider;
    List errProviderLst = new List();

    ...

    errProvider = new ErrorProvider();
    errProviderLst.Add(errProvider);
    errProvider.SetError(c, "Ange ett heltal");

    ...

    errProviderLst[i].Clear();
    errProviderLst.RemoveAt[i];

    C# question

  • Linq extension method and referencing object?
    T TMattC

    Yes, should have thought of that. I know the struct is a value typ, though didnt think of the implications of it. Thanks for the explanation, and Ill take a peek at your article.

    C# csharp linq question announcement

  • Linq extension method and referencing object?
    T TMattC

    Well, I made the classic mistake of attaching too little code to my question, sorry about that. The thing is that my ProdTotalSales was declared as a struct. That meant that when I was debugging, the debugger showed ps.Antal=1 but salesLst[0].Antal=0 after executing the two rows of code above. I still dont understand how that could be. Now when I changed the ProdTotalSales into being a class, it works just fine. I would really appreciate an explanation to this. Could it have something to do with me calling the default-constructor? The ProdTotalSales looked like this:

    public struct ProdTotalSales
    {
        public int Year { get; set; }
        public int Month { get; set; }
        public int Antal { get; set; }
    
        public ProdTotalSales(int \_year, int \_month, int \_antal) : this()
        {
            Year = \_year;
            Month = \_month;
            Antal = \_antal;
        }
    }
    
    C# csharp linq question announcement

  • Linq extension method and referencing object?
    T TMattC

    Is there a good looking way of receiving an object reference instead of a new object with the linq extension methods? Id like the two code lines below to update the object within the salesLst.

    // List salesLst
    ProdTotalSales ps = salesLst.First(p => p.Month == o.OrderDatum.Month);
    ps.Antal += r.BestSaldo;

    C# csharp linq question announcement

  • Combobox and "autocompletion"
    T TMattC

    Well, my idea was to not use any autocomplete, but I found this article Auto Complete ComboBox[^] where he is using it, and it works really well, just a slightly different approach. So five starts on that one. :-D

    C# wpf wcf regex json help

  • Combobox and "autocompletion"
    T TMattC

    I think this question might boil down to: Can you open the list of a combobox without having one of the list entries selected, ie having something entirely different in the input field?

    C# wpf wcf regex json help

  • Combobox and "autocompletion"
    T TMattC

    I have bound a BindingList<Produkt> to a ComboBox. When the user writes some text into the combobox, it will remove (filter out) all the entries that doesnt match what the user has entered, and then open the list showing the rest for the user to select one. The idea is that the more the user writes, the fewer options will be listed. The binding and filtering part works fine, but the problem is that when the user writes the first letter, the list opens up and autocompletes (in the text field) with the first entry of the list. Is there a way to turn this auto-completion off? I want the user to have the opportunity to keep writing OR to actively select an entry from the list.

        public void Init(BindingList \_produktLst)
        {
            produktLst = \_produktLst;
            namnCbx.DataSource = produktLst;
            namnCbx.DisplayMember = "Namn";
            namnCbx.ValueMember = "Namn";
        }
    
        private void namnCbx\_TextChanged(object sender, EventArgs e)
        {
            RunFilter();
        }
    
        private void RunFilter()
        {
            BindingList filteredLst;
    
            if (namnCbx.Text.Length == 0) // Om fältet är tomt
            {
                namnCbx.DataSource = produktLst;
            }
            else // Om det finns kriterier att filtrera på
            {
                filteredLst = new BindingList(produktLst.Where(delegate(Produkt p)
                    {
                        if (!p.Namn.Contains(namnCbx.Text))
                            return false;
                        else
                            return true;
                    }
                ).ToList());
    
                namnCbx.DataSource = filteredLst;
                if(namnCbx.Focused)
                    namnCbx.DroppedDown = true;
            }
        }
        }
    
    C# wpf wcf regex json help

  • using usage (IDisposable)
    T TMattC

    Ok guys, thanks for your help.

    C# question

  • using usage (IDisposable)
    T TMattC

    Hi! I wonder if this code is a correct way of using the IDisposable using keyword. Can I put the try-catch block inside it like this, and is it fine to insert a return inside the usage block? (the code is part of a method)

    using(StreamReader reader = File.OpenText(fileName))
    {
    int lineIndex=0;
    string line;
    while((line=reader.ReadLine()) != null)
    {
    string[] fields = line.Split(';');
    try
    {
    prodLst.Add(new Produkt(Int64.Parse(fields[0]), (ProdKategoriEnum)Int32.Parse(fields[1]), fields[2], Int32.Parse(fields[3]), Int32.Parse(fields[4])));
    prodFileIndexDic.Add(Int64.Parse(fields[0]), lineIndex);
    lineIndex++;
    }
    catch(FormatException e)
    {
    return false;
    }
    }
    return true;
    }

    C# question

  • How to filter a List<> in a DataGridView?
    T TMattC

    Ive got a List<myClass> instance added to a DataGridView.DataSource. Now I want to filter this. What is the correct way to do so?

    C# question tutorial

  • Threads and event handlers
    T TMattC

    I´m writing a communication library and have a threaded method listening for incoming data:

    ///// Inside my library /////
    public partial class MCSerialPort
    {
    (SerialPort)port.DataReceived += DataReceivedHandler;

    // The event handler method
    private async void DataReceivedHandler(object sender, SerialDataReceivedEventArgs args)
    {
    await ReadData();
    }

    private async Task ReadData()
    {
    await Task.Run(() =>
    {
    ... // Reading incoming
    // Triggering an event
    PackageReceived(this, new PackageReceivedEventArgs(package));
    });
    }
    }

    ///// Inside the "client" using the library /////
    (MCSerialPort)port.PackageReceived += PackageReceivedHandler;

    private void PackageReceivedHandler(object sender, PackageReceivedEventArgs args)
    {
    Package p = args.Package;
    receivedRtb.Invoke((Action)delegate{receivedRtb.Text += p.ToString();} );
    }

    The thing is that when using this library in a "client" project the PackageReceived event handler method in the client (here the PackageReceivedHandler()) seems to be running on the same thread as the librarys ReadData() method, because I have to call Invoke() in this example in order to get the info into the receivedRtb RichTextBox. This complicates things slightly for the average library user, who might not think of having to write threadsafe code in the handler method. Is there any convenient way to make PackageReceivedHandler() run on the "main" thread?

    C# tutorial question

  • Event with no handler-method throws exception?
    T TMattC

    I just realized I can do like this:

    if(BrokenPackageReceived != null)
    BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

    Is this the common way to do it?

    C# question

  • Event with no handler-method throws exception?
    T TMattC

    Hi! I have defined an event in a certain class:

    public class BrokenPackageReceivedEventArgs : EventArgs
    {
    private readonly byte[] package;
    public byte[] Package { get { return package; } }
    public BrokenPackageReceivedEventArgs(byte[] _package) { package = _package; }
    }

    public event EventHandler BrokenPackageReceived;

    // Trigging the event:
    BrokenPackageReceived(this, new BrokenPackageReceivedEventArgs(dataLst.ToArray()));

    It seems that if no method is added to the event, it throws a NullReferenceException. I dont want that exception to occur, in fact I am baffled it actually does occur. Is this really the way events always do if no method is attached? Should I just use a try-catch with an empty catch to get rid of it, or am I missing something?

    C# question

  • How to upgrade a SQL Server Mobile database? [Solved]
    T TMattC

    A small note after some further research: There is an add-in to Visual Studio called "SQL Server Compact/SQLite Toolbox" which give you som really nice tools for working with those databases from within Visual Studio. One of those features is to update the SQL Server CE database from v.3.5 to v.4.0.

    Database database visual-studio csharp c++ sql-server

  • How to upgrade a SQL Server Mobile database? [Solved]
    T TMattC

    In the end I solved it myself. It turned out the SQL Server Mobile file (.sdf) was created by version 3.5, while I tried to access it from Visual Studio 2013 with version 4.0. I solved it entering References under project properties. Then removing the reference to System.Data.SqlServerCe.dll (v4.0). After that I added a new reference to the System.Data.SqlServerCe.dll v3.5. I had to Browse my hard drive in order to find it. Problem solved.

    Database database visual-studio csharp c++ sql-server

  • How to upgrade a SQL Server Mobile database? [Solved]
    T TMattC

    Hi! Im kinda out of my dept here. Im upgrading an old C++/CLI project to Visual Studio 2013, so I have a SQL Server Mobile database file (.sdf) that needs to be upgraded to current version (whatever that is). Im not sure what version it has now, but VS says I need to upgrade it with SqlCeEngine.Upgrade(). I´ve got SQL Server Pro 2014 installed on my computer, and when I tried to install Sql Server Compact 4.0 it said I have a later version installed already. Im totally lost here. Could someone please push me in the right direction.

    Database database visual-studio csharp c++ sql-server

  • Encoding.ASCII.GetString(byte[]) and CR LF
    T TMattC

    Thankyou! Thought I was getting insane.

    C# question

  • Encoding.ASCII.GetString(byte[]) and CR LF
    T TMattC

    Could someone please explain to me what´s going on in this conversion. I dont understand the result.

    byte[] arr = new byte[6] { 68, 65, 84, 65, 13, 10 };
    string str = Encoding.ASCII.GetString(arr);

    According to the ASCII table the bytes mean DATA\n\t. (13=CR='\n', 10=LF='\r') But the string receives DATA\t\n :confused:!?! If I change their places { 68, 65, 84, 65, 10, 13 } the string receives DATA\n\t. Why does the CR and LF change place? Im really confused here.

    C# question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups