VB6 Val() equivalent in C#

So today I came across a bug in our new system, which is written in C#.NET 4.0.

It was an upgrade from an old VB6 app, and it happened to be the way that Val() was used in VB6.

Since there is no direct equivalant, the internet didn’t provide me fast enough with a good enough method and it’s behavior is kinda odd, here’s my best approiximation in C#:

public static Double Val(string value)
{
    String result = String.Empty;
    foreach (char c in value)
    {
        if (Char.IsNumber(c) || (c.Equals('.') && result.Count(x => x.Equals('.')) == 0))
            result += c;
        else if (!c.Equals(' '))
            return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
    }
    return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
}

Works great!
(my initial Regex method was much faster, but lacked the accuracy I wanted)

PrestaShop wholesale mode fix (force catalog mode when uses is logged in)

After searching all of Google’s entries for how to make PrestaShop hide it’s prices when the user is not logged on, which is the common behaviour for dealer/wholesale stores (where by non-registered users do not see prices) I was rather disappointed by the variety of the non-elegant solutions out there.

Also, there are some paid solutions, but what’s the point of paying 50 bucks for open source applications?

Here’s my solution, it works like a charm and is a very easy fix!

In classes\Configuration.php (around line 114) it looks like this

static public function get($key, $id_lang = NULL)
{
	if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
		return self::$_CONF_LANG[(int)$id_lang][$key];
	elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
		return self::$_CONF[$key];
	return false;
}

change it to this:

static public function get($key, $id_lang = NULL)
{
	//Grab access to the $cookie which is already loaded in the FrontController as global $cookie;
	global $cookie;
	if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
		return self::$_CONF_LANG[(int)$id_lang][$key];
	elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
		//If the system is trying to find out if Catalog Mode is ON, then return the configuration setting,
		//but override it with the user logon status
		if($key == 'PS_CATALOG_MODE')
		{
			return !$cookie->logged || self::$_CONF[$key];
		}
		else
		{
			return self::$_CONF[$key];
		}
	return false;
}

Essentially, I wanted to force the system to display the “Catalog Mode” when the user is not logged in, and to turn this off when he is logged in. Instead of going through every call that checks if it’s on or off, we override the call to also check if the user is logged in.

I can guarantee this works for v1.4.3.0 and the code for the current version 1.4.8.2 (at the time of this post) has not changed, so it should work there.

If you have any difficulties with this, let me know with a comment below!

Filter GridView as you type (ASP.NET C# with jQuery)

I’ve often found that our client’s web portals have way too much data on screen, so here’s a quick and easy “Filter as you type” JavaScript/jQuery block:

function SetupFilter(textboxID, gridID, columnName) {
    $('#' + textboxID).keyup(function () {
        var index;
        var text = $("#" + textboxID).val();
 
        $('#' + gridID + ' tbody tr').each(function () {
            $(this).children('th').each(function () {
                if ($(this).html() == columnName)
                    index = $(this).index();
            });
 
            $(this).children('td').each(function () {
                if ($(this).index() == index) {
                    var tdText = $(this).children(0).html() == null ? $(this).html() : $(this).children(0).html();
 
                    if (tdText.indexOf(text, 0) > -1) {
                        $(this).closest('tr').show();
                    } else {
                        $(this).closest('tr').hide();
                    }
                };
            });
        });
    });
};

Then all you need to do, after you include the above code segment in your page head or startup .js file is to call the below for each textbox you want to actively filter your grid:

$(function () { SetupFilter("myTextBox", "myGridView", "My Column Name"); });

Just make sure you reference your controls with the correct ID. By using ClientIDMode=”Static” you can ensure it’s the same ID as you defined it in the ASP.NET markup.

<asp:TextBox ID="myTextBox" runat="server" ClientIDMode="Static" />
 
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" ClientIDMode="Static"
ShowHeaderWhenEmpty="True">
    <Columns>
        <asp:BoundField DataField="one" HeaderText="My first column" />
        <asp:BoundField DataField="two" HeaderText="My Column Name" />
    </Columns>
</asp:GridView>

You can even include the textbox as template fields in the grid header.

Make sure you are making use of jQuery libraries in order for this to work.

the future of education is here

no comment.
watch ;)

swessies.com

http://www.swessies.com

if anyone knows who Aletia is, and what she means to me, then you’ll also know how important it is to me that you visit her site! Swessies is what the two sisters, Aletia and Carmen call each other. So that’s their site and they put a new song per day (the very least!).

Subscribe, comment etc. It’s a great idea!